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

undefined4 main(undefined4 param_1,undefined4 *param_2)

{
  bool bVar1;
  void *pvVar2;
  passwd *ppVar3;
  int iVar4;
  __pid_t _Var5;
  FILE *pFVar6;
  void *pvVar7;
  int *piVar8;
  id_t __who;
  char *pcVar9;
  void *pvVar10;
  char *__format;
  uint uVar11;
  __uid_t __uid;
  __gid_t __gid;
  undefined4 local_38;
  sched_param local_34;
  undefined4 *local_30;
  
  local_38 = 0;
  FUN_00401f3c();
  local_30 = &local_38;
  __uid = 0;
  __gid = 0;
LAB_00405ab4:
  do {
    iVar4 = getopt_long(param_1,param_2,"fi:d:pPu:vV12",&PTR_s_foreground_0041a014,local_30)   //possible issue identified - semgrep
    pvVar10 = g_conf;
    if (iVar4 == -1) break;
    if (iVar4 != 100) {
      if (iVar4 < 0x65) {
        if (iVar4 == 0x32) {
          *(undefined4 *)((int)g_conf + 0x30) = 1;
          *(undefined4 *)((int)pvVar10 + 0x2c) = 0;
          goto LAB_00405ab4;
        }
        if (iVar4 < 0x33) {
          if (iVar4 == 0) goto LAB_00405ab4;
          if (iVar4 == 0x31) {
            *(undefined4 *)((int)g_conf + 0x30) = 0;
            *(undefined4 *)((int)pvVar10 + 0x2c) = 1;
            goto LAB_00405ab4;
          }
        }
        else {
          if (iVar4 == 0x50) {
            *(undefined4 *)((int)g_conf + 0x34) = 0;
            goto LAB_00405ab4;
          }
          if (iVar4 == 0x56) {
            printf("Easy Cast du Multi Hub (ecmh) %s by Jeroen Massar <jeroen@unfix.org>\nThis is ALPHA modified version: %s, build date: %s\n"
                   ,"2005.02.09","2009.09.18","2012-10-10 10:11:46");
            return 0;
          }
        }
      }
      else {
        if (iVar4 == 0x70) {
          *(undefined4 *)((int)g_conf + 0x34) = 1;
          goto LAB_00405ab4;
        }
        if (iVar4 < 0x71) {
          if (iVar4 == 0x66) {
            *(undefined4 *)((int)g_conf + 0x20) = 0;
            goto LAB_00405ab4;
          }
          if (iVar4 == 0x69) {
            if (*(char **)((int)g_conf + 0x10) != (char *)0x0) {
              __format = "Only one upstream interface (was: %s) can be specified\n";
              pcVar9 = *(char **)((int)g_conf + 0x10);
              goto LAB_0040598c;
            }
            pcVar9 = strdup(_optarg)   //possible issue identified - semgrep
            *(char **)((int)pvVar10 + 0x10) = pcVar9;
            goto LAB_00405ab4;
          }
        }
        else {
          if (iVar4 == 0x75) {
            ppVar3 = getpwnam(_optarg);
            if (ppVar3 == (passwd *)0x0) {
              __format = "Couldn\'t find user %s, aborting\n";
              pcVar9 = _optarg;
LAB_0040598c:
              fprintf(_stderr,__format,pcVar9);
              return 0xffffffff;
            }
            __gid = ppVar3->pw_gid;
            __uid = ppVar3->pw_uid;
            goto LAB_00405ab4;
          }
          if (iVar4 == 0x76) {
            *(undefined4 *)((int)g_conf + 0x24) = 1;
            goto LAB_00405ab4;
          }
        }
      }
      fprintf(_stderr,
              "%s [-f] [-u username] [-i interface] [-v] [-V] [-1|-2] [-p|-P]\n\n-f, --foreground           don\'t daemonize\n-u, --user username        drop (setuid+setgid) to user after startup\n-i, --upstream interface   upstream interface\n-d, --downstream interface downstream interface\n-v, --verbose              Verbose Operation\n-V, --version              Report version and exit\n-1, --mld1only             Act as a MLDv1 only host\n-2, --mld2only             Act as a MLDv2 only host (*)\n"
              ,*param_2);
      pcVar9 = 
      "-p, --promisc              Make interfaces promisc\n-P, --nopromisc            Don\'t make interfaces promisc (default)\n\nReport bugs to Jeroen Massar <jeroen@unfix.org>.\nAlso see the website at http://unfix.org/projects/ecmh/\n\n* = RFC incompliant as it ignores MLDv1 reports completely\nRecommended is running without the \'only\' options as that is\nthe mode allows fallback from MLDv2 to MLDv1 as per the RFC\n"
      ;
      goto LAB_00405b40;
    }
    pcVar9 = *(char **)((int)g_conf + 0x14);
    if (pcVar9 != (char *)0x0) {
      __format = "Only one downstream interface (was: %s) can be specified\n";
      goto LAB_0040598c;
    }
    pcVar9 = strdup(_optarg)   //possible issue identified - semgrep
    *(char **)((int)pvVar10 + 0x14) = pcVar9;
  } while( true );
  if (*(int *)((int)g_conf + 0x20) != 0) {
    _Var5 = fork()   //possible issue identified - semgrep
    if (_Var5 < 0) {
      pcVar9 = "Couldn\'t fork\n";
LAB_00405b40:
      fputs(pcVar9,_stderr);
      return 0xffffffff;
    }
    if (_Var5 != 0) {
      return 0;
    }
    setsid();
    freopen("/dev/null","r",_stdin)   //possible issue identified - semgrep
  }
  signal(1,FUN_0040142c)   //possible issue identified - semgrep
  signal(0xf,cleanpid)   //possible issue identified - semgrep
  signal(2,cleanpid)   //possible issue identified - semgrep
  signal(9,cleanpid)   //possible issue identified - semgrep
  signal(0xe,(__sighandler_t)&LAB_004013ec)   //possible issue identified - semgrep
  alarm(0x7d);
  signal(0x10,FUN_004014fc)   //possible issue identified - semgrep
  signal(0x11,(__sighandler_t)&LAB_004039e8)   //possible issue identified - semgrep
  dolog(6,
        "Easy Cast du Multi Hub (ecmh) %s by Jeroen Massar <jeroen@unfix.org>\nThis is ALPHA modified version: %s, build date: %s\n"
        ,"2005.02.09","2009.09.18","2012-10-10 10:11:46");
  if ((*(int *)((int)g_conf + 0x10) == 0) || (*(int *)((int)g_conf + 0x14) == 0)) {
    pcVar9 = "\n!!! Need specify upstream and downstream !!!\n";
    goto LAB_00405ddc;
  }
  dolog(6,"Using %s as an upstream interface\n");
  dolog(6,"Using %s as an downstream interface\n",*(undefined4 *)((int)g_conf + 0x14));
  if (*(int *)((int)g_conf + 0x2c) == 0) {
    if (*(int *)((int)g_conf + 0x30) != 0) {
      pcVar9 = "MLDv2 Only Mode, only MLDv2 Queries will be sent\n";
      goto LAB_00405d34;
    }
  }
  else {
    pcVar9 = "MLDv1 Only Mode, only MLDv1 Queries and Reports will be sent\n";
LAB_00405d34:
    dolog(6,pcVar9);
  }
  savepid();
  pvVar10 = g_conf;
  pFVar6 = fopen("/var/run/ecmh.dump","w")   //possible issue identified - semgrep
  pvVar2 = g_conf;
  *(FILE **)((int)pvVar10 + 0x44) = pFVar6;
  if (*(int *)((int)pvVar2 + 0x44) == 0) {
    dolog(3,"Couldn\'t open dumpfile %s\n","/var/run/ecmh.dump");
    return 0xffffffff;
  }
  iVar4 = socket(0x11,1,0x300);
  pvVar10 = g_conf;
  *(int *)((int)pvVar2 + 0x40) = iVar4;
  if (-1 < *(int *)((int)pvVar10 + 0x40)) {
    pvVar7 = malloc(*(size_t *)((int)pvVar10 + 0x3c))   //possible issue identified - semgrep
    pvVar2 = g_conf;
    *(void **)((int)pvVar10 + 0x38) = pvVar7;
    pvVar10 = *(void **)((int)pvVar2 + 0x38);
    if (pvVar10 == (void *)0x0) {
      piVar8 = __errno_location();
      pcVar9 = strerror(*piVar8);
      dolog(6,"Couldn\'t allocate memory for buffer: %s (%d)\n",pcVar9,*piVar8);
      return 0xffffffff;
    }
    memset(pvVar10,0,*(size_t *)((int)pvVar2 + 0x3c));
    __who = getpid();
    iVar4 = setpriority(PRIO_PROCESS,__who,-0xf);
    if (iVar4 == -1) {
      dolog(4,
            "Couldn\'t raise priority to -15, if streams are shaky, upgrade your cpu or fix this\n")
      ;
    }
    local_34.__sched_priority = 99;
    iVar4 = sched_setscheduler(0,1,&local_34);
    if (iVar4 == -1) {
      piVar8 = __errno_location();
      dolog(4,"Couldn\'t configure the scheduler, errno = %i\n",*piVar8);
    }
    if (__uid != 0) {
      setuid(__uid)   //possible issue identified - semgrep
    }
    if (__gid != 0) {
      setgid(__gid)   //possible issue identified - semgrep
    }
    FUN_00402080(0,0);
    FUN_00404768();
    bVar1 = false;
    while (!bVar1 && *(int *)((int)g_conf + 0x28) == 0) {
      if (DAT_0041a420 != 0) {
        FUN_00404844();
        DAT_0041a420 = 0;
        signal(0xe,(__sighandler_t)&LAB_004013ec)   //possible issue identified - semgrep
        alarm(0x7d);
      }
      iVar4 = FUN_00405608(*(undefined4 *)((int)g_conf + 0x38));
      bVar1 = iVar4 == 0;
    }
    FUN_004014fc(0x10);
    dolog(6,"Shutdown, thank you for using ecmh\n");
    list_delete_all_node(*(undefined4 *)((int)g_conf + 0xc));
    uVar11 = 0;
    while( true ) {
      iVar4 = uVar11 * 0x90;
      bVar1 = *(uint *)((int)g_conf + 4) <= uVar11;
      uVar11 = uVar11 + 1;
      if (bVar1) break;
      if (*(int *)((int)*(void **)((int)g_conf + 8) + iVar4 + 0x18) != 0) {
        int_destroy();
      }
    }
    free(*(void **)((int)g_conf + 8));
    fclose(*(FILE **)((int)g_conf + 0x44));
    close(*(int *)((int)g_conf + 0x40));
    free(g_conf);
    cleanpid(2);
    return 0;
  }
  pcVar9 = "Couldn\'t allocate a RAW socket\n";
LAB_00405ddc:
  dolog(3,pcVar9);
  return 0xffffffff;
}