#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
unsigned int bufread(int s, void *buf, unsigned int numel) {
  unsigned int numcall = 0, numread = 0, verbose = 0;
  int numthis = 0;
  while (numread < numel) {
    numthis = recv(s, (char *)buf + numread, numel - numread, 0);
    if (numthis < 0) {
      perror("bufread");
      break;
    } else if (numthis == 0)
      break;
    if (verbose > 0)
      fprintf(stderr, "bufread: read %d bytes\n", numthis);
    numread += numthis;
    numcall++;
  }
  if (verbose > 1)
    fprintf(stderr, "bufread: reading the complete buffer required %d calls\n",
            numcall);
  return numread;
}


x86 assembly (clang, -O0):
<bufread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11ef <bufread+0xbf>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a2 <bufread+0x72>
lea    0xe68(%rip),%rdi
call   1050 <perror@plt>
jmp    11ef <bufread+0xbf>
cmpl   $0x0,-0x24(%rbp)
jne    11b1 <bufread+0x81>
jmp    11ef <bufread+0xbf>
jmp    11b6 <bufread+0x86>
cmpl   $0x0,-0x20(%rbp)
jbe    11d8 <bufread+0xa8>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe35(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115e <bufread+0x2e>
cmpl   $0x1,-0x20(%rbp)
jbe    1211 <bufread+0xe1>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe14(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     1161 <bufread+0x41>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%esi
add    %r14,%rsi
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1170 <bufread+0x50>
je     1163 <bufread+0x43>
add    %eax,%ebx
cmp    %ebp,%ebx
jb     1140 <bufread+0x20>
jmp    1163 <bufread+0x43>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
jmp    1163 <bufread+0x43>

LLVM IR (-O0):
@.str = [8 x i8] c"bufread\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"bufread: read %d bytes\0A\00"
@.str.2 = [56 x i8] c"bufread: reading the complete buffer required %d calls\0A\00"
define i32 @bufread(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %41, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %6
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %47
15: ; preds = %11
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %8
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %6
%22 = load i32, i32* %8
%23 = sub i32 %21, %22
%24 = call i32 @recv(i32 %16, i8* %20, i32 %23, i32 0)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %47
29: ; preds = %15
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
br label %47
33: ; preds = %29
br label %34
34: ; preds = %33
%35 = load i32, i32* %9
%36 = icmp ugt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = load i32, i32* %10
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %37, %34
%42 = load i32, i32* %10
%43 = load i32, i32* %8
%44 = add i32 %43, %42
store i32 %44, i32* %8
%45 = load i32, i32* %7
%46 = add i32 %45, 1
store i32 %46, i32* %7
br label %11
47: ; preds = %32, %27, %11
%48 = load i32, i32* %9
%49 = icmp ugt i32 %48, 1
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = load i32, i32* %7
%53 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %50, %47
%55 = load i32, i32* %8
ret i32 %55
}

LLVM IR (-O2):
@.str = [8 x i8] c"bufread\00"
@stderr = global i32 0
define i32 @bufread(i32 %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %19, label %7
5: ; preds = %16
%6 = icmp ult i32 %18, %2
br i1 %6, label %7, label %19
7: ; preds = %3, %5
%8 = phi i32 [ %18, %5 ], [ 0, %3 ]
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = sub i32 %2, %8
%12 = tail call i32 @recv(i32 %0, i8* %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %19
16: ; preds = %7
%17 = icmp eq i32 %12, 0
%18 = add i32 %12, %8
br i1 %17, label %19, label %5
19: ; preds = %16, %5, %3, %14
%20 = phi i32 [ %8, %14 ], [ 0, %3 ], [ %8, %16 ], [ %18, %5 ]
ret i32 %20
}