// File: /workspace/github_c_makefile_star2_O2/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 0x146c
callq 0x140e
pushq $-0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
movl %edi, %r14d
xorl %r12d, %r12d
leaq 0xbc6(%rip), %r15       # 0x2042
pushq $0x1
popq %r13
movq 0x2b59(%rip), %rbp      # 0x3fe0
movl %r12d, %ecx
movl %ecx, %r12d
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1120
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x14c4
sarl $0x18, %eax
movl %r13d, %ecx
cmpl $0x6c, %eax
je 0x148a
cmpl $0x70, %eax
jne 0x1452
movq (%rbp), %rdi
callq 0x1140
movl %eax, 0x4(%rsp)
jmp 0x1487
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1190
cmpl $-0x1, %eax
jne 0x14e2
leaq 0xb69(%rip), %rdi       # 0x2046
jmp 0x15bc
movl %eax, %ebx
leaq 0xca5(%rip), %rdi       # 0x2190
callq 0x1050
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
leaq 0x8(%rsp), %rcx
pushq $0x4
popq %r8
movl %ebx, %edi
callq 0x1070
cmpl $-0x1, %eax
jne 0x1517
leaq 0xb4f(%rip), %rdi       # 0x2061
jmp 0x15bc
leaq 0xc92(%rip), %rdi       # 0x21b0
callq 0x1050
leaq 0x20(%rsp), %rsi
movw $0x2, (%rsi)
movl 0x4(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
andl $0x0, 0x4(%rsi)
andq $0x0, 0x8(%rsi)
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x1100
cmpl $-0x1, %eax
jne 0x155a
leaq 0xb28(%rip), %rdi       # 0x2080
jmp 0x15bc
leaq 0xc6a(%rip), %rdi       # 0x21cb
callq 0x1050
pushq $0xa
popq %rsi
movl %ebx, %edi
callq 0x10f0
cmpl $-0x1, %eax
jne 0x157e
leaq 0xb18(%rip), %rdi       # 0x2094
jmp 0x15bc
leaq -0x2e9(%rip), %rax      # 0x129c
leaq 0x38(%rsp), %r14
movq %rax, -0x8(%r14)
movq %r14, %rdi
callq 0x10d0
movl $0x10000000, 0x80(%r14) # imm = 0x10000000
pushq $0x11
popq %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x1060
cmpl $-0x1, %eax
jne 0x15c9
leaq 0xaee(%rip), %rdi       # 0x20aa
callq 0x1110
pushq $0x1
popq %rdi
callq 0x1150
leaq 0xc12(%rip), %rdi       # 0x21e2
callq 0x1050
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r15
leaq 0xaf3(%rip), %r13       # 0x20d9
movl $0x10, 0xc(%rsp)
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1130
cmpl $-0x1, %eax
je 0x1636
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 0x1644
movl %ebp, %edi
callq 0x10c0
jmp 0x15e6
leaq 0xa86(%rip), %rdi       # 0x20c3
callq 0x1110
jmp 0x15e6
movl %ebx, %edi
callq 0x10c0
movl %ebp, %edi
testl %r12d, %r12d
jne 0x1659
callq 0x12aa
jmp 0x165e
callq 0x1315
movl %ebp, %edi
callq 0x10c0
xorl %edi, %edi
callq 0x1150