// File: /workspace/github_c_makefile_star2_O1/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 0x7ab1
leaq -0x20af(%rip), %rsi     # 0x635a
movl $0x1, %edi
callq 0x2210
movl $0x1, %esi
movl $0xd, %edi
callq 0x2210
movl $0x1, %edi
callq 0x7fd7
testl %eax, %eax
jne 0x8660
cmpl $0x0, 0x2600d(%rip)     # 0x2e448
jne 0x8453
movq (%rbx), %rsi
leaq 0x2a0c(%rip), %rdi      # 0xae53
xorl %eax, %eax
callq 0x2a00
jmp 0x8660
movl 0x4f6f(%rip), %eax      # 0xd3c8
testb $0x8, %al
je 0x8469
andl $-0x51, %eax
orl $0x10, %eax
movl %eax, 0x4f5f(%rip)      # 0xd3c8
movl 0x4f59(%rip), %eax      # 0xd3c8
movl %eax, %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
movl %ecx, 0x4f4b(%rip)      # 0xd3c8
andl $0x50, %eax
cmpl $0x10, %eax
jne 0x84cd
movq 0x4b4c(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
callq 0x2110
movq 0x4b35(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2110
movq 0x4b4e(%rip), %rax      # 0xcff8
movq (%rax), %rdi
callq 0x2110
xorl %edi, %edi
callq 0x21b0
movl $0x1, %edi
callq 0x21b0
movl $0x2, %edi
callq 0x21b0
movb 0x4ef5(%rip), %al       # 0xd3c8
shrb $0x3, %al
movq 0x4efb(%rip), %rdi      # 0xd3d8
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8528
callq 0x2090
movq 0x4ee2(%rip), %rdi      # 0xd3d8
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x2330
testl %eax, %eax
jns 0x852d
movq (%rbx), %rsi
movq 0x4ec3(%rip), %rdx      # 0xd3d8
leaq 0x2987(%rip), %rdi      # 0xaea3
xorl %eax, %eax
callq 0x2a00
jmp 0x8660
xorl %r14d, %r14d
jmp 0x853e
leaq 0x2995(%rip), %rsi      # 0xaec9
movl %eax, %edi
callq 0x22e0
movq %rax, %r14
movq 0x4e8b(%rip), %rdi      # 0xd3d0
testq %rdi, %rdi
je 0x8578
callq 0x20b0
cmpl $-0x1, %eax
jne 0x856c
movq (%rbx), %rsi
movq 0x4e72(%rip), %rdx      # 0xd3d0
leaq 0x2966(%rip), %rdi      # 0xaecb
xorl %eax, %eax
callq 0x2a00
leaq 0x2978(%rip), %rdi      # 0xaeeb
callq 0x2140
cmpl $0x0, 0x4e39(%rip)      # 0xd3b8
jne 0x858d
movl 0x4e29(%rip), %eax      # 0xd3b0
movl %eax, 0x4e2b(%rip)      # 0xd3b8
movslq 0x4e24(%rip), %rax      # 0xd3b8
testq %rax, %rax
je 0x85c9
movq %rsp, %rsi
movq %rax, 0x8(%rsi)
movq %rax, (%rsi)
movl $0x7, %edi
callq 0x22d0
cmpl $-0x1, %eax
jne 0x85c9
movq (%rbx), %rsi
movl 0x4dfd(%rip), %edx      # 0xd3b8
leaq 0x292b(%rip), %rdi      # 0xaeed
xorl %eax, %eax
callq 0x2b35
movl 0x4df1(%rip), %eax      # 0xd3c0
testl %eax, %eax
je 0x860f
shll $0x14, %eax
cltd
idivl 0x4dc3(%rip)            # 0xd3a0
cltq
movq %rsp, %rsi
movq %rax, 0x8(%rsi)
movq %rax, (%rsi)
movl $0x9, %edi
callq 0x22d0
cmpl $-0x1, %eax
jne 0x860f
movq (%rbx), %rsi
movl 0x4dbf(%rip), %edx      # 0xd3c0
leaq 0x290f(%rip), %rdi      # 0xaf17
xorl %eax, %eax
callq 0x2b35
movl 0x4d83(%rip), %edi      # 0xd398
testl %edi, %edi
je 0x8635
callq 0x2310
cmpl $-0x1, %eax
jne 0x8635
movq (%rbx), %rsi
movl 0x4d6c(%rip), %edx      # 0xd398
leaq 0x2912(%rip), %rdi      # 0xaf45
jmp 0x8659
movl 0x4d55(%rip), %edi      # 0xd390
testl %edi, %edi
je 0x866a
callq 0x23d0
cmpl $-0x1, %eax
jne 0x866a
movq (%rbx), %rsi
movl 0x4d3e(%rip), %edx      # 0xd390
leaq 0x290c(%rip), %rdi      # 0xaf65
xorl %eax, %eax
callq 0x2a00
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 0x4d25(%rip), %r8       # 0xd3b0
cmpq %r8, %rdx
jae 0x86aa
movq (%rbx), %rsi
movl 0x4d0f(%rip), %ecx      # 0xd3a8
leaq 0x28e5(%rip), %rdi      # 0xaf85
movl %r8d, %r9d
xorl %eax, %eax
callq 0x2b35
testb $0x8, 0x4d17(%rip)      # 0xd3c8
je 0x878d
xorl %ebp, %ebp
cmpl $0x0, 0x4ce0(%rip)      # 0xd3a0
jle 0x8700
leaq 0x2950(%rip), %r15      # 0xb019
xorl %ebp, %ebp
callq 0x2400
testl %eax, %eax
js 0x87c4
je 0x8700
testq %r14, %r14
je 0x86f6
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x2230
movq %r14, %rdi
callq 0x22b0
incl %ebp
cmpl 0x4ca2(%rip), %ebp      # 0xd3a0
jl 0x86cb
testq %r14, %r14
je 0x870d
movq %r14, %rdi
callq 0x2110
movq 0x4cc4(%rip), %rdi      # 0xd3d8
callq 0x2050
cmpl 0x4c81(%rip), %ebp      # 0xd3a0
je 0x87bd
testb $0x10, 0x4c9c(%rip)     # 0xd3c8
jne 0x877d
movq 0x48a3(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
callq 0x2110
movq 0x488c(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x2110
movq 0x48a5(%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, 0x4c4b(%rip)     # 0xd3c8
callq 0x2100
movl %eax, 0x25db0(%rip)     # 0x2e538
callq 0x21c0
testb $0x1, 0x4bc0(%rip)      # 0xd354
je 0x87b8
xorl %edi, %edi
callq 0x60b9
movl $0x1, %edi
callq 0x60b9
movl $0x2, %edi
callq 0x60b9
andl $0x1, 0x4b9c(%rip)      # 0xd354
callq 0x82ba
xorl %edi, %edi
callq 0x23b0
movq (%rbx), %rsi
leaq 0x2831(%rip), %rdi      # 0xafff
jmp 0x8447