// File: /workspace/github_c_makefile_star2_O0/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
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movl $0x1, -0xf4(%rbp)
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x10d0
movl $0x0, -0x44(%rbp)
movl $0x1, -0x40(%rbp)
movl $0x1, -0x48(%rbp)
movq -0x10(%rbp), %rsi
leaq 0x1b3d(%rip), %rdi      # 0x3006
leaq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0x11d0
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
je 0x151b
movq 0x3b13(%rip), %rax      # 0x4ff8
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movl -0x14(%rbp), %edi
callq 0x10c0
movq -0x100(%rbp), %rdi
movq %rax, %rdx
leaq 0x1b08(%rip), %rsi      # 0x3010
movb $0x0, %al
callq 0x1120
movl $0x1, -0x4(%rbp)
jmp 0x1682
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x15dd
movq -0x58(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x58(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x1200
movl %eax, -0x18(%rbp)
cmpl $-0x1, %eax
jne 0x155e
leaq 0x1aca(%rip), %rdi      # 0x3021
callq 0x1180
jmp 0x15cc
movl -0x18(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0xf4(%rbp), %rcx
movl $0x4, %r8d
callq 0x1080
cmpl $-0x1, %eax
jne 0x1598
leaq 0x1aa7(%rip), %rdi      # 0x3030
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movl -0x18(%rbp), %edi
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x58(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x1160
cmpl $-0x1, %eax
jne 0x15ca
movl -0x18(%rbp), %edi
callq 0x10f0
leaq 0x1a78(%rip), %rdi      # 0x303b
callq 0x1180
jmp 0x15cc
jmp 0x15dd
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
jmp 0x1523
movq -0x50(%rbp), %rdi
callq 0x11f0
cmpq $0x0, -0x58(%rbp)
jne 0x160f
movq 0x3a04(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1a4a(%rip), %rsi      # 0x3048
movb $0x0, %al
callq 0x1120
movl $0x1, %edi
callq 0x11c0
movl -0x18(%rbp), %edi
movl $0xa, %esi
callq 0x1140
cmpl $-0x1, %eax
jne 0x1637
leaq 0x1a38(%rip), %rdi      # 0x3060
callq 0x1180
movl $0x1, %edi
callq 0x11c0
leaq -0x32e(%rip), %rax      # 0x1310
movq %rax, -0xf0(%rbp)
movl $0x10000000, -0x68(%rbp) # imm = 0x10000000
movl $0x11, %edi
leaq -0xf0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1070
cmpl $-0x1, %eax
jne 0x167c
leaq 0x19fa(%rip), %rdi      # 0x3067
callq 0x1180
movl $0x1, %edi
callq 0x11c0
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nop