// File: qian-jiahong[P]uvncrep017-ws/repeater.cpp
// Function: startListeningOnPort(_listenPortInfo*)
static void startListeningOnPort(listenPortInfo * pInfo)
{
    int yes = 1;
    struct sockaddr_in name;
    
    pInfo->socket = socket(PF_INET, SOCK_STREAM, 0);

    if (pInfo->socket < 0)
        fatal("startListeningOnPort(): socket() failed, errno=%d (%s)\n", errno, strerror(errno));
    else
        debug(LEVEL_3, "startListeningOnPort(): socket() initialized\n");

    if (setsockopt(pInfo->socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
        fatal("startListeningOnPort(): setsockopt() failed, errno=%d (%s)\n", errno, strerror(errno));
    else
        debug(LEVEL_3, "startListeningOnPort(): setsockopt() success\n");

    name.sin_family = AF_INET;

    name.sin_port = htons(pInfo->port);

    name.sin_addr.s_addr = inet_addr(ownIpAddress);

    if (bind(pInfo->socket, (struct sockaddr *) &name, sizeof(name)) < 0)
        fatal("startListeningOnPort(): bind() to (ip: %s, port: %d) failed, errno=%d (%s)\n", 
            ownIpAddress, pInfo -> port, errno, strerror(errno));
    else
        debug(LEVEL_3, "startListeningOnPort(): bind() to (ip: %s, port: %d) succeeded\n", ownIpAddress, pInfo->port);

    if (listen(pInfo->socket, LISTEN_BACKLOG) < 0)
        fatal("startListeningOnPort(): listen() failed, errno=%d (%s)\n", errno, strerror(errno));
    else
        debug(LEVEL_3, "startListeningOnPort(): listen() succeeded\n");

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, -0xc(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2070
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x3dd1
callq 0x2030
movl (%rax), %eax
movl %eax, -0x20(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movl -0x20(%rbp), %esi
movq %rax, %rdx
leaq 0x6d9b(%rip), %rdi      # 0xab63
movb $0x0, %al
callq 0x2610
jmp 0x3de4
movl $0x3, %edi
leaq 0x6dbe(%rip), %rsi      # 0xab9b
movb $0x0, %al
callq 0x2480
movq -0x8(%rbp), %rax
movl (%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0xc(%rbp), %rcx
movl $0x4, %r8d
callq 0x2340
cmpl $-0x1, %eax
jne 0x3e34
callq 0x2030
movl (%rax), %eax
movl %eax, -0x24(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movl -0x24(%rbp), %esi
movq %rax, %rdx
leaq 0x6d9e(%rip), %rdi      # 0xabc9
movb $0x0, %al
callq 0x2610
jmp 0x3e47
movl $0x3, %edi
leaq 0x6dc5(%rip), %rsi      # 0xac05
movb $0x0, %al
callq 0x2480
movw $0x2, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movzwl %ax, %edi
callq 0x2040
movw %ax, -0x1a(%rbp)
leaq 0xb499(%rip), %rdi      # 0xf300
callq 0x2220
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %edi
leaq -0x1c(%rbp), %rsi
movl $0x10, %edx
callq 0x2060
cmpl $0x0, %eax
jge 0x3ec8
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
callq 0x2030
movl (%rax), %eax
movl %eax, -0x28(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movl -0x2c(%rbp), %edx
movl -0x28(%rbp), %ecx
movq %rax, %r8
leaq 0x6d7b(%rip), %rdi      # 0xac33
leaq 0xb441(%rip), %rsi      # 0xf300
movb $0x0, %al
callq 0x2610
jmp 0x3ee9
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x3, %edi
leaq 0x6da4(%rip), %rsi      # 0xac7f
leaq 0xb41e(%rip), %rdx      # 0xf300
movb $0x0, %al
callq 0x2480
movq -0x8(%rbp), %rax
movl (%rax), %edi
movl $0x5, %esi
callq 0x2230
cmpl $0x0, %eax
jge 0x3f2a
callq 0x2030
movl (%rax), %eax
movl %eax, -0x30(%rbp)
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movl -0x30(%rbp), %esi
movq %rax, %rdx
leaq 0x6d9e(%rip), %rdi      # 0xacbf
movb $0x0, %al
callq 0x2610
jmp 0x3f3d
movl $0x3, %edi
leaq 0x6dc1(%rip), %rsi      # 0xacf7
movb $0x0, %al
callq 0x2480
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)