#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int EINTR;
int MSG_DONTWAIT;
int MSG_PEEK;
int /*<<< orphan*/ printf(char *, int, int, int, ...);
int recv(int, char *, int, int);
int TCPrecv(int sock, char *buf, int buflen, int flag) {
  int inbytes, n;
  if (buflen <= 0)
    return 0;
  inbytes = 0;
  n = recv(sock, &buf[inbytes], buflen - inbytes, flag);
  if (n <= 0 && n != EINTR)
    return n;
  buf[n] = 0;
  printf("\tTCPrecv(sock=%d, buflen=%d, flag=%d): first read %d bytes : `%s`\n",
         sock, buflen, flag, n, buf);
  for (inbytes += n; inbytes < buflen; inbytes += n) {
    if (recv(sock, &buf[inbytes], buflen - inbytes, MSG_PEEK | MSG_DONTWAIT) <=
        0)
      break;
    n = recv(sock, &buf[inbytes], buflen - inbytes, MSG_DONTWAIT);
    buf[n] = 0;
    printf("\tTCPrecv(sock=%d, buflen=%d, flag=%d): subsequent read %d bytes : "
           "`%s`\n",
           sock, buflen, flag, n, &buf[inbytes]);
    if (n <= 0)
      break;
  };
  printf("\tTCPrecv(sock=%d, buflen=%d): read totally %d bytes : `%s`\n", sock,
         buflen, inbytes, buf);
  return inbytes;
}


x86 assembly (clang, -O0):
<TCPrecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     114b <TCPrecv+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    12ab <TCPrecv+0x18b>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1198 <TCPrecv+0x78>
mov    -0x20(%rbp),%eax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%eax
je     1198 <TCPrecv+0x78>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ab <TCPrecv+0x18b>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x10(%rbp),%r9
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    128a <TCPrecv+0x16a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    0x2df5(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddc(%rip),%rax
or     (%rax),%ecx
call   1030 <recv@plt>
cmp    $0x0,%eax
jg     1211 <TCPrecv+0xf1>
jmp    128a <TCPrecv+0x16a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    0x2dac(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x10(%rbp),%r9
movslq -0x1c(%rbp),%rax
add    %rax,%r9
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
jg     1277 <TCPrecv+0x157>
jmp    128a <TCPrecv+0x16a>
jmp    127c <TCPrecv+0x15c>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11cc <TCPrecv+0xac>
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TCPrecv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    122b <TCPrecv+0x10b>
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     115b <TCPrecv+0x3b>
mov    0x2e7d(%rip),%rax
cmp    (%rax),%ebx
jne    1253 <TCPrecv+0x133>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
lea    0xe96(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %ebx,%r8d
mov    %r14,%r9
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %ebp,%ebx
jge    123b <TCPrecv+0x11b>
mov    %r12d,0xc(%rsp)
mov    %r14,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,0x8(%rsp)
movslq %ebx,%rax
lea    (%r14,%rax,1),%r13
mov    %ebp,%r12d
sub    %eax,%ebp
mov    0x2e21(%rip),%rbx
mov    (%rbx),%ecx
mov    0x2e28(%rip),%rax
or     (%rax),%ecx
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <recv@plt>
test   %eax,%eax
jle    1234 <TCPrecv+0x114>
mov    (%rbx),%ecx
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <recv@plt>
mov    %eax,%ebp
mov    %r14,%rax
movslq %ebp,%r14
mov    %r15d,%esi
movb   $0x0,(%rax,%r14,1)
lea    0xe4a(%rip),%rdi
mov    %r12d,%edx
mov    0xc(%rsp),%ecx
mov    %r14d,%r8d
mov    %r13,%r9
xor    %eax,%eax
call   1040 <printf@plt>
test   %r14d,%r14d
jle    122f <TCPrecv+0x10f>
mov    0x8(%rsp),%ebx
add    %ebp,%ebx
cmp    %r12d,%ebx
mov    0x10(%rsp),%r14
mov    %r12d,%ebp
jl     11a0 <TCPrecv+0x80>
jmp    123b <TCPrecv+0x11b>
xor    %ebx,%ebx
jmp    1253 <TCPrecv+0x133>
mov    0x10(%rsp),%r14
mov    %r12d,%ebp
mov    0x8(%rsp),%ebx
lea    0xe49(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r14,%r8
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINTR = global i32 0
@.str = [67 x i8] c"\09TCPrecv(sock=%d, buflen=%d, flag=%d): first read %d bytes : `%s`\0A\00"
@MSG_PEEK = global i32 0
@MSG_DONTWAIT = global i32 0
@.str.1 = [72 x i8] c"\09TCPrecv(sock=%d, buflen=%d, flag=%d): subsequent read %d bytes : `%s`\0A\00"
@.str.2 = [60 x i8] c"\09TCPrecv(sock=%d, buflen=%d): read totally %d bytes : `%s`\0A\00"
define i32 @TCPrecv(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %8
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 0, i32* %5
br label %106
15: ; preds = %4
store i32 0, i32* %10
%16 = load i32, i32* %6
%17 = load i8*, i8** %7
%18 = load i32, i32* %10
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %8
%22 = load i32, i32* %10
%23 = sub nsw i32 %21, %22
%24 = load i32, i32* %9
%25 = call i32 @recv(i32 %16, i8* %20, i32 %23, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %15
%29 = load i32, i32* %11
%30 = load i32, i32* @EINTR
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = load i32, i32* %11
store i32 %33, i32* %5
br label %106
34: ; preds = %28, %15
%35 = load i8*, i8** %7
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %6
%40 = load i32, i32* %8
%41 = load i32, i32* %9
%42 = load i32, i32* %11
%43 = load i8*, i8** %7
%44 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i32 %39, i32 %40, i32 %41, i32 %42, i8* %43)
%45 = load i32, i32* %11
%46 = load i32, i32* %10
%47 = add nsw i32 %46, %45
store i32 %47, i32* %10
br label %48
48: ; preds = %95, %34
%49 = load i32, i32* %10
%50 = load i32, i32* %8
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %99
52: ; preds = %48
%53 = load i32, i32* %6
%54 = load i8*, i8** %7
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i32, i32* %8
%59 = load i32, i32* %10
%60 = sub nsw i32 %58, %59
%61 = load i32, i32* @MSG_PEEK
%62 = load i32, i32* @MSG_DONTWAIT
%63 = or i32 %61, %62
%64 = call i32 @recv(i32 %53, i8* %57, i32 %60, i32 %63)
%65 = icmp sle i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %52
br label %99
67: ; preds = %52
%68 = load i32, i32* %6
%69 = load i8*, i8** %7
%70 = load i32, i32* %10
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i32, i32* %8
%74 = load i32, i32* %10
%75 = sub nsw i32 %73, %74
%76 = load i32, i32* @MSG_DONTWAIT
%77 = call i32 @recv(i32 %68, i8* %72, i32 %75, i32 %76)
store i32 %77, i32* %11
%78 = load i8*, i8** %7
%79 = load i32, i32* %11
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %78, i64 %80
store i8 0, i8* %81
%82 = load i32, i32* %6
%83 = load i32, i32* %8
%84 = load i32, i32* %9
%85 = load i32, i32* %11
%86 = load i8*, i8** %7
%87 = load i32, i32* %10
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %86, i64 %88
%90 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %82, i32 %83, i32 %84, i32 %85, i8* %89)
%91 = load i32, i32* %11
%92 = icmp sle i32 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %67
br label %99
94: ; preds = %67
br label %95
95: ; preds = %94
%96 = load i32, i32* %11
%97 = load i32, i32* %10
%98 = add nsw i32 %97, %96
store i32 %98, i32* %10
br label %48
99: ; preds = %93, %66, %48
%100 = load i32, i32* %6
%101 = load i32, i32* %8
%102 = load i32, i32* %10
%103 = load i8*, i8** %7
%104 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i32 %100, i32 %101, i32 %102, i8* %103)
%105 = load i32, i32* %10
store i32 %105, i32* %5
br label %106
106: ; preds = %99, %32, %14
%107 = load i32, i32* %5
ret i32 %107
}

LLVM IR (-O2):
@EINTR = global i32 0
@.str = [67 x i8] c"\09TCPrecv(sock=%d, buflen=%d, flag=%d): first read %d bytes : `%s`\0A\00"
@MSG_PEEK = global i32 0
@MSG_DONTWAIT = global i32 0
@.str.1 = [72 x i8] c"\09TCPrecv(sock=%d, buflen=%d, flag=%d): subsequent read %d bytes : `%s`\0A\00"
@.str.2 = [60 x i8] c"\09TCPrecv(sock=%d, buflen=%d): read totally %d bytes : `%s`\0A\00"
define i32 @TCPrecv(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp slt i32 %2, 1
br i1 %5, label %40, label %6
6: ; preds = %4
%7 = tail call i32 @recv(i32 %0, i8* %1, i32 %2, i32 %3)
%8 = icmp sgt i32 %7, 0
%9 = load i32, i32* @EINTR
%10 = icmp eq i32 %7, %9
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %12, label %40
12: ; preds = %6
%13 = sext i32 %7 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 0, i8* %14
%15 = tail call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %2, i32 %3, i32 %7, i8* %1)
%16 = icmp slt i32 %7, %2
br i1 %16, label %19, label %37
17: ; preds = %29
%18 = icmp slt i32 %36, %2
br i1 %18, label %19, label %37
19: ; preds = %12, %17
%20 = phi i32 [ %36, %17 ], [ %7, %12 ]
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = sub nsw i32 %2, %20
%24 = load i32, i32* @MSG_PEEK
%25 = load i32, i32* @MSG_DONTWAIT
%26 = or i32 %25, %24
%27 = tail call i32 @recv(i32 %0, i8* %22, i32 %23, i32 %26)
%28 = icmp slt i32 %27, 1
br i1 %28, label %37, label %29
29: ; preds = %19
%30 = load i32, i32* @MSG_DONTWAIT
%31 = tail call i32 @recv(i32 %0, i8* %22, i32 %23, i32 %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %1, i64 %32
store i8 0, i8* %33
%34 = tail call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %2, i32 %3, i32 %31, i8* %22)
%35 = icmp slt i32 %31, 1
%36 = add nsw i32 %31, %20
br i1 %35, label %37, label %17
37: ; preds = %17, %19, %29, %12
%38 = phi i32 [ %7, %12 ], [ %20, %29 ], [ %20, %19 ], [ %36, %17 ]
%39 = tail call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %2, i32 %38, i8* %1)
br label %40
40: ; preds = %6, %4, %37
%41 = phi i32 [ %38, %37 ], [ 0, %4 ], [ %7, %6 ]
ret i32 %41
}