// File: /workspace/github_c_makefile_star2_O1/hasindu2008[P]f5p/socket.c
// Function: TCP_server_init
int TCP_server_init(int PORT) {
    //initialize variables
    int listenfd, ret;
    struct sockaddr_in server;

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(PORT);

    //open socket bind and listen
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    errorCheck(listenfd, "Cannot create socket");

    /****sophisticated way of binding*/

    //This lets a port be used as soon as the using program ends. Otherwise you will get "Cannot bind : Address is used"
    //for few minutes after the program that previously used the port exists
    int enable = 1;
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) <
        0 && verbose_socklibc>=1) {
        WARNING("%s", "setsockopt(SO_REUSEADDR) failed");
    }

    //try binding
    ret = -1;
    while (ret == -1) {
        ret = bind(listenfd, (struct sockaddr*)&server, sizeof(server));
        if (ret == -1) {
            
			if(verbose_socklibc>=1){
				WARNING(
                "%s",
                "Cannot bind to address as it is already used. Trying again.");
			}
            sleep(1);
        }
    }

    /*** end of the sophisticated way*/

    /*following (commented) is the simplest way of doing it. But You will get "Cannot bind : Address is used"
    for few minutes after the program that used the port exited */
    //ret=bind(listenfd,(struct sockaddr *)&server, sizeof(server));

    errorCheck(ret, "Cannot bind");
    if(verbose_socklibc>=4){
		INFO("Binding successfull to port %d.", PORT);
	}

    //now listen
    ret = listen(listenfd, 5);
    errorCheck(ret, "Cannot listen");
	if(verbose_socklibc>=4){
		INFO("Listening on port %d.", PORT);
	}

    return listenfd;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x20, %rsp
movl %edi, %ebx
movw $0x2, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movl %ebx, %eax
rolw $0x8, %ax
movw %ax, 0xe(%rsp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1260
movl %eax, %ebp
leaq 0x147c(%rip), %rsi      # 0x30a1
movl %eax, %edi
callq 0x1d45
leaq 0x1c(%rsp), %rcx
movl $0x1, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x1080
testl %eax, %eax
jns 0x1c5f
cmpl $0x0, 0x44ef(%rip)      # 0x6148
jg 0x1cef
leaq 0xc(%rsp), %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x1180
cmpl $-0x1, %eax
je 0x1cba
leaq 0x14c3(%rip), %rsi      # 0x313f
movl %eax, %edi
callq 0x1d45
cmpl $0x4, 0x44be(%rip)      # 0x6148
jge 0x1cf9
movl %ebp, %edi
movl $0x5, %esi
callq 0x1170
leaq 0x14d8(%rip), %rsi      # 0x3177
movl %eax, %edi
callq 0x1d45
cmpl $0x4, 0x449b(%rip)      # 0x6148
jge 0x1d1f
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq
leaq 0xc(%rsp), %r14
cmpl $0x0, 0x4482(%rip)      # 0x6148
jg 0x1ce8
movl $0x1, %edi
callq 0x1240
movl %ebp, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x1180
cmpl $-0x1, %eax
je 0x1cbf
jmp 0x1c75
callq 0x139a
jmp 0x1cc8
callq 0x136e
jmp 0x1c5f
movq 0x42f8(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1441(%rip), %rsi      # 0x314b
leaq 0x13c2(%rip), %rdx      # 0x30d3
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x1c8c
movq 0x42d2(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1455(%rip), %rsi      # 0x3185
leaq 0x139c(%rip), %rdx      # 0x30d3
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x1caf