#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;
int /*<<< orphan*/ usleep(int);
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 (numread < numel)
      usleep(1000);
  }
  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    1215 <bufread+0xd5>
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    11b2 <bufread+0x72>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
jmp    1215 <bufread+0xd5>
cmpl   $0x0,-0x24(%rbp)
jne    11c1 <bufread+0x81>
jmp    1215 <bufread+0xd5>
jmp    11c6 <bufread+0x86>
cmpl   $0x0,-0x20(%rbp)
jbe    11e8 <bufread+0xa8>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe25(%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)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1210 <bufread+0xd0>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
jmp    116e <bufread+0x2e>
cmpl   $0x1,-0x20(%rbp)
jbe    1237 <bufread+0xf7>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xdee(%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     11a1 <bufread+0x71>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1191 <bufread+0x61>
mov    %eax,%ecx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
test   %ecx,%ecx
je     11a3 <bufread+0x73>
mov    %ecx,%ebp
add    %eax,%ebp
cmp    %ebx,%ebp
jae    119d <bufread+0x6d>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
mov    %ebp,%esi
add    %r14,%rsi
mov    %ebx,%edx
sub    %ebp,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
mov    %ebp,%eax
test   %ecx,%ecx
jns    1160 <bufread+0x30>
lea    0xe68(%rip),%rdi
call   1040 <perror@plt>
mov    %ebp,%eax
jmp    11a3 <bufread+0x73>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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 = %52, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %6
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %53
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 %53
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 %53
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
%47 = load i32, i32* %8
%48 = load i32, i32* %6
%49 = icmp ult i32 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %41
%51 = call i32 @usleep(i32 1000)
br label %52
52: ; preds = %50, %41
br label %11
53: ; preds = %32, %27, %11
%54 = load i32, i32* %9
%55 = icmp ugt i32 %54, 1
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = load i32, i32* %7
%59 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %56, %53
%61 = load i32, i32* %8
ret i32 %61
}

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 %25, label %5
5: ; preds = %3
%6 = tail call i32 @recv(i32 %0, i8* %1, i32 %2, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %18, %5
%9 = phi i32 [ 0, %5 ], [ %16, %18 ]
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %25
11: ; preds = %5, %18
%12 = phi i32 [ %23, %18 ], [ %6, %5 ]
%13 = phi i32 [ %16, %18 ], [ 0, %5 ]
%14 = icmp eq i32 %12, 0
br i1 %14, label %25, label %15
15: ; preds = %11
%16 = add i32 %12, %13
%17 = icmp ult i32 %16, %2
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = tail call i32 @usleep(i32 1000)
%20 = zext i32 %16 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = sub i32 %2, %16
%23 = tail call i32 @recv(i32 %0, i8* %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %8, label %11
25: ; preds = %15, %11, %3, %8
%26 = phi i32 [ %9, %8 ], [ 0, %3 ], [ %16, %15 ], [ %13, %11 ]
ret i32 %26
}