// File: /workspace/github_c_makefile_star2_O3/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 0x1485(%rip), %rsi      # 0x30a1
movl %eax, %edi
callq 0x1d3c
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 0x1c56
cmpl $0x0, 0x44f8(%rip)      # 0x6148
jg 0x1ce6
leaq 0xc(%rsp), %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x1180
cmpl $-0x1, %eax
je 0x1cb1
leaq 0x14cc(%rip), %rsi      # 0x313f
movl %eax, %edi
callq 0x1d3c
cmpl $0x4, 0x44c7(%rip)      # 0x6148
jge 0x1cf0
movl %ebp, %edi
movl $0x5, %esi
callq 0x1170
leaq 0x14e1(%rip), %rsi      # 0x3177
movl %eax, %edi
callq 0x1d3c
cmpl $0x4, 0x44a4(%rip)      # 0x6148
jge 0x1d16
movl %ebp, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq
leaq 0xc(%rsp), %r14
cmpl $0x0, 0x448b(%rip)      # 0x6148
jg 0x1cdf
movl $0x1, %edi
callq 0x1240
movl %ebp, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x1180
cmpl $-0x1, %eax
je 0x1cb6
jmp 0x1c6c
callq 0x136f
jmp 0x1cbf
callq 0x1343
jmp 0x1c56
movq 0x4301(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x144a(%rip), %rsi      # 0x314b
leaq 0x13cb(%rip), %rdx      # 0x30d3
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x1c83
movq 0x42db(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x145e(%rip), %rsi      # 0x3185
leaq 0x13a5(%rip), %rdx      # 0x30d3
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1130
jmp 0x1ca6