#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 EAGAIN;
int ipRecv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ logError(char *, int);
int recvBytes(int sd, void *buf, int size) {
  int len, lenDone = 0;
  char *p = buf;
  while (lenDone < size) {
    len = ipRecv(sd, &p[lenDone], size - lenDone, 0);
    if (len == 0)
      return 0;
    if (len < 0 && len != EAGAIN) {
      logError("ipRecv() %d\n", len);
      return len;
    }
    lenDone += len;
  }
  return lenDone;
}


x86 assembly (clang, -O0):
<recvBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c5 <recvBytes+0xa5>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <ipRecv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1181 <recvBytes+0x61>
movl   $0x0,-0x4(%rbp)
jmp    11cb <recvBytes+0xab>
cmpl   $0x0,-0x18(%rbp)
jge    11b7 <recvBytes+0x97>
mov    -0x18(%rbp),%eax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%eax
je     11b7 <recvBytes+0x97>
mov    -0x18(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1030 <logError@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cb <recvBytes+0xab>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1141 <recvBytes+0x21>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvBytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    118d <recvBytes+0x6d>
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e99(%rip),%r13
xor    %ebp,%ebp
jmp    1157 <recvBytes+0x37>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %ebx,%ebp
cmp    %r12d,%ebp
jge    118f <recvBytes+0x6f>
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %r12d,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1040 <ipRecv@plt>
test   %eax,%eax
je     118d <recvBytes+0x6d>
mov    %eax,%ebx
jns    1150 <recvBytes+0x30>
cmp    0x0(%r13),%ebx
je     1150 <recvBytes+0x30>
lea    0xe7e(%rip),%rdi
mov    %ebx,%esi
call   1030 <logError@plt>
mov    %ebx,%ebp
jmp    118f <recvBytes+0x6f>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EAGAIN = global i32 0
@.str = [13 x i8] c"ipRecv() %d\0A\00"
define i32 @recvBytes(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%11 = load i8*, i8** %6
store i8* %11, i8** %10
br label %12
12: ; preds = %40, %3
%13 = load i32, i32* %9
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %44
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %7
%23 = load i32, i32* %9
%24 = sub nsw i32 %22, %23
%25 = call i32 @ipRecv(i32 %17, i8* %21, i32 %24, i32 0)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %16
store i32 0, i32* %4
br label %46
29: ; preds = %16
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = load i32, i32* %8
%34 = load i32, i32* @EAGAIN
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i32, i32* %8
%38 = call i32 @logError(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %8
store i32 %39, i32* %4
br label %46
40: ; preds = %32, %29
%41 = load i32, i32* %8
%42 = load i32, i32* %9
%43 = add nsw i32 %42, %41
store i32 %43, i32* %9
br label %12
44: ; preds = %12
%45 = load i32, i32* %9
store i32 %45, i32* %4
br label %46
46: ; preds = %44, %36, %28
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@EAGAIN = global i32 0
@.str = [13 x i8] c"ipRecv() %d\0A\00"
define i32 @recvBytes(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %22
5: ; preds = %3, %19
%6 = phi i32 [ %20, %19 ], [ 0, %3 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @ipRecv(i32 %0, i8* %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %5
%13 = icmp sgt i32 %10, -1
%14 = load i32, i32* @EAGAIN
%15 = icmp eq i32 %10, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = tail call i32 @logError(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %22
19: ; preds = %12
%20 = add nsw i32 %10, %6
%21 = icmp slt i32 %20, %2
br i1 %21, label %5, label %22
22: ; preds = %5, %19, %3, %17
%23 = phi i32 [ %10, %17 ], [ 0, %3 ], [ 0, %5 ], [ %20, %19 ]
ret i32 %23
}