// File: qian-jiahong[P]uvncrep017-ws/repeater.cpp
// Function: nonBlockingAccept(int, sockaddr*, unsigned int*)
int nonBlockingAccept(int socket, struct sockaddr *sa, socklen_t *sockLen)
{
    long arg; 
    int socketToReturn;
    
    //First, set socket non-blocking 
    arg = fcntl(socket, F_GETFL, NULL);
    if (arg < 0) { 
        debug(LEVEL_2, "nonBlockingAccept(): error in fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
        return -1; 
    } 
    arg |= O_NONBLOCK; 
    if (fcntl(socket, F_SETFL, arg) < 0) { 
        debug(LEVEL_2, "nonBlockingAccept(): error in fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
        return -1; 
    }     
    
    //Accept connection 
    socketToReturn = accept(socket, sa, sockLen);

    //Set to blocking mode again... 
    if ((arg = fcntl(socket, F_GETFL, NULL)) < 0) { 
        debug(LEVEL_2, "nonBlockingAccept(): Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
        if (socketToReturn != -1)
            close(socketToReturn);
        return -1; 
    } 
    arg &= (~O_NONBLOCK); 
    if (fcntl(socket, F_SETFL, arg) < 0) { 
        debug(LEVEL_2, "nonBlockingAccept(): Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
        if (socketToReturn != -1)
            close(socketToReturn);
        return -1; 
    } 

    //At last, we are ready to return accept():ed socket ;-)
    return socketToReturn;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl -0x8(%rbp), %edi
movl $0x3, %esi
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x2130
cltq
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jge 0x3171
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movq %rax, %rdx
movl $0x2, %edi
leaq 0x7103(%rip), %rsi      # 0xa261
movb $0x0, %al
callq 0x2480
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3295
movq -0x20(%rbp), %rax
orq $0x800, %rax            # imm = 0x800
movq %rax, -0x20(%rbp)
movl -0x8(%rbp), %edi
movq -0x20(%rbp), %rdx
movl $0x4, %esi
movb $0x0, %al
callq 0x2130
cmpl $0x0, %eax
jge 0x31c5
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movq %rax, %rdx
movl $0x2, %edi
leaq 0x70e7(%rip), %rsi      # 0xa299
movb $0x0, %al
callq 0x2480
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3295
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x2090
movl %eax, -0x24(%rbp)
movl -0x8(%rbp), %edi
movl $0x3, %esi
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x2130
cltq
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jge 0x3230
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movq %rax, %rdx
movl $0x2, %edi
leaq 0x70bf(%rip), %rsi      # 0xa2d1
movb $0x0, %al
callq 0x2480
cmpl $-0x1, -0x24(%rbp)
je 0x3227
movl -0x24(%rbp), %edi
callq 0x2370
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3295
movq -0x20(%rbp), %rax
andq $-0x801, %rax           # imm = 0xF7FF
movq %rax, -0x20(%rbp)
movl -0x8(%rbp), %edi
movq -0x20(%rbp), %rdx
movl $0x4, %esi
movb $0x0, %al
callq 0x2130
cmpl $0x0, %eax
jge 0x328f
callq 0x2030
movl (%rax), %edi
callq 0x20b0
movq %rax, %rdx
movl $0x2, %edi
leaq 0x7095(%rip), %rsi      # 0xa306
movb $0x0, %al
callq 0x2480
cmpl $-0x1, -0x24(%rbp)
je 0x3286
movl -0x24(%rbp), %edi
callq 0x2370
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3295
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop