// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x20, %rsp
movl %edi, -0x4(%rbp)
movw $0x2, -0x1c(%rbp)
xorl %edi, %edi
callq 0x1100
movl %eax, -0x18(%rbp)
movl -0x4(%rbp), %eax
movzwl %ax, %edi
callq 0x10d0
movw %ax, -0x1a(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1280
movl %eax, -0x8(%rbp)
movl -0x8(%rbp), %edi
leaq 0x2889(%rip), %rsi      # 0x40a1
callq 0x1970
movl $0x1, -0x20(%rbp)
movl -0x8(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x20(%rbp), %rcx
movl $0x4, %r8d
callq 0x1080
cmpl $0x0, %eax
jge 0x1874
cmpl $0x1, 0x590c(%rip)      # 0x7158
jl 0x1874
movq 0x57a3(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2857(%rip), %rsi      # 0x40b6
leaq 0x286d(%rip), %rdx      # 0x40d3
leaq 0x2876(%rip), %rcx      # 0x40e3
movb $0x0, %al
callq 0x1160
movl $0xffffffff, -0xc(%rbp) # imm = 0xFFFFFFFF
cmpl $-0x1, -0xc(%rbp)
jne 0x18d6
movl -0x8(%rbp), %edi
leaq -0x1c(%rbp), %rsi
movl $0x10, %edx
callq 0x11b0
movl %eax, -0xc(%rbp)
cmpl $-0x1, -0xc(%rbp)
jne 0x18d4
cmpl $0x1, 0x58b6(%rip)      # 0x7158
jl 0x18ca
movq 0x574d(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x2801(%rip), %rsi      # 0x40b6
leaq 0x2817(%rip), %rdx      # 0x40d3
leaq 0x2840(%rip), %rcx      # 0x4103
movb $0x0, %al
callq 0x1160
movl $0x1, %edi
callq 0x1260
jmp 0x187b
movl -0xc(%rbp), %edi
leaq 0x285f(%rip), %rsi      # 0x413f
callq 0x1970
cmpl $0x4, 0x586c(%rip)      # 0x7158
jl 0x1910
movq 0x5703(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movl -0x4(%rbp), %ecx
leaq 0x2849(%rip), %rsi      # 0x414b
leaq 0x27ca(%rip), %rdx      # 0x40d3
movb $0x0, %al
callq 0x1160
movl -0x8(%rbp), %edi
movl $0x5, %esi
callq 0x11a0
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
leaq 0x284d(%rip), %rsi      # 0x4177
callq 0x1970
cmpl $0x4, 0x5822(%rip)      # 0x7158
jl 0x195a
movq 0x56b9(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movl -0x4(%rbp), %ecx
leaq 0x2839(%rip), %rsi      # 0x4185
leaq 0x2780(%rip), %rdx      # 0x40d3
movb $0x0, %al
callq 0x1160
movl -0x8(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)