// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0xdc(%rbp)
movl $0x0, -0xe8(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1584
callq 0x1510
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1823
jmp 0x1586
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xb45(%rip), %rdx       # 0x20d9
callq 0x1130
movb %al, -0xe1(%rbp)
movsbl %al, %eax
cmpl $-0x1, %eax
je 0x15fe
movsbl -0xe1(%rbp), %eax
movl %eax, -0xec(%rbp)
subl $0x6c, %eax
je 0x15df
jmp 0x15bb
movl -0xec(%rbp), %eax
subl $0x70, %eax
jne 0x15eb
jmp 0x15c8
movq 0x2a11(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x1150
movl %eax, -0xe0(%rbp)
jmp 0x15fc
movl $0x1, -0xe8(%rbp)
jmp 0x15fc
callq 0x1510
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1823
jmp 0x1586
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
jne 0x162d
leaq 0xabf(%rip), %rdi       # 0x20dd
callq 0x1120
movl $0x1, %edi
callq 0x1160
leaq 0xac4(%rip), %rdi       # 0x20f8
movb $0x0, %al
callq 0x10a0
movl -0x14(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0xdc(%rbp), %rcx
movl $0x4, %r8d
callq 0x1060
cmpl $-0x1, %eax
jne 0x1675
leaq 0xab3(%rip), %rdi       # 0x2119
callq 0x1120
movl $0x1, %edi
callq 0x1160
leaq 0xabc(%rip), %rdi       # 0x2138
movb $0x0, %al
callq 0x10a0
movw $0x2, -0x28(%rbp)
movl -0xe0(%rbp), %eax
movzwl %ax, %edi
callq 0x1080
movw %ax, -0x26(%rbp)
movl $0x0, -0x24(%rbp)
leaq -0x28(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x10b0
movl -0x14(%rbp), %edi
leaq -0x28(%rbp), %rsi
movl $0x10, %edx
callq 0x1110
cmpl $-0x1, %eax
jne 0x16e2
leaq 0xa81(%rip), %rdi       # 0x2154
callq 0x1120
movl $0x1, %edi
callq 0x1160
leaq 0xa7f(%rip), %rdi       # 0x2168
movb $0x0, %al
callq 0x10a0
movl -0x14(%rbp), %edi
movl $0xa, %esi
callq 0x10f0
cmpl $-0x1, %eax
jne 0x1718
leaq 0xa77(%rip), %rdi       # 0x2180
callq 0x1120
movl $0x1, %edi
callq 0x1160
leaq -0x46f(%rip), %rax      # 0x12b0
movq %rax, -0xd8(%rbp)
leaq -0xd8(%rbp), %rdi
addq $0x8, %rdi
callq 0x10d0
movl $0x10000000, -0x50(%rbp) # imm = 0x10000000
movl $0x11, %edi
leaq -0xd8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1050
cmpl $-0x1, %eax
jne 0x176d
leaq 0xa38(%rip), %rdi       # 0x2196
callq 0x1120
movl $0x1, %edi
callq 0x1160
leaq 0xa3b(%rip), %rdi       # 0x21af
movb $0x0, %al
callq 0x10a0
jmp 0x177d
movl $0x10, -0x3c(%rbp)
movl -0x14(%rbp), %edi
leaq -0x38(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
callq 0x1140
movl %eax, -0x18(%rbp)
cmpl $-0x1, %eax
jne 0x17aa
leaq 0xa29(%rip), %rdi       # 0x21cc
callq 0x1120
jmp 0x177d
movl -0x34(%rbp), %edi
callq 0x1070
movq %rax, -0xf8(%rbp)
movzwl -0x36(%rbp), %edi
callq 0x1100
movq -0xf8(%rbp), %rsi
movzwl %ax, %edx
leaq 0xa0f(%rip), %rdi       # 0x21e2
movb $0x0, %al
callq 0x10a0
callq 0x1190
cmpl $0x0, %eax
jne 0x1816
movl -0x14(%rbp), %edi
callq 0x10c0
cmpl $0x0, -0xe8(%rbp)
je 0x17ff
movl -0x18(%rbp), %edi
callq 0x1360
jmp 0x1807
movl -0x18(%rbp), %edi
callq 0x12e0
movl -0x18(%rbp), %edi
callq 0x10c0
xorl %edi, %edi
callq 0x1160
movl -0x18(%rbp), %edi
callq 0x10c0
jmp 0x177d
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq