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

undefined4
FUN_0040a210(uint param_1,int *param_2,undefined4 param_3,undefined4 param_4,undefined4 param_5)

{
  int *piVar1;
  ushort uVar2;
  void *__ptr;
  size_t sVar3;
  undefined4 uVar4;
  int *piVar5;
  int iVar6;
  char *pcVar7;
  uint uVar8;
  int iVar9;
  int iVar10;
  undefined1 *puVar11;
  undefined1 *puVar12;
  char *pcVar13;
  size_t __size;
  char *pcVar14;
  void *__dest;
  undefined1 local_130;
  undefined1 local_12f [254];
  undefined1 uStack_31;
  uint local_30;
  
  piVar5 = (int *)*param_2;
  if (piVar5 != (int *)0x0) {
    __size = 0;
    do {
      piVar1 = piVar5 + 3;
      piVar5 = (int *)*piVar5;
      __size = __size + *piVar1 + 1   //possible issue identified - semgrep
    } while (piVar5 != (int *)0x0);
    if (__size != 0) {
      __ptr = malloc(__size)   //possible issue identified - semgrep
      if (__ptr == (void *)0x0) {
        uVar4 = dhcp6optstr(param_1);
        dprintf(3,s_dhcp6_set_domain_00428ac0,"memory allocation failed for %s domain options",uVar4
               )   //possible issue identified - semgrep
        return 0xffffffff;
      }
      param_2 = (int *)*param_2;
      if (param_2 == (int *)0x0) {
        uVar8 = 0;
      }
      else {
        local_30 = (uint)(&local_130 < &uStack_31);
        __dest = __ptr;
        do {
          pcVar14 = (char *)param_2[4];
          sVar3 = strlen(pcVar14);
          if (sVar3 != 0) {
            pcVar13 = pcVar14 + sVar3;
            puVar12 = &local_130;
            if (local_30 != 0) {
              do {
                if (pcVar13 <= pcVar14) break;
                iVar6 = 0;
                pcVar7 = pcVar14;
                do {
                  if ((*pcVar7 == '\0') || (*pcVar7 == '.')) break;
                  pcVar7 = pcVar7 + 1;
                  iVar6 = iVar6 + 1;
                } while (pcVar7 < pcVar13);
                if ((&uStack_31 <= puVar12 + iVar6 + 1) || (0x3e < iVar6 - 1U)) goto LAB_0040a558;
                *puVar12 = (char)iVar6;
                iVar10 = (int)*pcVar14;
                iVar9 = iVar10 * 2;
                uVar2 = *(ushort *)(___ctype_b + iVar9);
                if (((uVar2 & 4) == 0) ||
                   ((*(ushort *)(___ctype_b + pcVar14[iVar6 + -1] * 2) & 0x800) == 0))
                goto LAB_0040a558;
                puVar11 = puVar12 + 1;
                if (iVar6 != 0) {
                  puVar11 = puVar12 + 2;
                  while( true ) {
                    pcVar14 = pcVar14 + 1;
                    if (((uVar2 & 0x800) == 0) && (iVar10 != 0x2d)) goto LAB_0040a558;
                    if ((uVar2 & 1) == 0) {
                      puVar11[-1] = (char)iVar10;
                    }
                    else {
                      puVar11[-1] = (char)*(undefined2 *)(___ctype_tolower + iVar9);
                    }
                    if (iVar6 == 1) break;
                    iVar6 = iVar6 + -1;
                    puVar11 = puVar11 + 1;
                    iVar10 = (int)*pcVar14;
                    iVar9 = iVar10 * 2;
                    uVar2 = *(ushort *)(___ctype_b + iVar9);
                  }
                }
                if ((pcVar7 < pcVar13) && (*pcVar7 == '.')) {
                  pcVar7 = pcVar7 + 1;
                }
                puVar12 = puVar11;
                pcVar14 = pcVar7;
              } while (puVar11 < &uStack_31);
            }
            sVar3 = (int)(puVar12 + 1) - (int)&local_130   //possible issue identified - semgrep
            if ((puVar12 + 1 < &uStack_31) && (*puVar12 = 0, -1 < (int)sVar3)) goto LAB_0040a4ac;
LAB_0040a558:
            uVar4 = dhcp6optstr(param_1);
            pcVar14 = "failed to encode a %s domain name";
LAB_0040a574:
            dprintf(3,s_dhcp6_set_domain_00428ac0,pcVar14,uVar4)   //possible issue identified - semgrep
            goto LAB_0040a588;
          }
          sVar3 = 0;
LAB_0040a4ac:
          if ((int)((int)__ptr + (__size - (int)__dest)) < (int)sVar3) {
            uVar4 = dhcp6optstr(param_1);
            pcVar14 = "buffer length for %s domain name is too short";
            goto LAB_0040a574;
          }
          memcpy(__dest,&local_130,sVar3)   //possible issue identified - semgrep
          param_2 = (int *)*param_2;
          __dest = (void *)((int)__dest + sVar3);
        } while (param_2 != (int *)0x0);
        uVar8 = (int)__dest - (int)__ptr & 0xffff;
      }
      iVar6 = FUN_0040874c(param_1 & 0xffff,uVar8,__ptr,param_3,param_4,param_5);
      if (iVar6 == 0) {
        free(__ptr);
        return 0;
      }
LAB_0040a588:
      free(__ptr);
      return 0xffffffff;
    }
  }
  return 0;
}