// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
callq 0x7aee
leaq -0x2259(%rip), %rsi     # 0x6382
movl $0x1, %edi
callq 0x2210
movl $0x1, %esi
movl $0xd, %edi
callq 0x2210
movl $0x1, %edi
callq 0x81b6
testl %eax, %eax
jne 0x8823
cmpl $0x0, 0x25e3b(%rip)     # 0x2e448
jne 0x8625
movq (%rbx), %rsi
leaq 0x292a(%rip), %rdi      # 0xaf43
xorl %eax, %eax
callq 0x29fa
jmp 0x8823
movl 0x4d9d(%rip), %eax      # 0xd3c8
movl %eax, %ecx
andl $-0x51, %ecx
orl $0x10, %ecx
testb $0x8, %al
cmovel %eax, %ecx
movl %ecx, %eax
andl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
movl %eax, 0x4d83(%rip)      # 0xd3c8
andl $0x50, %ecx
cmpl $0x10, %ecx
jne 0x869b
movq 0x4984(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
callq 0x2110
movq 0x496d(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2110
movq 0x4986(%rip), %rax      # 0xcff8
movq (%rax), %rdi
callq 0x2110
xorl %edi, %edi
callq 0x21b0
movl $0x1, %edi
callq 0x21b0
movl $0x2, %edi
callq 0x21b0
movl 0x4d2d(%rip), %eax      # 0xd3c8
shrb $0x3, %al
movq 0x4d33(%rip), %rdi      # 0xd3d8
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x86f0
callq 0x2090
movq 0x4d1a(%rip), %rdi      # 0xd3d8
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x2330
testl %eax, %eax
jns 0x86f5
movq (%rbx), %rsi
movq 0x4cfb(%rip), %rdx      # 0xd3d8
leaq 0x28af(%rip), %rdi      # 0xaf93
xorl %eax, %eax
callq 0x29fa
jmp 0x8823
xorl %r14d, %r14d
jmp 0x8706
leaq 0x28bd(%rip), %rsi      # 0xafb9
movl %eax, %edi
callq 0x22e0
movq %rax, %r14
movq 0x4cc3(%rip), %rdi      # 0xd3d0
testq %rdi, %rdi
je 0x8740
callq 0x20b0
cmpl $-0x1, %eax
jne 0x8734
movq (%rbx), %rsi
movq 0x4caa(%rip), %rdx      # 0xd3d0
leaq 0x288e(%rip), %rdi      # 0xafbb
xorl %eax, %eax
callq 0x29fa
leaq 0x28a0(%rip), %rdi      # 0xafdb
callq 0x2140
movl 0x4c72(%rip), %eax      # 0xd3b8
testl %eax, %eax
jne 0x875a
movl 0x4c60(%rip), %eax      # 0xd3b0
movl %eax, 0x4c62(%rip)      # 0xd3b8
testl %eax, %eax
je 0x878c
cltq
movq %rsp, %rsi
movq %rax, 0x8(%rsi)
movq %rax, (%rsi)
movl $0x7, %edi
callq 0x22d0
cmpl $-0x1, %eax
jne 0x878c
movq (%rbx), %rsi
movl 0x4c3a(%rip), %edx      # 0xd3b8
leaq 0x2858(%rip), %rdi      # 0xafdd
xorl %eax, %eax
callq 0x2b2f
movl 0x4c2e(%rip), %eax      # 0xd3c0
testl %eax, %eax
je 0x87d2
shll $0x14, %eax
cltd
idivl 0x4c00(%rip)            # 0xd3a0
cltq
movq %rsp, %rsi
movq %rax, 0x8(%rsi)
movq %rax, (%rsi)
movl $0x9, %edi
callq 0x22d0
cmpl $-0x1, %eax
jne 0x87d2
movq (%rbx), %rsi
movl 0x4bfc(%rip), %edx      # 0xd3c0
leaq 0x283c(%rip), %rdi      # 0xb007
xorl %eax, %eax
callq 0x2b2f
movl 0x4bc0(%rip), %edi      # 0xd398
testl %edi, %edi
je 0x87f8
callq 0x2310
cmpl $-0x1, %eax
jne 0x87f8
movq (%rbx), %rsi
movl 0x4ba9(%rip), %edx      # 0xd398
leaq 0x283f(%rip), %rdi      # 0xb035
jmp 0x881c
movl 0x4b92(%rip), %edi      # 0xd390
testl %edi, %edi
je 0x882d
callq 0x23d0
cmpl $-0x1, %eax
jne 0x882d
movq (%rbx), %rsi
movl 0x4b7b(%rip), %edx      # 0xd390
leaq 0x2839(%rip), %rdi      # 0xb055
xorl %eax, %eax
callq 0x29fa
movl $0x1, %edi
callq 0x23b0
xorps %xmm0, %xmm0
movq %rsp, %r15
movaps %xmm0, (%r15)
movl $0x7, %edi
movq %r15, %rsi
callq 0x23c0
movq (%r15), %rdx
movslq 0x4b62(%rip), %r8       # 0xd3b0
cmpq %r8, %rdx
jae 0x886d
movq (%rbx), %rsi
movl 0x4b4c(%rip), %ecx      # 0xd3a8
leaq 0x2812(%rip), %rdi      # 0xb075
movl %r8d, %r9d
xorl %eax, %eax
callq 0x2b2f
testb $0x8, 0x4b54(%rip)      # 0xd3c8
je 0x8950
xorl %ebp, %ebp
cmpl $0x0, 0x4b1d(%rip)      # 0xd3a0
jle 0x88c3
leaq 0x287d(%rip), %r15      # 0xb109
xorl %ebp, %ebp
callq 0x2400
testl %eax, %eax
js 0x8987
je 0x88c3
testq %r14, %r14
je 0x88b9
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x2230
movq %r14, %rdi
callq 0x22b0
incl %ebp
cmpl 0x4adf(%rip), %ebp      # 0xd3a0
jl 0x888e
testq %r14, %r14
je 0x88d0
movq %r14, %rdi
callq 0x2110
movq 0x4b01(%rip), %rdi      # 0xd3d8
callq 0x2050
cmpl 0x4abe(%rip), %ebp      # 0xd3a0
je 0x8980
testb $0x10, 0x4ad9(%rip)     # 0xd3c8
jne 0x8940
movq 0x46e0(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
callq 0x2110
movq 0x46c9(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2110
movq 0x46e2(%rip), %rax      # 0xcff8
movq (%rax), %rdi
callq 0x2110
xorl %edi, %edi
callq 0x21b0
movl $0x1, %edi
callq 0x21b0
movl $0x2, %edi
callq 0x21b0
orb $0x10, 0x4a88(%rip)     # 0xd3c8
callq 0x2100
movl %eax, 0x25bed(%rip)     # 0x2e538
callq 0x21c0
testb $0x1, 0x49fd(%rip)      # 0xd354
je 0x897b
xorl %edi, %edi
callq 0x60d6
movl $0x1, %edi
callq 0x60d6
movl $0x2, %edi
callq 0x60d6
andl $0x1, 0x49d9(%rip)      # 0xd354
callq 0x848c
xorl %edi, %edi
callq 0x23b0
movq (%rbx), %rsi
leaq 0x275e(%rip), %rdi      # 0xb0ef
jmp 0x8619