// File: /workspace/github_c_makefile_star2_O2/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 0x7565
leaq -0x1ea1(%rip), %rsi     # 0x5f54
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x2210
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x2210
movl %ebp, %edi
callq 0x7a18
testl %eax, %eax
jne 0x802e
cmpl $0x0, 0x27628(%rip)     # 0x2f448
jne 0x7e38
movq (%rbx), %rsi
leaq 0x3027(%rip), %rdi      # 0xae53
xorl %eax, %eax
callq 0x29aa
jmp 0x802e
movl 0x658a(%rip), %eax      # 0xe3c8
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, 0x6570(%rip)      # 0xe3c8
andl $0x50, %ecx
cmpl $0x10, %ecx
jne 0x7eaa
movq 0x6171(%rip), %rax      # 0xdfd8
movq (%rax), %rdi
callq 0x2110
movq 0x615a(%rip), %rax      # 0xdfd0
movq (%rax), %rdi
callq 0x2110
movq 0x6173(%rip), %rax      # 0xdff8
movq (%rax), %rdi
callq 0x2110
xorl %edi, %edi
callq 0x21b0
pushq $0x1
popq %rdi
callq 0x21b0
pushq $0x2
popq %rdi
callq 0x21b0
movl 0x651e(%rip), %eax      # 0xe3c8
shrb $0x3, %al
movq 0x6524(%rip), %rdi      # 0xe3d8
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7eff
callq 0x2090
movq 0x650b(%rip), %rdi      # 0xe3d8
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x2330
testl %eax, %eax
jns 0x7f04
movq (%rbx), %rsi
movq 0x64ec(%rip), %rdx      # 0xe3d8
leaq 0x2fb0(%rip), %rdi      # 0xaea3
xorl %eax, %eax
callq 0x29aa
jmp 0x802e
xorl %r14d, %r14d
jmp 0x7f15
leaq 0x2fbe(%rip), %rsi      # 0xaec9
movl %eax, %edi
callq 0x22e0
movq %rax, %r14
movq 0x64b4(%rip), %rdi      # 0xe3d0
testq %rdi, %rdi
je 0x7f4f
callq 0x20b0
cmpl $-0x1, %eax
jne 0x7f43
movq (%rbx), %rsi
movq 0x649b(%rip), %rdx      # 0xe3d0
leaq 0x2f8f(%rip), %rdi      # 0xaecb
xorl %eax, %eax
callq 0x29aa
leaq 0x2fa1(%rip), %rdi      # 0xaeeb
callq 0x2140
movl 0x6463(%rip), %eax      # 0xe3b8
testl %eax, %eax
jne 0x7f69
movl 0x6451(%rip), %eax      # 0xe3b0
movl %eax, 0x6453(%rip)      # 0xe3b8
testl %eax, %eax
je 0x7f99
cltq
movq %rsp, %rsi
movq %rax, 0x8(%rsi)
movq %rax, (%rsi)
pushq $0x7
popq %rdi
callq 0x22d0
cmpl $-0x1, %eax
jne 0x7f99
movq (%rbx), %rsi
movl 0x642d(%rip), %edx      # 0xe3b8
leaq 0x2f5b(%rip), %rdi      # 0xaeed
xorl %eax, %eax
callq 0x2adf
movl 0x6421(%rip), %eax      # 0xe3c0
testl %eax, %eax
je 0x7fdd
shll $0x14, %eax
cltd
idivl 0x63f3(%rip)            # 0xe3a0
cltq
movq %rsp, %rsi
movq %rax, 0x8(%rsi)
movq %rax, (%rsi)
pushq $0x9
popq %rdi
callq 0x22d0
cmpl $-0x1, %eax
jne 0x7fdd
movq (%rbx), %rsi
movl 0x63f1(%rip), %edx      # 0xe3c0
leaq 0x2f41(%rip), %rdi      # 0xaf17
xorl %eax, %eax
callq 0x2adf
movl 0x63b5(%rip), %edi      # 0xe398
testl %edi, %edi
je 0x8003
callq 0x2310
cmpl $-0x1, %eax
jne 0x8003
movq (%rbx), %rsi
movl 0x639e(%rip), %edx      # 0xe398
leaq 0x2f44(%rip), %rdi      # 0xaf45
jmp 0x8027
movl 0x6387(%rip), %edi      # 0xe390
testl %edi, %edi
je 0x8036
callq 0x23d0
cmpl $-0x1, %eax
jne 0x8036
movq (%rbx), %rsi
movl 0x6370(%rip), %edx      # 0xe390
leaq 0x2f3e(%rip), %rdi      # 0xaf65
xorl %eax, %eax
callq 0x29aa
pushq $0x1
popq %rdi
callq 0x23b0
xorps %xmm0, %xmm0
movq %rsp, %r15
movaps %xmm0, (%r15)
pushq $0x7
popq %rdi
movq %r15, %rsi
callq 0x23c0
movq (%r15), %rdx
movslq 0x635b(%rip), %r8       # 0xe3b0
cmpq %r8, %rdx
jae 0x8074
movq (%rbx), %rsi
movl 0x6345(%rip), %ecx      # 0xe3a8
leaq 0x2f1b(%rip), %rdi      # 0xaf85
movl %r8d, %r9d
xorl %eax, %eax
callq 0x2adf
testb $0x8, 0x634d(%rip)      # 0xe3c8
je 0x814a
leaq 0x2f91(%rip), %r15      # 0xb019
xorl %ebp, %ebp
cmpl 0x6310(%rip), %ebp      # 0xe3a0
jge 0x80c1
callq 0x2400
testl %eax, %eax
js 0x817d
je 0x80c1
testq %r14, %r14
je 0x80bd
movq %r14, %rdi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x2230
movq %r14, %rdi
callq 0x22b0
incl %ebp
jmp 0x808a
testq %r14, %r14
je 0x80ce
movq %r14, %rdi
callq 0x2110
movq 0x6303(%rip), %rdi      # 0xe3d8
callq 0x2050
cmpl %ebp, 0x62c0(%rip)      # 0xe3a0
je 0x8176
testb $0x10, 0x62db(%rip)     # 0xe3c8
jne 0x813a
movq 0x5ee2(%rip), %rax      # 0xdfd8
movq (%rax), %rdi
callq 0x2110
movq 0x5ecb(%rip), %rax      # 0xdfd0
movq (%rax), %rdi
callq 0x2110
movq 0x5ee4(%rip), %rax      # 0xdff8
movq (%rax), %rdi
callq 0x2110
xorl %edi, %edi
callq 0x21b0
pushq $0x1
popq %rdi
callq 0x21b0
pushq $0x2
popq %rdi
callq 0x21b0
orb $0x10, 0x628e(%rip)     # 0xe3c8
callq 0x2100
movl %eax, 0x273f3(%rip)     # 0x2f538
callq 0x21c0
testb $0x1, 0x6203(%rip)      # 0xe354
je 0x8171
xorl %edi, %edi
callq 0x5cec
pushq $0x1
popq %rdi
callq 0x5cec
pushq $0x2
popq %rdi
callq 0x5cec
andl $0x1, 0x61e3(%rip)      # 0xe354
callq 0x7d0d
xorl %edi, %edi
callq 0x23b0
movq (%rbx), %rsi
leaq 0x2e78(%rip), %rdi      # 0xafff
jmp 0x7e2c