// File: /workspace/github_cpp_makefile_star2_O3/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 0x3a89(%rip), %rsi      # 0x763a
xorl %ebx, %ebx
movl $0x3, %edi
xorl %eax, %eax
callq 0x352c
testl %ebp, %ebp
jle 0x3c3a
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 0x3bf5
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r14
subl %r12d, %ebp
addl %r12d, %ebx
jmp 0x3c2a
movq %r14, (%rsp)
movl %ebx, %r14d
movl %r13d, %ebx
callq 0x2030
movl (%rax), %r13d
cmpl $-0x1, %r12d
jne 0x3c6a
cmpl $0xb, %r13d
jne 0x3c20
movl $0x1, %edi
callq 0x2160
incl %r15d
movl %ebx, %r13d
movl %r14d, %ebx
movq (%rsp), %r14
testl %ebp, %ebp
jle 0x3c34
cmpl $0x5, %r15d
jl 0x3bc8
cmpl $0x4, %r15d
jg 0x3c4f
leaq 0x3a4d(%rip), %rsi      # 0x768e
movl $0x3, %edi
xorl %eax, %eax
callq 0x352c
jmp 0x3caa
testl %ebx, %ebx
jle 0x3c92
leaq 0x3a5b(%rip), %rsi      # 0x76b5
movl $0x3, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x352c
jmp 0x3caa
movl %r13d, %edi
callq 0x20a0
leaq 0x39db(%rip), %rsi      # 0x7654
movl $0x2, %edi
movl %r13d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x352c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x3caa
leaq 0x3a43(%rip), %rsi      # 0x76dc
movl $0x3, %edi
xorl %eax, %eax
callq 0x352c
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq