// File: /workspace/github_c_makefile_star2_O0/wtarreau[P]httpterm/httpterm.c
// Function: main
int main(int argc, char **argv) {
    int err;
    struct rlimit limit;
    FILE *pidfile = NULL;
    init(argc, argv);

    signal(SIGHUP, sig_dump_state);

    /* on very high loads, a sigpipe sometimes happen just between the
     * getsockopt() which tells "it's OK to write", and the following write :-(
     */
    signal(SIGPIPE, SIG_IGN);

    /* We will loop at most 100 times with 10 ms delay each time.
     * That's at most 1 second. We only send a signal to old pids
     * if we cannot grab at least one port.
     */
    err = start_proxies(1);

    /* Note: start_proxies() sends an alert when it fails. */
    if (err != ERR_NONE) {
	exit(1);
    }

    if (listeners == 0) {
	Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]);
	/* Note: we don't have to send anything to the old pids because we
	 * never stopped them. */
	exit(1);
    }

    if (global.mode & MODE_DAEMON) {
	global.mode &= ~MODE_VERBOSE;
	global.mode |= MODE_QUIET;
    }

    /* MODE_QUIET can inhibit alerts and warnings below this line */

    global.mode &= ~MODE_STARTING;
    if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) {
	/* detach from the tty */
	fclose(stdin); fclose(stdout); fclose(stderr);
	close(0); close(1); close(2);
    }

    /* open pid files before the chroot */
    if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
	int pidfd;
	unlink(global.pidfile);
	pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644);
	if (pidfd < 0) {
	    Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile);
	    exit(1);
	}
	pidfile = fdopen(pidfd, "w");
    }

    /* chroot if needed */
    if (global.chroot != NULL) {
	if (chroot(global.chroot) == -1) {
	    Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot);
	}
	(void)!chdir("/");
    }

    /* ulimits */
    if (!global.rlimit_nofile)
	global.rlimit_nofile = global.maxsock;

    if (global.rlimit_nofile) {
	limit.rlim_cur = limit.rlim_max = global.rlimit_nofile;
	if (setrlimit(RLIMIT_NOFILE, &limit) == -1) {
	    Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile);
	}
    }

    if (global.rlimit_memmax) {
	limit.rlim_cur = limit.rlim_max =
		global.rlimit_memmax * 1048576 / global.nbproc;
#ifdef RLIMIT_AS
	if (setrlimit(RLIMIT_AS, &limit) == -1) {
	    Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
		    argv[0], global.rlimit_memmax);
	}
#else
	if (setrlimit(RLIMIT_DATA, &limit) == -1) {
	    Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n",
		    argv[0], global.rlimit_memmax);
	}
#endif
    }

    /* Note that any error at this stage will be fatal because we will not
     * be able to restart the old pids.
     */

    /* setgid / setuid */
    if (global.gid && setgid(global.gid) == -1) {
	Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid);
	exit(1);
    }

    if (global.uid && setuid(global.uid) == -1) {
	Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid);
	exit(1);
    }

    /* check ulimits */
    limit.rlim_cur = limit.rlim_max = 0;
    getrlimit(RLIMIT_NOFILE, &limit);
    if (limit.rlim_cur < global.maxsock) {
	Warning("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. Please raise 'ulimit-n' to %d or more to avoid any trouble.\n",
		argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock);
    }

    if (global.mode & MODE_DAEMON) {
	int ret = 0;
	int proc;

	/* the father launches the required number of processes */
	for (proc = 0; proc < global.nbproc; proc++) {
	    ret = fork();
	    if (ret < 0) {
		Alert("[%s.main()] Cannot fork.\n", argv[0]);
		exit(1); /* there has been an error */
	    }
	    else if (ret == 0) /* child breaks here */
		break;
	    if (pidfile != NULL) {
		fprintf(pidfile, "%d\n", ret);
		fflush(pidfile);
	    }
	}
	/* close the pidfile both in children and father */
	if (pidfile != NULL)
	    fclose(pidfile);
	free(global.pidfile);

	if (proc == global.nbproc)
	    exit(0); /* parent must leave */

	/* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure
	 * that we can detach from the TTY. We MUST NOT do it in other cases since
	 * it would have already be done, and 0-2 would have been affected to listening
	 * sockets
	 */
    	if (!(global.mode & MODE_QUIET)) {
	    /* detach from the tty */
	    fclose(stdin); fclose(stdout); fclose(stderr);
	    close(0); close(1); close(2); /* close all fd's */
    	    global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */
	}
	pid = getpid(); /* update child's pid */
	setsid();
    }

#ifdef ENABLE_SPLICE
    init_splice();
#endif

#if defined(ENABLE_EPOLL)
    if (cfg_polling_mechanism & POLL_USE_EPOLL) {
	if (epoll_loop(POLL_LOOP_ACTION_INIT)) {
	    epoll_loop(POLL_LOOP_ACTION_RUN);
	    epoll_loop(POLL_LOOP_ACTION_CLEAN);
	    cfg_polling_mechanism &= POLL_USE_EPOLL;
	}
	else {
	    Warning("epoll() is not available. Using poll()/select() instead.\n");
	    cfg_polling_mechanism &= ~POLL_USE_EPOLL;
	}
    }
#endif

#if defined(ENABLE_POLL)
    if (cfg_polling_mechanism & POLL_USE_POLL) {
	if (poll_loop(POLL_LOOP_ACTION_INIT)) {
	    poll_loop(POLL_LOOP_ACTION_RUN);
	    poll_loop(POLL_LOOP_ACTION_CLEAN);
	    cfg_polling_mechanism &= POLL_USE_POLL;
	}
	else {
	    Warning("poll() is not available. Using select() instead.\n");
	    cfg_polling_mechanism &= ~POLL_USE_POLL;
	}
    }
#endif
    if (cfg_polling_mechanism & POLL_USE_SELECT) {
	if (select_loop(POLL_LOOP_ACTION_INIT)) {
	    select_loop(POLL_LOOP_ACTION_RUN);
	    select_loop(POLL_LOOP_ACTION_CLEAN);
	    cfg_polling_mechanism &= POLL_USE_SELECT;
	}
    }


    /* Do some cleanup */ 
    deinit();
    
    exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x30(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0xb530
movl $0x1, %edi
leaq -0x3346(%rip), %rsi     # 0x8da0
callq 0x2230
movl $0xd, %edi
movl $0x1, %esi
callq 0x2230
movl $0x1, %edi
callq 0xbbb0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0xc117
movl $0x1, %edi
callq 0x23e0
cmpl $0x0, 0x2633a(%rip)     # 0x32458
jne 0xc13f
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x2e44(%rip), %rdi      # 0xef72
movb $0x0, %al
callq 0x2d40
movl $0x1, %edi
callq 0x23e0
movl 0x5297(%rip), %eax      # 0x113dc
andl $0x8, %eax
cmpl $0x0, %eax
je 0xc16b
movl 0x5289(%rip), %eax      # 0x113dc
andl $-0x41, %eax
movl %eax, 0x5280(%rip)      # 0x113dc
movl 0x527a(%rip), %eax      # 0x113dc
orl $0x10, %eax
movl %eax, 0x5271(%rip)      # 0x113dc
movl 0x526b(%rip), %eax      # 0x113dc
andl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
movl %eax, 0x5260(%rip)      # 0x113dc
movl 0x525a(%rip), %eax      # 0x113dc
andl $0x10, %eax
cmpl $0x0, %eax
je 0xc1e0
movl 0x524c(%rip), %eax      # 0x113dc
andl $0x40, %eax
cmpl $0x0, %eax
jne 0xc1e0
movq 0x4e39(%rip), %rax      # 0x10fd8
movq (%rax), %rdi
callq 0x2100
movq 0x4e22(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x2100
movq 0x4e3b(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
callq 0x2100
xorl %edi, %edi
callq 0x21d0
movl $0x1, %edi
callq 0x21d0
movl $0x2, %edi
callq 0x21d0
movl 0x51f6(%rip), %eax      # 0x113dc
andl $0x8, %eax
cmpl $0x0, %eax
je 0xc25e
cmpq $0x0, 0x51f2(%rip)      # 0x113e8
je 0xc25e
movq 0x51e9(%rip), %rdi      # 0x113e8
callq 0x2080
movq 0x51dd(%rip), %rdi      # 0x113e8
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
movb $0x0, %al
callq 0x2360
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0xc24b
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq 0x51b5(%rip), %rdx      # 0x113e8
leaq 0x2d88(%rip), %rdi      # 0xefc2
movb $0x0, %al
callq 0x2d40
movl $0x1, %edi
callq 0x23e0
movl -0x34(%rbp), %edi
leaq 0x2d93(%rip), %rsi      # 0xefe8
callq 0x2300
movq %rax, -0x30(%rbp)
cmpq $0x0, 0x517a(%rip)      # 0x113e0
je 0xc2a1
movq 0x5171(%rip), %rdi      # 0x113e0
callq 0x20a0
cmpl $-0x1, %eax
jne 0xc295
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq 0x5159(%rip), %rdx      # 0x113e0
leaq 0x2d5c(%rip), %rdi      # 0xefea
movb $0x0, %al
callq 0x2d40
leaq 0x2d6e(%rip), %rdi      # 0xf00a
callq 0x2130
cmpl $0x0, 0x512c(%rip)      # 0x113d4
jne 0xc2b6
movl 0x5120(%rip), %eax      # 0x113d0
movl %eax, 0x511e(%rip)      # 0x113d4
cmpl $0x0, 0x5117(%rip)      # 0x113d4
je 0xc2fe
movslq 0x510e(%rip), %rax      # 0x113d4
movq %rax, -0x20(%rbp)
movq %rax, -0x28(%rbp)
movl $0x7, %edi
leaq -0x28(%rbp), %rsi
callq 0x22f0
cmpl $-0x1, %eax
jne 0xc2fc
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl 0x50e6(%rip), %edx      # 0x113d4
leaq 0x2d17(%rip), %rdi      # 0xf00c
movb $0x0, %al
callq 0x2eb0
jmp 0xc2fe
cmpl $0x0, 0x50d3(%rip)      # 0x113d8
je 0xc351
movl 0x50cb(%rip), %eax      # 0x113d8
shll $0x14, %eax
cltd
idivl 0x50b1(%rip)            # 0x113c8
cltq
movq %rax, -0x20(%rbp)
movq %rax, -0x28(%rbp)
movl $0x9, %edi
leaq -0x28(%rbp), %rsi
callq 0x22f0
cmpl $-0x1, %eax
jne 0xc34f
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl 0x5097(%rip), %edx      # 0x113d8
leaq 0x2cee(%rip), %rdi      # 0xf036
movb $0x0, %al
callq 0x2eb0
jmp 0xc351
cmpl $0x0, 0x506c(%rip)      # 0x113c4
je 0xc38f
movl 0x5064(%rip), %edi      # 0x113c4
callq 0x2340
cmpl $-0x1, %eax
jne 0xc38f
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl 0x504d(%rip), %edx      # 0x113c4
leaq 0x2ce6(%rip), %rdi      # 0xf064
movb $0x0, %al
callq 0x2d40
movl $0x1, %edi
callq 0x23e0
cmpl $0x0, 0x502a(%rip)      # 0x113c0
je 0xc3cd
movl 0x5022(%rip), %edi      # 0x113c0
callq 0x2400
cmpl $-0x1, %eax
jne 0xc3cd
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl 0x500b(%rip), %edx      # 0x113c0
leaq 0x2cc8(%rip), %rdi      # 0xf084
movb $0x0, %al
callq 0x2d40
movl $0x1, %edi
callq 0x23e0
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x7, %edi
leaq -0x28(%rbp), %rsi
callq 0x23f0
movq -0x28(%rbp), %rax
movslq 0x4fda(%rip), %rcx      # 0x113d0
cmpq %rcx, %rax
jae 0xc428
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rdx
movl 0x4fc0(%rip), %ecx      # 0x113cc
movl 0x4fbd(%rip), %r8d      # 0x113d0
movl 0x4fb6(%rip), %r9d      # 0x113d0
leaq 0x2c83(%rip), %rdi      # 0xf0a4
movb $0x0, %al
callq 0x2eb0
movl 0x4fae(%rip), %eax      # 0x113dc
andl $0x8, %eax
cmpl $0x0, %eax
je 0xc55f
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0x4f77(%rip), %eax      # 0x113c8
jge 0xc4bc
callq 0x2440
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0xc480
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x2caf(%rip), %rdi      # 0xf11e
movb $0x0, %al
callq 0x2d40
movl $0x1, %edi
callq 0x23e0
cmpl $0x0, -0x38(%rbp)
jne 0xc488
jmp 0xc4bc
jmp 0xc48a
cmpq $0x0, -0x30(%rbp)
je 0xc4af
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq 0x2c99(%rip), %rsi      # 0xf138
movb $0x0, %al
callq 0x2250
movq -0x30(%rbp), %rdi
callq 0x22d0
jmp 0xc4b1
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xc448
cmpq $0x0, -0x30(%rbp)
je 0xc4cc
movq -0x30(%rbp), %rdi
callq 0x2100
movq 0x4f15(%rip), %rdi      # 0x113e8
callq 0x2040
movl -0x3c(%rbp), %eax
cmpl 0x4ee7(%rip), %eax      # 0x113c8
jne 0xc4ea
xorl %edi, %edi
callq 0x23e0
movl 0x4eec(%rip), %eax      # 0x113dc
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xc54f
movq 0x4ad9(%rip), %rax      # 0x10fd8
movq (%rax), %rdi
callq 0x2100
movq 0x4ac2(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x2100
movq 0x4adb(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
callq 0x2100
xorl %edi, %edi
callq 0x21d0
movl $0x1, %edi
callq 0x21d0
movl $0x2, %edi
callq 0x21d0
movl 0x4e96(%rip), %eax      # 0x113dc
orl $0x10, %eax
movl %eax, 0x4e8d(%rip)      # 0x113dc
callq 0x20f0
movl %eax, 0x25fee(%rip)     # 0x32548
callq 0x21e0
movl 0x4e1f(%rip), %eax      # 0x11384
andl $0x1, %eax
cmpl $0x0, %eax
je 0xc59e
xorl %edi, %edi
callq 0x89c0
cmpl $0x0, %eax
je 0xc59c
movl $0x1, %edi
callq 0x89c0
movl $0x2, %edi
callq 0x89c0
movl 0x4df1(%rip), %eax      # 0x11384
andl $0x1, %eax
movl %eax, 0x4de8(%rip)      # 0x11384
jmp 0xc59e
callq 0xbf40
xorl %edi, %edi
callq 0x23e0