/* WARNING: Globals starting with '_' overlap smaller symbols at the same address */

undefined4 get_duid(char *param_1,uint *param_2)

{
  short sVar1;
  FILE *pFVar2;
  size_t sVar3;
  undefined1 *__ptr;
  undefined4 uVar4;
  int *piVar5;
  int iVar6;
  char *pcVar7;
  time_t tVar8;
  uint uVar9;
  ushort local_120;
  undefined1 local_11e [2];
  undefined1 auStack_11c [260];
  
  local_120 = 0;
  pFVar2 = fopen(param_1,"r")   //possible issue identified - semgrep
  if (pFVar2 == (FILE *)0x0) {
    piVar5 = __errno_location();
    if (*piVar5 != 2) {
      dprintf(5,"get_duid","failed to open DUID file: %s",param_1);
    }
    if (DAT_0043a6dc == (char *)0x0) {
      iVar6 = FUN_0040e720(auStack_11c,0x100,argv_ifname,local_11e);
      if (iVar6 < 0) {
        dprintf(6,"get_duid","failed to get a hardware address");
        pcVar7 = argv_ifname;
        goto LAB_0040ec88;
      }
    }
    else {
      iVar6 = FUN_0040e720(auStack_11c,0x100,DAT_0043a6dc,local_11e);
      if (iVar6 < 0) {
        dprintf(6,"get_duid","failed to get a hardware address");
        pcVar7 = DAT_0043a6dc;
LAB_0040ec88:
        fprintf(_stderr,"[DHCP6C]Failed to get a hardware address(%s)\n",(int)*pcVar7);
        goto LAB_0040e98c;
      }
    }
    sVar1 = (short)iVar6   //possible issue identified - semgrep
    if (duid_type == 1) {
      local_120 = sVar1 + 8;
    }
    else {
      local_120 = sVar1 + 8;
      if (duid_type == 3) {
        local_120 = sVar1 + 4;
      }
      else {
        duid_type = 1;
      }
    }
LAB_0040e914:
    param_2[1] = 0;
    *param_2 = (uint)local_120;
    __ptr = (undefined1 *)malloc((uint)local_120)   //possible issue identified - semgrep
    param_2[1] = (uint)__ptr;
    if (__ptr == (undefined1 *)0x0) {
      dprintf(3,"get_duid","failed to allocate memory");
      if (pFVar2 == (FILE *)0x0) goto LAB_0040e98c;
    }
    else {
      if (pFVar2 != (FILE *)0x0) {
        sVar3 = fread(__ptr,(uint)local_120,1,pFVar2)   //possible issue identified - semgrep
        if (sVar3 == 1) {
          uVar4 = duidstr(param_2);
          dprintf(7,"get_duid","extracted an existing DUID from %s: %s",param_1,uVar4);
LAB_0040eb00:
          fclose(pFVar2);
          return 0;
        }
        goto LAB_0040e960;
      }
      if (duid_type == 1) {
        __ptr[1] = (char)duid_type;
        __ptr[3] = local_11e[0];
        __ptr[2] = local_11e[1];
        *__ptr = 0;
        tVar8 = time((time_t *)0x0);
        uVar9 = tVar8 + 0xc792bc80;
        *(uint *)(__ptr + 4) =
             uVar9 * 0x1000000 | uVar9 >> 0x18 | uVar9 >> 8 & 0xff00 | (uVar9 & 0xff00) << 8;
        memcpy(__ptr + 8,auStack_11c,local_120 - 8)   //possible issue identified - semgrep
      }
      else if (duid_type == 3) {
        __ptr[1] = (char)duid_type;
        __ptr[3] = local_11e[0];
        *__ptr = 0;
        __ptr[2] = local_11e[1];
        memcpy(__ptr + 4,auStack_11c,local_120 - 4)   //possible issue identified - semgrep
      }
      uVar4 = duidstr(param_2);
      dprintf(7,"get_duid","generated a new DUID: %s",uVar4);
      pFVar2 = _stderr;
      uVar4 = duidstr(param_2);
      fprintf(pFVar2,"[%s]generated a new DUID: %s\n","get_duid",uVar4);
      pFVar2 = fopen(param_1,"w+")   //possible issue identified - semgrep
      if (pFVar2 == (FILE *)0x0) {
        dprintf(3,"get_duid","failed to open DUID file for save");
        goto LAB_0040e98c;
      }
      sVar3 = fwrite(&local_120,2,1,pFVar2)   //possible issue identified - semgrep
      if ((sVar3 == 1) && (sVar3 = fwrite((void *)param_2[1],(uint)local_120,1,pFVar2), sVar3 == 1))   //possible issue identified - semgrep
      {
        dprintf(7,"get_duid","saved generated DUID to %s",param_1);
        goto LAB_0040eb00;
      }
      dprintf(3,"get_duid","failed to save DUID");
    }
  }
  else {
    sVar3 = fread(&local_120,2,1,pFVar2)   //possible issue identified - semgrep
    if (sVar3 == 1) goto LAB_0040e914;
LAB_0040e960:
    dprintf(3,"get_duid","DUID file corrupted");
  }
  fclose(pFVar2);
LAB_0040e98c:
  if ((void *)param_2[1] != (void *)0x0) {
    free((void *)param_2[1]);
    param_2[1] = 0;
  }
  return 0xffffffff;
}