int Net_recv_All(unsigned int sock, int timeout_ms, char *buf, int maxlength) {
  int recvbytes = 0;
  int cnt = 0;
  int space = maxlength;
  int select_counter = 0;
  long long tm = getTMmillisec();
  int maxPer = (maxlength < 20) ? maxlength : 20;
  if (maxlength <= 0 || timeout_ms <= 0) {
    do {
      if (2 >= 0)
        printf("[%d]%s(%d)%s: ***err*** "
               "arg maxlength:%d, timeout_ms:%d\n",
               getpid(),
               "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
               "outputs/2022-01-22/02-19-57/repos/tangyong3691/-CodingLearners/"
               "refs/heads/master/sources/linux/tcptest/tcpclient.c",
               454, __func__, maxlength, timeout_ms);
    } while (0);
    return -1;
  }
  while (getTMmillisec() - tm < timeout_ms) {
    while (cnt == 0) {
      int ret = Net_Recv(sock, buf + cnt,
                         (space - cnt < maxPer ? space - cnt : maxPer));
      if (ret < 0) {
        do {
          if (2 >= 0)
            printf("[%d]%s(%d)%s: ***err*** "
                   "recv return < 0, ret=%d\n",
                   getpid(),
                   "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/tangyong3691/"
                   "-CodingLearners/refs/heads/master/sources/linux/tcptest/"
                   "tcpclient.c",
                   466, __func__, ret);
        } while (0);
        return recvbytes ? recvbytes : ret;
      } else if (ret == 0) {
        select_counter++;
      } else {
        cnt += ret;
        select_counter = 0;
      }
      if (select_counter >= 20) {
        break;
      }
      if (ret != maxPer)
        usleep(0);
    }
    buf += cnt;
    recvbytes += cnt;
    space -= cnt;
    if (cnt != 0)
      break;
    cnt = 0;
    usleep(50 * 1000);
  }
  return recvbytes;
}


x86 assembly (clang, -O0):
<Net_recv_All>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x0,%al
call   1060 <getTMmillisec@plt>
cltq
mov    %rax,-0x38(%rbp)
cmpl   $0x14,-0x1c(%rbp)
jge    11a2 <Net_recv_All+0x52>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    11af <Net_recv_All+0x5f>
mov    $0x14,%eax
mov    %eax,-0x44(%rbp)
jmp    11af <Net_recv_All+0x5f>
mov    -0x44(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11c9 <Net_recv_All+0x79>
cmpl   $0x0,-0xc(%rbp)
jg     120e <Net_recv_All+0xbe>
jmp    11ce <Net_recv_All+0x7e>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x1c(%rbp),%r9d
mov    -0xc(%rbp),%eax
lea    0xe1b(%rip),%rdi
lea    0xe4d(%rip),%rdx
mov    $0x1c6,%ecx
lea    0xeec(%rip),%r8
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1393 <Net_recv_All+0x243>
jmp    1213 <Net_recv_All+0xc3>
mov    $0x0,%al
call   1060 <getTMmillisec@plt>
cltq
sub    -0x38(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmp    %rcx,%rax
jge    138d <Net_recv_All+0x23d>
jmp    1232 <Net_recv_All+0xe2>
cmpl   $0x0,-0x24(%rbp)
jne    1341 <Net_recv_All+0x1f1>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    126e <Net_recv_All+0x11e>
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x58(%rbp)
jmp    1274 <Net_recv_All+0x124>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edi
mov    -0x58(%rbp),%edx
mov    $0x0,%al
call   1030 <Net_Recv@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    12eb <Net_recv_All+0x19b>
jmp    1297 <Net_recv_All+0x147>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x40(%rbp),%r9d
lea    0xe46(%rip),%rdi
lea    0xd87(%rip),%rdx
mov    $0x1d2,%ecx
lea    0xe26(%rip),%r8
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
je     12da <Net_recv_All+0x18a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x5c(%rbp)
jmp    12e0 <Net_recv_All+0x190>
mov    -0x40(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1393 <Net_recv_All+0x243>
cmpl   $0x0,-0x40(%rbp)
jne    1303 <Net_recv_All+0x1b3>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1313 <Net_recv_All+0x1c3>
mov    -0x40(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
jmp    1318 <Net_recv_All+0x1c8>
cmpl   $0x14,-0x2c(%rbp)
jl     1327 <Net_recv_All+0x1d7>
jmp    1341 <Net_recv_All+0x1f1>
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
je     133c <Net_recv_All+0x1ec>
xor    %edi,%edi
mov    $0x0,%al
call   1070 <usleep@plt>
jmp    1232 <Net_recv_All+0xe2>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1375 <Net_recv_All+0x225>
jmp    138d <Net_recv_All+0x23d>
movl   $0x0,-0x24(%rbp)
mov    $0xc350,%edi
mov    $0x0,%al
call   1070 <usleep@plt>
jmp    1213 <Net_recv_All+0xc3>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Net_recv_All>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%r13d
mov    %edi,%r12d
xor    %eax,%eax
call   1060 <getTMmillisec@plt>
cmp    $0x14,%ebp
mov    $0x14,%ebx
cmovl  %ebp,%ebx
test   %r13d,%r13d
jle    122c <Net_recv_All+0xdc>
test   %ebp,%ebp
jle    122c <Net_recv_All+0xdc>
movslq %eax,%rbp
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <getTMmillisec@plt>
cltq
sub    %rbp,%rax
mov    %r13d,%ecx
cmp    %rcx,%rax
jge    129e <Net_recv_All+0x14e>
xor    %r14d,%r14d
xor    %r13d,%r13d
mov    %rbp,0x10(%rsp)
mov    %rcx,0x8(%rsp)
jmp    11cc <Net_recv_All+0x7c>
nopl   0x0(%rax)
add    $0x1,%r13d
test   %ebp,%ebp
jne    1265 <Net_recv_All+0x115>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <Net_Recv@plt>
mov    %eax,%ebp
test   %eax,%eax
js     126a <Net_recv_All+0x11a>
jne    11ed <Net_recv_All+0x9d>
cmp    $0x12,%r13d
jg     11fc <Net_recv_All+0xac>
cmp    %ebx,%ebp
je     11c0 <Net_recv_All+0x70>
xor    %edi,%edi
xor    %eax,%eax
call   1070 <usleep@plt>
jmp    11c0 <Net_recv_All+0x70>
add    %ebp,%r14d
mov    $0xc350,%edi
xor    %eax,%eax
call   1070 <usleep@plt>
xor    %eax,%eax
call   1060 <getTMmillisec@plt>
cltq
mov    0x10(%rsp),%rbp
sub    %rbp,%rax
add    $0x1,%r13d
mov    0x8(%rsp),%rcx
cmp    %rcx,%rax
jl     11cc <Net_recv_All+0x7c>
jmp    129e <Net_recv_All+0x14e>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r13d,(%rsp)
lea    0xdc2(%rip),%rdi
lea    0xdf4(%rip),%rdx
lea    0xe98(%rip),%r8
mov    %eax,%esi
mov    $0x1c6,%ecx
mov    %ebp,%r9d
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%r14d
jmp    129e <Net_recv_All+0x14e>
add    %ebp,%r14d
jmp    129e <Net_recv_All+0x14e>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe79(%rip),%rdi
lea    0xdba(%rip),%rdx
lea    0xe5e(%rip),%r8
mov    %eax,%esi
mov    $0x1d2,%ecx
mov    %ebp,%r9d
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
cmove  %ebp,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [57 x i8] c"[%d]%s(%d)%s: ***err*** arg maxlength:%d, timeout_ms:%d\0A\00"
@.str.1 = [171 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/tangyong3691/-CodingLearners/refs/heads/master/sources/linux/tcptest/tcpclient.c\00"
@__func__.Net_recv_All = [13 x i8] c"Net_recv_All\00"
@.str.2 = [49 x i8] c"[%d]%s(%d)%s: ***err*** recv return < 0, ret=%d\0A\00"
define i32 @Net_recv_All(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%17 = load i32, i32* %9
store i32 %17, i32* %12
store i32 0, i32* %13
%18 = call i32 (...) @getTMmillisec()
%19 = sext i32 %18 to i64
store i64 %19, i64* %14
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 20
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = load i32, i32* %9
br label %25
24: ; preds = %4
br label %25
25: ; preds = %24, %22
%26 = phi i32 [ %23, %22 ], [ 20, %24 ]
store i32 %26, i32* %15
%27 = load i32, i32* %9
%28 = icmp sle i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = load i32, i32* %7
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %29, %25
br label %33
33: ; preds = %32
%34 = call i32 (...) @getpid()
%35 = load i32, i32* %9
%36 = load i32, i32* %7
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %34, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 454, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__func__.Net_recv_All, i64 0, i64 0), i32 %35, i32 %36)
br label %38
38: ; preds = %33
store i32 -1, i32* %5
br label %128
39: ; preds = %29
br label %40
40: ; preds = %124, %39
%41 = call i32 (...) @getTMmillisec()
%42 = sext i32 %41 to i64
%43 = load i64, i64* %14
%44 = sub nsw i64 %42, %43
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = icmp slt i64 %44, %46
br i1 %47, label %48, label %126
48: ; preds = %40
br label %49
49: ; preds = %109, %48
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %110
52: ; preds = %49
%53 = load i32, i32* %6
%54 = load i8*, i8** %8
%55 = load i32, i32* %11
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i32, i32* %12
%59 = load i32, i32* %11
%60 = sub nsw i32 %58, %59
%61 = load i32, i32* %15
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %67
63: ; preds = %52
%64 = load i32, i32* %12
%65 = load i32, i32* %11
%66 = sub nsw i32 %64, %65
br label %69
67: ; preds = %52
%68 = load i32, i32* %15
br label %69
69: ; preds = %67, %63
%70 = phi i32 [ %66, %63 ], [ %68, %67 ]
%71 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @Net_Recv to i32 (i32, i8*, i32, ...)*)(i32 %53, i8* %57, i32 %70)
store i32 %71, i32* %16
%72 = load i32, i32* %16
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %88
74: ; preds = %69
br label %75
75: ; preds = %74
%76 = call i32 (...) @getpid()
%77 = load i32, i32* %16
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %76, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 466, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__func__.Net_recv_All, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %75
%80 = load i32, i32* %10
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = load i32, i32* %10
br label %86
84: ; preds = %79
%85 = load i32, i32* %16
br label %86
86: ; preds = %84, %82
%87 = phi i32 [ %83, %82 ], [ %85, %84 ]
store i32 %87, i32* %5
br label %128
88: ; preds = %69
%89 = load i32, i32* %16
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %94
91: ; preds = %88
%92 = load i32, i32* %13
%93 = add nsw i32 %92, 1
store i32 %93, i32* %13
br label %98
94: ; preds = %88
%95 = load i32, i32* %16
%96 = load i32, i32* %11
%97 = add nsw i32 %96, %95
store i32 %97, i32* %11
store i32 0, i32* %13
br label %98
98: ; preds = %94, %91
br label %99
99: ; preds = %98
%100 = load i32, i32* %13
%101 = icmp sge i32 %100, 20
br i1 %101, label %102, label %103
102: ; preds = %99
br label %110
103: ; preds = %99
%104 = load i32, i32* %16
%105 = load i32, i32* %15
%106 = icmp ne i32 %104, %105
br i1 %106, label %107, label %109
107: ; preds = %103
%108 = call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 0)
br label %109
109: ; preds = %107, %103
br label %49
110: ; preds = %102, %49
%111 = load i32, i32* %11
%112 = load i8*, i8** %8
%113 = sext i32 %111 to i64
%114 = getelementptr inbounds i8, i8* %112, i64 %113
store i8* %114, i8** %8
%115 = load i32, i32* %11
%116 = load i32, i32* %10
%117 = add nsw i32 %116, %115
store i32 %117, i32* %10
%118 = load i32, i32* %11
%119 = load i32, i32* %12
%120 = sub nsw i32 %119, %118
store i32 %120, i32* %12
%121 = load i32, i32* %11
%122 = icmp ne i32 %121, 0
br i1 %122, label %123, label %124
123: ; preds = %110
br label %126
124: ; preds = %110
store i32 0, i32* %11
%125 = call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 50000)
br label %40
126: ; preds = %123, %40
%127 = load i32, i32* %10
store i32 %127, i32* %5
br label %128
128: ; preds = %126, %86, %38
%129 = load i32, i32* %5
ret i32 %129
}

LLVM IR (-O2):
@.str = [57 x i8] c"[%d]%s(%d)%s: ***err*** arg maxlength:%d, timeout_ms:%d\0A\00"
@.str.1 = [171 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/tangyong3691/-CodingLearners/refs/heads/master/sources/linux/tcptest/tcpclient.c\00"
@__func__.Net_recv_All = [13 x i8] c"Net_recv_All\00"
@.str.2 = [49 x i8] c"[%d]%s(%d)%s: ***err*** recv return < 0, ret=%d\0A\00"
define i32 @Net_recv_All(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i32 (...) @getTMmillisec()
%6 = sext i32 %5 to i64
%7 = icmp slt i32 %3, 20
%8 = select i1 %7, i32 %3, i32 20
%9 = icmp slt i32 %3, 1
%10 = icmp slt i32 %1, 1
%11 = or i1 %10, %9
br i1 %11, label %18, label %12
12: ; preds = %4
%13 = tail call i32 (...) @getTMmillisec()
%14 = sext i32 %13 to i64
%15 = sub nsw i64 %14, %6
%16 = zext i32 %1 to i64
%17 = icmp slt i64 %15, %16
br i1 %17, label %21, label %52
18: ; preds = %4
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %19, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 454, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__func__.Net_recv_All, i64 0, i64 0), i32 %3, i32 %1)
br label %52
21: ; preds = %12, %45
%22 = phi i32 [ %46, %45 ], [ 0, %12 ]
%23 = phi i32 [ %37, %45 ], [ 0, %12 ]
br label %26
24: ; preds = %44
%25 = add nsw i32 %28, %22
br label %52
26: ; preds = %21, %44
%27 = phi i32 [ %23, %21 ], [ %37, %44 ]
%28 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @Net_Recv to i32 (i32, i8*, i32, ...)*)(i32 %0, i8* %2, i32 %8)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = tail call i32 (...) @getpid()
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %31, i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.1, i64 0, i64 0), i32 466, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @__func__.Net_recv_All, i64 0, i64 0), i32 %28)
%33 = icmp eq i32 %22, 0
%34 = select i1 %33, i32 %28, i32 %22
br label %52
35: ; preds = %26
%36 = icmp eq i32 %28, 0
%37 = add nsw i32 %27, 1
%38 = icmp sgt i32 %27, 18
%39 = select i1 %36, i1 %38, i1 false
br i1 %39, label %45, label %40
40: ; preds = %35
%41 = icmp eq i32 %28, %8
br i1 %41, label %44, label %42
42: ; preds = %40
%43 = tail call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 0)
br label %44
44: ; preds = %40, %42
br i1 %36, label %26, label %24
45: ; preds = %35
%46 = add nsw i32 %28, %22
%47 = tail call i32 (i32, ...) bitcast (i32 (...)* @usleep to i32 (i32, ...)*)(i32 50000)
%48 = tail call i32 (...) @getTMmillisec()
%49 = sext i32 %48 to i64
%50 = sub nsw i64 %49, %6
%51 = icmp slt i64 %50, %16
br i1 %51, label %21, label %52
52: ; preds = %45, %12, %24, %30, %18
%53 = phi i32 [ -1, %18 ], [ %34, %30 ], [ %25, %24 ], [ 0, %12 ], [ %46, %45 ]
ret i32 %53
}