#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 recv(int, char *, int, int);
int readall(int sockfd, char *buffer, int bufLen, int flags) {
  int numBytesToRead = bufLen, length;
  while (numBytesToRead > 0) {
    length = recv(sockfd, buffer, numBytesToRead, flags);
    if (length <= 0) {
      return (length);
    }
    numBytesToRead -= length;
    buffer += length;
  }
  return (bufLen);
}


x86 assembly (clang, -O0):
<readall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1180 <readall+0x70>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     115f <readall+0x4f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1186 <readall+0x76>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    112b <readall+0x1b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readall>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
test   %edx,%edx
jle    1152 <readall+0x42>
mov    %ecx,%r15d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    %r15d,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    114f <readall+0x3f>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1130 <readall+0x20>
jmp    1152 <readall+0x42>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readall(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
store i32 %12, i32* %10
br label %13
13: ; preds = %26, %4
%14 = load i32, i32* %10
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load i32, i32* %6
%18 = load i8*, i8** %7
%19 = load i32, i32* %10
%20 = load i32, i32* %9
%21 = call i32 @recv(i32 %17, i8* %18, i32 %19, i32 %20)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = load i32, i32* %11
store i32 %25, i32* %5
br label %36
26: ; preds = %16
%27 = load i32, i32* %11
%28 = load i32, i32* %10
%29 = sub nsw i32 %28, %27
store i32 %29, i32* %10
%30 = load i32, i32* %11
%31 = load i8*, i8** %7
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %7
br label %13
34: ; preds = %13
%35 = load i32, i32* %8
store i32 %35, i32* %5
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
define i32 @readall(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %16
6: ; preds = %4, %11
%7 = phi i32 [ %12, %11 ], [ %2, %4 ]
%8 = phi i8* [ %14, %11 ], [ %1, %4 ]
%9 = tail call i32 @recv(i32 %0, i8* %8, i32 %7, i32 %3)
%10 = icmp slt i32 %9, 1
br i1 %10, label %16, label %11
11: ; preds = %6
%12 = sub nsw i32 %7, %9
%13 = zext i32 %9 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = icmp sgt i32 %12, 0
br i1 %15, label %6, label %16
16: ; preds = %6, %11, %4
%17 = phi i32 [ %2, %4 ], [ %2, %11 ], [ %9, %6 ]
ret i32 %17
}