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


x86 assembly (clang, -O0):
<bufwrite>:
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)
cmpl   $0x1,-0x24(%rbp)
jbe    1185 <bufwrite+0x55>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x4(%rbp),%ecx
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    118a <bufwrite+0x5a>
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    121d <bufwrite+0xed>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ce <bufwrite+0x9e>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
jmp    121d <bufwrite+0xed>
cmpl   $0x0,-0x18(%rbp)
jne    11dd <bufwrite+0xad>
jmp    121d <bufwrite+0xed>
jmp    11e2 <bufwrite+0xb2>
cmpl   $0x0,-0x24(%rbp)
je     1206 <bufwrite+0xd6>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118a <bufwrite+0x5a>
cmpl   $0x1,-0x24(%rbp)
jbe    1241 <bufwrite+0x111>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     1161 <bufwrite+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 <send@plt>
test   %eax,%eax
js     1170 <bufwrite+0x50>
je     1163 <bufwrite+0x43>
add    %eax,%ebx
cmp    %ebp,%ebx
jb     1140 <bufwrite+0x20>
jmp    1163 <bufwrite+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 <bufwrite+0x43>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [9 x i8] c"bufwrite\00"
define i32 @bufwrite(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 @send(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 ([9 x i8], [9 x i8]* @.str.1, 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
}