// File: /workspace/github_c_makefile_star2_O2/MAGI003769[P]EE450-Socket-Project/aws.c
// Function: setupTCP
int setupTCP(char* port) {
    int rv;
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    struct sigaction sa;
    int yes = 1;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    if((rv = getaddrinfo(IPADDRESS, port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
    }

    for(p = servinfo; p != NULL; p = p->ai_next) {
        if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }
        if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
            perror("setsockopt");
            exit(1);
        }
        if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }
        break;
    }

    freeaddrinfo(servinfo);

    if(p == NULL) {
        fprintf(stderr, "server: failed to bind\n");
        exit(1);
    }

    if(listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }
    
    sa.sa_handler = sigchld_handler; // reap all dead processes sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1); 
    }

    return sockfd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, %rsi
pushq $0x1
popq %rbx
movl %ebx, -0x2c(%rbp)
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, 0x20(%rdx)
movl %ebx, 0x8(%rdx)
movl %ebx, (%rdx)
leaq 0xbd9(%rip), %rdi       # 0x2004
leaq -0x38(%rbp), %r14
movq %r14, %rcx
callq 0x1200
testl %eax, %eax
jne 0x151d
leaq 0xbd9(%rip), %r12       # 0x201f
leaq -0x2c(%rbp), %r13
pushq $0x4
popq %r15
movq (%r14), %r14
testq %r14, %r14
je 0x14ba
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x1230
movl %eax, %ebx
movq %r12, %rdi
cmpl $-0x1, %eax
je 0x14af
movl %ebx, %edi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %r13, %rcx
movl %r15d, %r8d
callq 0x1090
cmpl $-0x1, %eax
je 0x1544
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %ebx, %edi
callq 0x1180
cmpl $-0x1, %eax
jne 0x14ba
movl %ebx, %edi
callq 0x1100
leaq 0xb8a(%rip), %rdi       # 0x2039
callq 0x11a0
addq $0x28, %r14
jmp 0x144e
movq -0x38(%rbp), %rdi
callq 0x1220
testq %r14, %r14
je 0x154d
pushq $0xa
popq %rsi
movl %ebx, %edi
callq 0x1150
cmpl $-0x1, %eax
je 0x156b
leaq -0x1aa(%rip), %rax      # 0x133c
leaq -0x108(%rbp), %rsi
movq %rax, (%rsi)
movl $0x10000000, 0x88(%rsi) # imm = 0x10000000
pushq $0x11
popq %rdi
xorl %edx, %edx
callq 0x1080
cmpl $-0x1, %eax
je 0x1574
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2ad4(%rip), %rcx      # 0x3ff8
movq (%rcx), %r14
movl %eax, %edi
callq 0x10d0
leaq 0xad9(%rip), %rsi       # 0x200e
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x1509
leaq 0xae3(%rip), %rdi       # 0x202e
jmp 0x157b
movq 0x2aa4(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xae8(%rip), %rdi       # 0x2046
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
jmp 0x1580
leaq 0xaec(%rip), %rdi       # 0x205e
jmp 0x157b
leaq 0xaea(%rip), %rdi       # 0x2065
callq 0x11a0
pushq $0x1
popq %rdi
callq 0x11e0