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

void udhcpc_main(undefined4 param_1,undefined4 param_2)

{
  uint uVar1;
  int iVar2;
  undefined1 uVar3;
  long lVar4;
  in_addr __in;
  undefined4 uVar5;
  undefined2 uVar6;
  int iVar7;
  undefined4 uVar8;
  __fd_mask *p_Var9;
  int iVar10;
  char *pcVar11;
  ssize_t sVar12;
  int *piVar13;
  int iVar14;
  in_addr_t *piVar15;
  uint uVar16;
  char *pcVar17;
  size_t __n;
  undefined1 *puVar18;
  uint uVar19;
  byte *pbVar20;
  size_t sVar21;
  undefined1 uVar22;
  uint uVar23;
  uint local_398;
  int local_394;
  in_addr local_390;
  in_addr local_38c;
  uint local_388;
  timeval local_384;
  char acStack_37c [16];
  char acStack_36c [16];
  undefined4 local_35c;
  undefined4 local_358;
  undefined2 local_354;
  undefined2 local_352;
  undefined2 local_350;
  undefined2 local_34e;
  sysinfo local_34c;
  __fd_mask local_30c [32];
  __fd_mask _Stack_28c;
  int local_288;
  in_addr local_27c;
  byte local_197 [303];
  int local_68;
  int local_64;
  uint local_60;
  uint local_5c;
  byte *local_58;
  uint *local_54;
  int *local_50;
  in_addr *local_4c;
  in_addr *local_48;
  uint *local_44;
  timeval *local_40;
  char *local_3c;
  char *local_38;
  char *local_34;
  sysinfo *local_30;
  __fd_mask *local_2c;
  
  DAT_0041b524 = 4;
  DAT_0041b52c = 0x3c;
  DAT_0041b528 = 5;
  local_44 = &local_388;
  local_64 = 0;
  local_68 = 0;
LAB_0040533c:
  local_388 = 0;
  iVar7 = getopt_long(param_1,param_2,"c:fbH:h:i:np:qr:s:vdD:R:S:ut",&PTR_s_clientid_0041b530,
                      local_44)   //possible issue identified - semgrep
  if (iVar7 != -1) {
    switch(iVar7) {
    case 0x44:
      DAT_0041b524 = atoi(_optarg);
      goto LAB_0040533c;
    default:
      puts(
          "Usage: udhcpc [OPTIONS]\n\n  -c, --clientid=CLIENTID         Client identifier\n  -H, --hostname=HOSTNAME         Client hostname\n  -h                              Alias for -H\n  -f, --foreground                Do not fork after getting lease\n  -b, --background                Fork to background if lease cannot be\n                                  immediately negotiated.\n  -i, --interface=INTERFACE       Interface to use (default: eth0)\n  -n, --now                       Exit with failure if lease cannot be\n                                  immediately negotiated.\n  -p, --pidfile=file              Store process ID of daemon in file\n  -q, --quit                      Quit after obtaining lease\n  -r, --request=IP                IP address to request (default: none)\n  -s, --script=file               Run file at dhcp events (default:\n                                  /usr/share/udhcpc/default.script)\n  -v, --version                   Display version"
          );
                    /* WARNING: Subroutine does not return */
      exit(0);
    case 0x48:
    case 0x68:
      __n = strlen(_optarg);
      if (__n < 0x100) {
        sVar21 = __n + 2   //possible issue identified - semgrep
        uVar22 = (undefined1)__n;
      }
      else {
        sVar21 = 0x101   //possible issue identified - semgrep
        uVar22 = 0xff;
        __n = 0xff   //possible issue identified - semgrep
      }
      if (DAT_0041b514 != (undefined1 *)0x0) {
        free(DAT_0041b514);
      }
      puVar18 = (undefined1 *)malloc(sVar21)   //possible issue identified - semgrep
      pcVar11 = _optarg;
      puVar18[1] = uVar22;
      DAT_0041b514 = puVar18;
      *puVar18 = 0xc;
      break;
    case 0x52:
      DAT_0041b528 = atoi(_optarg);
      goto LAB_0040533c;
    case 0x53:
      DAT_0041b52c = atoi(_optarg);
      goto LAB_0040533c;
    case 0x62:
      DAT_0041b503 = '\x01';
      goto LAB_0040533c;
    case 99:
      __n = strlen(_optarg);
      if (__n < 0x100) {
        sVar21 = __n + 2   //possible issue identified - semgrep
        uVar22 = (undefined1)__n;
      }
      else {
        sVar21 = 0x101   //possible issue identified - semgrep
        uVar22 = 0xff;
        __n = 0xff   //possible issue identified - semgrep
      }
      if (DAT_0041b510 != (undefined1 *)0x0) {
        free(DAT_0041b510);
      }
      puVar18 = (undefined1 *)malloc(sVar21)   //possible issue identified - semgrep
      pcVar11 = _optarg;
      puVar18[1] = uVar22;
      DAT_0041b510 = puVar18;
      *puVar18 = 0x3d;
      puVar18[2] = 0;
      break;
    case 100:
      local_64 = local_64 + 1;
      DAT_0041b524 = 2;
      DAT_0041b528 = 2;
      DAT_0041b52c = 0;
      goto LAB_0040533c;
    case 0x66:
      client_config = '\x01';
      goto LAB_0040533c;
    case 0x69:
      PTR_DAT_0041b504 = _optarg;
      goto LAB_0040533c;
    case 0x6e:
      DAT_0041b502 = '\x01';
      goto LAB_0040533c;
    case 0x70:
      DAT_0041b508 = _optarg;
      goto LAB_0040533c;
    case 0x71:
      DAT_0041b501 = '\x01';
      goto LAB_0040533c;
    case 0x72:
      DAT_0041bc04.s_addr = inet_addr(_optarg);
      goto LAB_0040533c;
    case 0x73:
      PTR_s__usr_share_udhcpc_default_script_0041b50c = _optarg;
      goto LAB_0040533c;
    case 0x74:
      local_68 = local_68 + 1;
      DAT_0041b524 = 2;
      DAT_0041b528 = 2;
      DAT_0041b52c = 0;
      goto LAB_0040533c;
    case 0x75:
      DAT_0041bc00 = 1;
      goto LAB_0040533c;
    case 0x76:
      goto switchD_004053b8_caseD_76;
    }
    strncpy(puVar18 + 2,pcVar11,__n)   //possible issue identified - semgrep
    goto LAB_0040533c;
  }
  uVar8 = pidfile_acquire(DAT_0041b508);
  pidfile_write_release(uVar8);
  iVar7 = read_interface(PTR_DAT_0041b504,&DAT_0041b518,0,&DAT_0041b51c);
  if (iVar7 < 0) {
    FUN_004051c0(1);
  }
  if (DAT_0041b510 == (undefined1 *)0x0) {
    DAT_0041b510 = (undefined1 *)malloc(9)   //possible issue identified - semgrep
    uVar8 = DAT_0041b51c;
    *DAT_0041b510 = 0x3d;
    DAT_0041b510[2] = 1;
    DAT_0041b510[1] = 7;
    uVar3 = DAT_0041b521;
    uVar22 = DAT_0041b520;
    *(undefined4 *)(DAT_0041b510 + 3) = uVar8;
    DAT_0041b510[7] = uVar22;
    DAT_0041b510[8] = uVar3;
  }
  iVar7 = fcntl(0,3);
  if (iVar7 == -1) {
    open("/dev/null",0)   //possible issue identified - semgrep
  }
  iVar7 = fcntl(1,3);
  if (iVar7 == -1) {
    open("/dev/null",1)   //possible issue identified - semgrep
  }
  iVar7 = fcntl(2,3);
  if (iVar7 == -1) {
    open("/dev/null",1)   //possible issue identified - semgrep
  }
  pipe((int *)&DAT_0041bc20)   //possible issue identified - semgrep
  signal(0x10,FUN_00405234)   //possible issue identified - semgrep
  signal(0x11,FUN_00405234)   //possible issue identified - semgrep
  signal(0xf,FUN_00405234)   //possible issue identified - semgrep
  DAT_0041bc10 = 0;
  FUN_00404f6c(2);
  local_30 = &local_34c;
  DAT_0041bc08 = 0;
  sysinfo(local_30);
  DAT_0041bc18 = local_34c.uptime;
  local_2c = local_30c;
  local_54 = &local_398;
  local_40 = &local_384;
  local_58 = local_197;
  local_38 = acStack_36c;
  local_4c = &local_390;
  local_48 = &local_38c;
  local_34 = (char *)&local_35c;
  local_3c = acStack_37c;
  local_50 = &local_394;
  local_5c = 0;
  local_60 = 0;
  iVar7 = 0;
  uVar16 = 0;
switchD_00405920_default:
  iVar14 = iVar7;
  sysinfo(local_30);
  local_384.tv_sec = DAT_0041bc18 - local_34c.uptime;
  local_384.tv_usec = 0;
  p_Var9 = local_2c;
  do {
    *p_Var9 = 0;
    p_Var9 = p_Var9 + 1;
  } while (&_Stack_28c != p_Var9);
  if (DAT_0041bc28 == 0) {
LAB_00405848:
    if (-1 < (int)DAT_0041bc1c) goto LAB_004056ac;
  }
  else {
    if ((int)DAT_0041bc1c < 0) {
      if (DAT_0041bc28 == 1) {
        DAT_0041bc1c = listen_socket(0,0x44,PTR_DAT_0041b504);
      }
      else {
        DAT_0041bc1c = raw_socket(DAT_0041b518);
      }
      if ((int)DAT_0041bc1c < 0) {
        FUN_004051c0(0);
        goto LAB_00405848;
      }
    }
LAB_004056ac:
    local_54[(DAT_0041bc1c >> 5) + 0x23] =
         local_54[(DAT_0041bc1c >> 5) + 0x23] | 1 << (DAT_0041bc1c & 0x1f);
  }
  local_54[(DAT_0041bc20 >> 5) + 0x23] =
       local_54[(DAT_0041bc20 >> 5) + 0x23] | 1 << (DAT_0041bc20 & 0x1f);
  iVar7 = iVar14;
  if (local_384.tv_sec < 1) {
    sysinfo(local_30);
    goto LAB_004058ec;
  }
  uVar23 = DAT_0041bc1c;
  if ((int)DAT_0041bc1c < (int)DAT_0041bc20) {
    uVar23 = DAT_0041bc20;
  }
  iVar10 = select(uVar23 + 1,(fd_set *)local_2c,(fd_set *)0x0,(fd_set *)0x0,local_40);
  sysinfo(local_30);
  lVar4 = local_34c.uptime;
  uVar23 = local_34c.uptime;
  if (iVar10 == 0) goto LAB_004058ec;
  if (iVar10 < 1) {
LAB_00405c9c:
    if ((iVar10 != -1) || (piVar13 = __errno_location(), *piVar13 != 4))
    goto switchD_00405920_default;
LAB_00405cd8:
    sVar12 = read(DAT_0041bc20,local_50,4)   //possible issue identified - semgrep
    if (-1 < sVar12) {
      if (local_394 == 0x10) {
        FUN_00404fd4();
        goto switchD_00405920_default;
      }
      if (local_394 == 0x11) {
        FUN_004050b0();
        goto switchD_00405920_default;
      }
      if (local_394 == 0xf) {
        FUN_004050b0();
        FUN_004051c0(0);
        goto switchD_00405920_default;
      }
    }
  }
  else {
    if ((DAT_0041bc28 == 0) ||
       ((local_54[(DAT_0041bc1c >> 5) + 0x23] >> (DAT_0041bc1c & 0x1f) & 1) == 0)) {
      if ((local_54[(DAT_0041bc20 >> 5) + 0x23] >> (DAT_0041bc20 & 0x1f) & 1) == 0)
      goto LAB_00405c9c;
      goto LAB_00405cd8;
    }
    if (DAT_0041bc28 == 1) {
      iVar10 = get_packet(&_Stack_28c);
      iVar2 = local_288;
    }
    else {
      iVar10 = get_raw_packet(&_Stack_28c);
      iVar2 = local_288;
    }
    local_288 = iVar2;
    if (iVar10 == -1) {
      piVar13 = __errno_location();
      if (*piVar13 == 4) goto switchD_00405920_caseD_5;
      FUN_00404f6c(DAT_0041bc28);
      goto switchD_00405920_default;
    }
    if (iVar10 < 0) goto switchD_00405920_default;
    if (iVar2 == iVar14) {
      pcVar11 = (char *)get_option(&_Stack_28c,0x35);
      __in.s_addr = local_27c.s_addr;
      iVar7 = iVar2;
      if ((pcVar11 != (char *)0x0) && (DAT_0041bc10 < 7)) {
        uVar19 = 1 << (DAT_0041bc10 & 0x1f);
        if ((uVar19 & 0x5a) != 0) {
          if (*pcVar11 == '\x05') {
            if ((DAT_0041bc10 != 1) ||
               (iVar10 = arpping(local_27c.s_addr,local_27c.s_addr,&DAT_0041b51c,PTR_DAT_0041b504),
               iVar10 != 0)) {
              uVar16 = get_option(&_Stack_28c,0x33);
              if (uVar16 == 0) {
                local_398 = 0xe10;
              }
              else {
                uVar23 = uVar16 + 3 & 3;
                uVar19 = uVar16 & 3;
                uVar16 = (*(int *)((uVar16 + 3) - uVar23) << (3 - uVar23) * 8 |
                         uVar16 & 0xffffffffU >> (uVar23 + 1) * 8) & -1 << (4 - uVar19) * 8 |
                         *(uint *)(uVar16 - uVar19) >> uVar19 * 8;
                local_398 = uVar16 << 0x18 | uVar16 >> 0x18 | uVar16 >> 8 & 0xff00 |
                            (uVar16 & 0xff00) << 8;
              }
              local_5c = local_398 >> 1;
              local_60 = local_398 * 7 >> 3;
              pcVar11 = inet_ntoa(DAT_0041bc14);
              pcVar11 = strdup(pcVar11)   //possible issue identified - semgrep
              pcVar17 = inet_ntoa(local_27c);
              pcVar17 = strdup(pcVar17)   //possible issue identified - semgrep
              syslog(0xe5,"DHCP: Client receive ACK from %s, IP=%s, Lease time=%d.",pcVar11,pcVar17,
                     local_398);
              free(pcVar11);
              free(pcVar17);
              DAT_0041bc18 = lVar4 + local_5c;
              DAT_0041bc04.s_addr = local_27c.s_addr;
              if (DAT_0041bc10 - 3 < 2) {
                pcVar11 = "renew";
              }
              else {
                pcVar11 = "bound";
              }
              run_script(&_Stack_28c,pcVar11);
              DAT_0041bc10 = 2;
              FUN_00404f6c(0);
              if (DAT_0041b501 != '\0') {
                FUN_004051c0(0);
              }
              uVar16 = lVar4;
              if (client_config == '\0') {
                FUN_00404f00();
              }
              goto switchD_00405920_default;
            }
            pcVar11 = inet_ntoa(__in);
            syslog(0xe5,"DHCP: Client receive ACK, but the address (%s) is used by another host.",
                   pcVar11);
            send_decline(iVar14,DAT_0041bc14.s_addr,__in.s_addr);
            DAT_0041bc18 = lVar4;
            DAT_0041bc10 = 0;
            DAT_0041bc04.s_addr = 0;
            DAT_0041bc08 = 0;
          }
          else {
            if ((*pcVar11 != '\x06') || (uVar23 = get_option(&_Stack_28c,0x36), uVar23 == 0))
            goto switchD_00405920_default;
            uVar19 = uVar23 + 3 & 3;
            uVar1 = uVar23 & 3;
            uVar23 = (*(int *)((uVar23 + 3) - uVar19) << (3 - uVar19) * 8 |
                     uVar23 & 0xffffffffU >> (uVar19 + 1) * 8) & -1 << (4 - uVar1) * 8 |
                     *(uint *)(uVar23 - uVar1) >> uVar1 * 8;
            *local_44 = uVar23;
            if (uVar23 != DAT_0041bc14.s_addr) goto switchD_00405920_default;
            syslog(0xe5,"DHCP: Client receive NAK.");
            run_script(&_Stack_28c,&DAT_0040a510);
            if (DAT_0041bc10 != 1) {
              run_script(0,"deconfig");
            }
            DAT_0041bc18 = lVar4;
            DAT_0041bc10 = 0;
            DAT_0041bc04.s_addr = 0;
            DAT_0041bc08 = 0;
            FUN_00404f6c(2);
          }
          sleep(3);
          goto switchD_00405920_default;
        }
        if ((((uVar19 & 1) != 0) && (*pcVar11 == '\x02')) &&
           (piVar15 = (in_addr_t *)get_option(&_Stack_28c,0x36), piVar15 != (in_addr_t *)0x0)) {
          if (local_64 != 0) {
            puts("yes");
            uVar8 = local_35c;
            uVar5 = local_358;
            uVar6 = local_354;
            if (local_68 != 0) {
              local_35c = 0x2e302e30;
              local_354 = 0x30;
              local_358 = 0x35322e30;
              local_352 = 0;
              local_350 = 0;
              local_34e = 0;
              DAT_0041bc14.s_addr = *piVar15;
              uVar8 = 0x2e302e30;
              uVar5 = 0x35322e30;
              uVar6 = 0x30;
              if (DAT_0041b675 != '\0') {
                iVar7 = 1;
                pbVar20 = local_58;
                do {
                  if ((pbVar20[-5] == 1) && (pbVar20[-4] == 4)) {
                    sprintf(local_38,"%d.%d.%d.%d",(uint)pbVar20[-3],(uint)pbVar20[-2],
                            (uint)pbVar20[-1],(uint)*pbVar20)   //possible issue identified - semgrep
                    if (pbVar20[-3] != 0) {
                      if (pbVar20[-2] == 0) {
                        if ((pbVar20[-1] == 0) && (*pbVar20 == 0)) {
                          printf("%d\n",8);
                        }
                      }
                      else if (pbVar20[-1] == 0) {
                        if (*pbVar20 == 0) {
                          printf("%d\n",0x10);
                        }
                      }
                      else if (*pbVar20 == 0) {
                        printf("%d\n",0x18);
                      }
                      else {
                        printf("%d\n",0x20);
                      }
                    }
                    local_4c->s_addr = 0;
                    inet_aton(local_38,local_4c);
                    local_48->s_addr = 0;
                    inet_aton(local_34,local_48);
                    DAT_0041bc0c = local_27c.s_addr;
                    pcVar11 = inet_ntoa((in_addr)(local_27c.s_addr & local_390.s_addr |
                                                 local_38c.s_addr));
                    strcpy(local_3c,pcVar11)   //possible issue identified - semgrep
                    puts(local_3c);
                  }
                  iVar14 = iVar7 * 0x16;
                  iVar7 = iVar7 + 1;
                  pbVar20 = pbVar20 + 1;
                  uVar8 = local_35c;
                  uVar5 = local_358;
                  uVar6 = local_354;
                } while ((&DAT_0041b675)[iVar14] != '\0');
              }
            }
            local_354 = uVar6;
            local_358 = uVar5;
            local_35c = uVar8;
            FUN_004051c0(0);
          }
          iVar7 = local_288;
          DAT_0041bc14.s_addr = *piVar15;
          DAT_0041bc04.s_addr = local_27c.s_addr;
          pcVar11 = inet_ntoa(DAT_0041bc14);
          syslog(0xe5,"DHCP: Client receive OFFER from %s.",pcVar11);
          DAT_0041bc10 = 1;
          DAT_0041bc08 = 0;
          DAT_0041bc18 = uVar23;
        }
      }
      goto switchD_00405920_default;
    }
  }
  goto switchD_00405920_caseD_5;
switchD_004053b8_caseD_76:
  printf("udhcpcd, version %s\n\n","0.9.8");
  FUN_004051c0(0);
  goto LAB_0040533c;
LAB_004058ec:
  lVar4 = local_34c.uptime;
  switch(DAT_0041bc10) {
  case 0:
    if ((int)DAT_0041bc08 < DAT_0041b528) {
      if (DAT_0041bc08 == 0) {
        iVar14 = random_xid();
      }
      send_discover(iVar14,DAT_0041bc04.s_addr);
      syslog(0xe5,"DHCP: Client send DISCOVER.");
      DAT_0041bc18 = DAT_0041b524 << (DAT_0041bc08 & 0x1f);
      if (0x40 < DAT_0041bc18) {
        DAT_0041bc18 = 0x40;
      }
      DAT_0041bc18 = lVar4 + DAT_0041bc18;
      DAT_0041bc08 = DAT_0041bc08 + 1;
      iVar7 = iVar14;
    }
    else {
      if (local_64 != 0) {
        puts("no");
        FUN_004051c0(1);
      }
      if (DAT_0041b503 == '\0') {
        if (DAT_0041b502 != '\0') {
          FUN_004051c0(1);
        }
      }
      else {
        FUN_00404f00();
      }
      DAT_0041bc08 = 0;
      if (DAT_0041b52c == 0) {
        FUN_004051c0(1);
      }
      DAT_0041bc18 = DAT_0041b52c + lVar4;
    }
    goto switchD_00405920_default;
  case 1:
  case 6:
    if ((int)DAT_0041bc08 < DAT_0041b528) {
      if (DAT_0041bc10 == 6) {
        send_renew(iVar14,DAT_0041bc14.s_addr,DAT_0041bc04.s_addr);
      }
      else {
        send_selecting(iVar14,DAT_0041bc14.s_addr,DAT_0041bc04.s_addr);
      }
      pcVar11 = inet_ntoa(DAT_0041bc04);
      pcVar11 = strdup(pcVar11)   //possible issue identified - semgrep
      pcVar17 = inet_ntoa(DAT_0041bc14);
      pcVar17 = strdup(pcVar17)   //possible issue identified - semgrep
      syslog(0xe5,"DHCP: Client send REQUEST, Request IP %s from %s.",pcVar11,pcVar17);
      free(pcVar11);
      free(pcVar17);
      DAT_0041bc18 = DAT_0041b524 << (DAT_0041bc08 & 0x1f);
      if (0x40 < DAT_0041bc18) {
        DAT_0041bc18 = 0x40;
      }
      DAT_0041bc08 = DAT_0041bc08 + 1;
      DAT_0041bc18 = lVar4 + DAT_0041bc18;
      goto switchD_00405920_default;
    }
    if (DAT_0041bc10 == 6) {
      run_script(0,"deconfig");
    }
    if (DAT_0041b52c == 0) {
      FUN_004051c0(1);
    }
    DAT_0041bc10 = 0;
    break;
  case 2:
    DAT_0041bc10 = 3;
    FUN_00404f6c(1);
  case 3:
    uVar23 = local_60 - local_5c;
    if (local_398 / 0x3840 + 1 < uVar23) {
      send_renew(iVar14,DAT_0041bc14.s_addr,DAT_0041bc04.s_addr);
      local_5c = local_5c + (uVar23 >> 1);
      DAT_0041bc18 = local_5c + uVar16;
    }
    else {
      DAT_0041bc10 = 4;
      DAT_0041bc18 = lVar4 + uVar23;
    }
    goto switchD_00405920_default;
  case 4:
    if (local_398 / 0x3840 + 1 < local_398 - local_60) {
      send_renew(iVar14,0,DAT_0041bc04.s_addr);
      local_60 = local_60 + (local_398 - local_60 >> 1);
      DAT_0041bc18 = local_60 + uVar16;
      goto switchD_00405920_default;
    }
    DAT_0041bc10 = 0;
    run_script(0,"deconfig");
    break;
  case 5:
switchD_00405920_caseD_5:
    goto switchD_00405920_default;
  case 7:
    DAT_0041bc18 = 0x7fffffff;
  default:
    goto switchD_00405920_default;
  }
  DAT_0041bc18 = lVar4;
  DAT_0041bc08 = 0;
  FUN_00404f6c(2);
  goto switchD_00405920_default;
}