/* WARNING: Heritage AFTER dead removal. Example location: r0x00423014 : 0x00403cbc */
/* WARNING: Globals starting with '_' overlap smaller symbols at the same address */
/* WARNING: Restarted to delay deadcode elimination for space: ram */

undefined4 tcprequest_main(int param_1,undefined4 *param_2)

{
  int iVar1;
  hostent *phVar2;
  uint __fd;
  uint uVar3;
  int *piVar4;
  __fd_mask *p_Var5;
  size_t __n;
  ssize_t sVar6;
  FILE *__stream;
  int iVar7;
  undefined4 uVar8;
  uint uVar9;
  char *pcVar10;
  int local_4c8 [2];
  timeval local_4c0;
  sockaddr local_4b8;
  uint local_4a8 [32];
  char local_428 [1024];
  
  __stream = _stdout;
  if (param_1 < 3) {
    FUN_00403810(*param_2);
    return 0;
  }
  uVar9 = 0x50;
  if (param_1 != 3) {
    uVar9 = atoi((char *)param_2[3]);
    if ((int)uVar9 < 1) {
      uVar9 = 0x50;
    }
    if (param_1 != 4) {
      uVar8 = *param_2;
      while (iVar1 = getopt(param_1 + -3,(char **)(param_2 + 3),"sf:t:"), 0 < iVar1) {   //possible issue identified - semgrep
        if (iVar1 == 0x73) {
          DAT_00422a24 = 1;
        }
        else if (iVar1 == 0x74) {
          DAT_00422110 = atoi(_optarg);
          if (DAT_00422110 < 1) {
            DAT_00422110 = 5;
          }
        }
        else {
          if (iVar1 != 0x66) {
            FUN_00403810(uVar8);
                    /* WARNING: Subroutine does not return */
            exit(0);
          }
          DAT_00422a20 = _optarg;
        }
      }
    }
  }
  pcVar10 = (char *)param_2[2];
  local_4c8[1] = 4;
  phVar2 = gethostbyname(pcVar10);
  if (phVar2 == (hostent *)0x0) {
    fprintf(_stderr,"Can not get host by name: %s\n",pcVar10);
    return 0xffffffff;
  }
  __fd = socket(2,2,0);
  if (__fd == 0xffffffff) {
    fputs("Can not create socket\n",_stderr);
    return 0xffffffff;
  }
  uVar3 = fcntl(__fd,3);
  fcntl(__fd,4,uVar3 | 0x80);
  local_4b8.sa_data._2_4_ = *(undefined4 *)*phVar2->h_addr_list;
  local_4b8.sa_data._0_2_ = (ushort)((uVar9 & 0xffff) << 8) | (ushort)((uVar9 & 0xffff) >> 8);
  local_4b8.sa_family = 2;
  local_4b8.sa_data[6] = '\0';
  local_4b8.sa_data[7] = '\0';
  local_4b8.sa_data[8] = '\0';
  local_4b8.sa_data[9] = '\0';
  local_4b8.sa_data[10] = '\0';
  local_4b8.sa_data[0xb] = '\0';
  local_4b8.sa_data[0xc] = '\0';
  local_4b8.sa_data[0xd] = '\0';
  iVar1 = connect(__fd,&local_4b8,0x10);
  if (iVar1 != -1) {
    return 0xffffffff;
  }
  piVar4 = __errno_location();
  if (*piVar4 == 0x96) {
    p_Var5 = (__fd_mask *)local_4a8;
    do {
      *p_Var5 = 0;
      p_Var5 = p_Var5 + 1;
    } while (p_Var5 != (__fd_mask *)local_428);
    local_4a8[__fd >> 5] = local_4a8[__fd >> 5] | 1 << (__fd & 0x1f);
    local_4c0.tv_sec = DAT_00422110;
    local_4c0.tv_usec = 0;
    iVar1 = select(__fd + 1,(fd_set *)0x0,(fd_set *)local_4a8,(fd_set *)0x0,&local_4c0);
    if (((0 < iVar1) &&
        (iVar1 = getsockopt(__fd,0xffff,0x1007,local_4c8,(socklen_t *)(local_4c8 + 1)), iVar1 == 0))
       && (local_4c8[0] == 0)) {
      uVar9 = fcntl(__fd,3);
      fcntl(__fd,4,uVar9 & 0xffffff7f);
      pcVar10 = (char *)param_2[1];
      __n = strlen(pcVar10);
      sVar6 = send(__fd,pcVar10,__n,0);
      if (sVar6 != -1) {
        if ((DAT_00422a20 != (char *)0x0) &&
           (__stream = fopen(DAT_00422a20,"w"), __stream == (FILE *)0x0)) {   //possible issue identified - semgrep
          __stream = _stdout;
        }
        uVar9 = 0;
LAB_00403cdc:
        sVar6 = recv(__fd,local_428,0x400,0)   //possible issue identified - semgrep
        iVar1 = 0;
        if (DAT_00422a24 != 0) {
LAB_00403dc0:
          if (5 < uVar9) goto LAB_00403ddc;
          if (sVar6 <= iVar1) goto LAB_00403cdc;
          iVar7 = (int)local_428[iVar1];
          switch(uVar9) {
          case 0:
            if (0x17 < (iVar7 - 9U & 0xff)) goto LAB_00403db8;
            if ((1 << (iVar7 - 9U & 0x1f) & 0x800013U) == 0) {
              uVar9 = 1;
            }
            break;
          case 1:
            if (iVar7 == 10) {
              uVar9 = 3;
            }
            else if (iVar7 == 0xd) {
              uVar9 = 2;
            }
            break;
          case 2:
            uVar9 = 4;
            if (iVar7 == 10) {
              iVar1 = iVar1 + 1;
              goto LAB_00403dc0;
            }
LAB_00403db8:
            uVar9 = 1;
            break;
          case 3:
          case 5:
            uVar9 = 6;
            if (iVar7 != 10) goto LAB_00403db8;
            break;
          case 4:
            uVar9 = 5;
            if (iVar7 != 0xd) goto LAB_00403db8;
            iVar1 = iVar1 + 1;
            goto LAB_00403dc0;
          }
          iVar1 = iVar1 + 1;
          goto LAB_00403dc0;
        }
        goto LAB_00403de0;
      }
      fputs("can not send request message\n",_stderr);
      goto LAB_00403e38;
    }
  }
  close(__fd);
  fprintf(_stderr,"Can not connect to %s:%d\n",pcVar10,uVar9);
  return 0xffffffff;
LAB_00403ddc:
  DAT_00422a24 = 0;
LAB_00403de0:
  if (0 < sVar6) {
    local_428[sVar6] = '\0';
    fputs(local_428 + iVar1,__stream);
    goto LAB_00403cdc;
  }
  if (__stream != _stdout) {
    fclose(__stream);
  }
LAB_00403e38:
  close(__fd);
  return 0;
}