// File: /workspace/github_cpp_makefile_star2_O1/qian-jiahong[P]uvncrep017-ws/repeater.cpp
// Function: nonBlockingRead(int, char*, int, int)
static int nonBlockingRead(int sock, char *buf, int len, int timeOut)
{
    int n;
    int timeOutCtr;
    int numRead = 0;
    
    debug(LEVEL_3, "nonBlockingRead(): start\n");
    timeOutCtr=0;
    while ((len > 0) && (timeOutCtr < timeOut)) {
        n = recv(sock, buf, len, MSG_DONTWAIT);

        if (n > 0) {
            buf += n;
            len -= n;
            numRead += n;
        }
        else {
            if (n == -1) {
                //recv() returned -1 to indicate some error
                //Because we use non-blocking in recv(), we have to 
                //handle EAGAIN by incrementing timeout counter
                if (errno == EAGAIN) {
                    sleep(1);
                    timeOutCtr++;
                }
            }
            else {
                debug(LEVEL_2, "nonBlockingRead(): recv() returned error, errno= %d (%s)\n", errno, strerror(errno));
                return -1;  //return value of recv() was unknown
            }
        }
    }

    if (timeOutCtr < timeOut) {
        debug(LEVEL_3, "nonBlockingRead(): returning normally\n");
        return numRead;
    }
    else {
        //In case of timeout, return number of bytes received if > 0, 
        //otherwise return -2 to indicate timeout
        if (numRead > 0) {
            debug(LEVEL_3, "nonBlockingRead(): returning %d bytes\n", numRead);
            return numRead;
        }
        else {
            debug(LEVEL_3, "nonBlockingRead(): timeout error\n");
            return -2;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r14
movl %edi, %r13d
leaq 0x3a6f(%rip), %rsi      # 0x763a
xorl %ebx, %ebx
movl $0x3, %edi
xorl %eax, %eax
callq 0x355c
testl %ebp, %ebp
jle 0x3c61
xorl %ebx, %ebx
xorl %r15d, %r15d
movl %ebp, %edx
movl %r13d, %edi
movq %r14, %rsi
movl $0x40, %ecx
callq 0x2090
movq %rax, %r12
testl %r12d, %r12d
jle 0x3c13
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r14
subl %r12d, %ebp
addl %r12d, %ebx
jmp 0x3c4e
movl %ebp, 0x4(%rsp)
movq %r14, %rbp
movl %ebx, %r14d
movl %r13d, %ebx
callq 0x2030
movl (%rax), %r13d
cmpl $-0x1, %r12d
jne 0x3c97
cmpl $0xb, %r13d
jne 0x3c41
movl $0x1, %edi
callq 0x2160
incl %r15d
movl %ebx, %r13d
movl %r14d, %ebx
movq %rbp, %r14
movl 0x4(%rsp), %ebp
cmpl $0x5, %r15d
setl %al
testl %ebp, %ebp
jle 0x3c63
cmpl $0x5, %r15d
jl 0x3be6
jmp 0x3c63
movb $0x1, %al
testb %al, %al
je 0x3c7c
leaq 0x3a20(%rip), %rsi      # 0x768e
movl $0x3, %edi
xorl %eax, %eax
callq 0x355c
jmp 0x3cd7
testl %ebx, %ebx
jle 0x3cbf
leaq 0x3a2e(%rip), %rsi      # 0x76b5
movl $0x3, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x355c
jmp 0x3cd7
movl %r13d, %edi
callq 0x20a0
leaq 0x39ae(%rip), %rsi      # 0x7654
movl $0x2, %edi
movl %r13d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x355c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x3cd7
leaq 0x3a16(%rip), %rsi      # 0x76dc
movl $0x3, %edi
xorl %eax, %eax
callq 0x355c
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq