// File: /workspace/github_c_makefile_star2_O3/dropletzhu[P]msuite/tcpserver.c
// Function: main
int main(int argc, char *argv[ ])
{
    int sockfd, new_fd;
    struct sockaddr_in my_addr;
    struct sockaddr_in their_addr;
    int sin_size;
    struct sigaction sa;
    int yes = 1;
	int port;
	char ch;
	int long_lived = 0;

	if (argc < 2) {
		usage();
		return -1;
	}

	while ((ch = getopt (argc, argv, "p:l")) != -1) {
        switch (ch) {
        case 'p':
			port = atoi(optarg);
            break;
		case 'l':
			long_lived = 1;
			break;
		default:
			usage();
			return -1;
		}
	}

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Server-socket() error lol!");
        exit(1);
    } else {
        printf("Server-socket() sockfd is OK...\n");
    }

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
        perror("Server-setsockopt() error lol!");
        exit(1);
    } else {
        printf("Server-setsockopt is OK...\n");
    }

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(port);
    my_addr.sin_addr.s_addr = INADDR_ANY;
    memset(&(my_addr.sin_zero), '\0', 8);

    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
        perror("Server-bind() error");
        exit(1);
    } else {
        printf("Server-bind() is OK...\n");
    }

    if(listen(sockfd, BACKLOG) == -1) {
        perror("Server-listen() error");
        exit(1);
    }

    sa.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if(sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("Server-sigaction() error");
        exit(1);
    } else {
        printf("Server-sigaction() is OK...\n");
    }

    /* accept() loop */
    while(1) {
        sin_size = sizeof(struct sockaddr_in);
        if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,(socklen_t*)&sin_size)) == -1) {
            perror("Server-accept() error");
            continue;
        }

        printf("Server: Got connection from %s-%d\n", inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port));

        /* this is the child process */
        if(!fork()) {
            close(sockfd);
			/* create long lived connection or echo connection */
			if (long_lived) {
				long_lived_server(new_fd);
			} else {
				echo_server(new_fd);
			}
            close(new_fd);
            exit(0);
        } else {
            close(new_fd);
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl $0x1, 0x8(%rsp)
cmpl $0x2, %edi
jge 0x14f2
callq 0x1492
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
movl %edi, %r14d
leaq 0xb43(%rip), %rdx       # 0x2042
callq 0x1120
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x16ed
xorl %r13d, %r13d
leaq 0xb26(%rip), %r15       # 0x2042
sarl $0x18, %eax
movl $0x1, %r12d
cmpl $0x6c, %eax
je 0x1543
cmpl $0x70, %eax
jne 0x14d6
movq 0x2aaa(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x1140
movl %eax, %ebp
movl %r13d, %r12d
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1120
shll $0x18, %eax
movl %r12d, %r13d
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x151c
testl %r12d, %r12d
sete 0x7(%rsp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1190
cmpl $-0x1, %eax
jne 0x1581
callq 0x1204
movl %eax, %ebx
leaq 0xc06(%rip), %rdi       # 0x2190
callq 0x1050
leaq 0x8(%rsp), %rcx
movl %ebx, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x1070
cmpl $-0x1, %eax
jne 0x15b5
callq 0x11ef
leaq 0xbf4(%rip), %rdi       # 0x21b0
callq 0x1050
leaq 0x20(%rsp), %rsi
rolw $0x8, %bp
movw $0x2, (%rsi)
movw %bp, 0x2(%rsi)
movl $0x0, 0x4(%rsi)
movq $0x0, 0x8(%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x1100
cmpl $-0x1, %eax
jne 0x15f8
callq 0x11da
leaq 0xbcc(%rip), %rdi       # 0x21cb
callq 0x1050
movl %ebx, %edi
movl $0xa, %esi
callq 0x10f0
cmpl $-0x1, %eax
jne 0x161a
callq 0x11c5
leaq -0x315(%rip), %rax      # 0x130c
leaq 0x38(%rsp), %r14
movq %rax, -0x8(%r14)
movq %r14, %rdi
callq 0x10d0
movl $0x10000000, 0x80(%r14) # imm = 0x10000000
leaq 0x30(%rsp), %rsi
movl $0x11, %edi
xorl %edx, %edx
callq 0x1060
cmpl $-0x1, %eax
jne 0x1658
callq 0x11b0
leaq 0xb83(%rip), %rdi       # 0x21e2
callq 0x1050
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r15
leaq 0xa4e(%rip), %r12       # 0x20c3
leaq 0xa5d(%rip), %r13       # 0x20d9
movl $0x10, 0xc(%rsp)
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1130
cmpl $-0x1, %eax
je 0x16cc
movl %eax, %ebp
movl 0x14(%rsp), %edi
callq 0x1080
movzwl 0x12(%rsp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
movq %r13, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x10a0
callq 0x1180
testl %eax, %eax
je 0x16d6
movl %ebp, %edi
callq 0x10c0
jmp 0x167c
movq %r12, %rdi
callq 0x1110
jmp 0x167c
movl %ebx, %edi
callq 0x10c0
cmpb $0x0, 0x7(%rsp)
je 0x16f7
movl %ebp, %edi
callq 0x131a
jmp 0x16fe
movb $0x1, 0x7(%rsp)
jmp 0x1566
movl %ebp, %edi
callq 0x1385
movl %ebp, %edi
callq 0x10c0
xorl %edi, %edi
callq 0x1150