#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 */
#define EAGAIN 128
int errno;
int /*<<< orphan*/ printf(char *);
size_t write(int, char const *, size_t);
int do_write(int fd, const void *buf, size_t count) {
  size_t bytes_written = 0;
  do {
    size_t ret =
        write(fd, (const char *)buf + bytes_written, count - bytes_written);
    if (ret == -1) {
      switch (errno) {
      case EAGAIN:
        continue;
      default:
        return -1;
      }
    } else if (ret == 0) {
      printf("Unexpected end of file\n");
      return -1;
    }
    bytes_written += ret;
  } while (bytes_written < count);
  return 0;
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    118c <do_write+0x6c>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
sub    $0x80,%eax
jne    1180 <do_write+0x60>
jmp    117b <do_write+0x5b>
jmp    11c0 <do_write+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <do_write+0xb5>
cmpq   $0x0,-0x28(%rbp)
jne    11af <do_write+0x8f>
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <do_write+0xb5>
jmp    11b4 <do_write+0x94>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     113b <do_write+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2ea6(%rip),%r12
jmp    114f <do_write+0x2f>
nopl   0x0(%rax)
cmpl   $0x80,(%r12)
jne    1184 <do_write+0x64>
cmp    %rbx,%rbp
jae    1174 <do_write+0x54>
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1040 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     1140 <do_write+0x20>
test   %rax,%rax
je     1178 <do_write+0x58>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     114f <do_write+0x2f>
xor    %eax,%eax
jmp    1189 <do_write+0x69>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [24 x i8] c"Unexpected end of file\0A\00"
define i32 @do_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %35, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i64, i64* %8
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i64, i64* %7
%16 = load i64, i64* %8
%17 = sub i64 %15, %16
%18 = call i64 @write(i32 %11, i8* %14, i64 %17)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %10
%22 = load i32, i32* @errno
switch i32 %22, label %24 [
i32 128, label %23
]
23: ; preds = %21
br label %35
24: ; preds = %21
store i32 -1, i32* %4
br label %40
25: ; preds = %10
%26 = load i64, i64* %9
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %40
30: ; preds = %25
br label %31
31: ; preds = %30
%32 = load i64, i64* %9
%33 = load i64, i64* %8
%34 = add i64 %33, %32
store i64 %34, i64* %8
br label %35
35: ; preds = %31, %23
%36 = load i64, i64* %8
%37 = load i64, i64* %7
%38 = icmp ult i64 %36, %37
br i1 %38, label %10, label %39
39: ; preds = %35
store i32 0, i32* %4
br label %40
40: ; preds = %39, %28, %24
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@errno = global i32 0
@str = [23 x i8] c"Unexpected end of file\00"
define i32 @do_write(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %16, %3
%5 = phi i64 [ 0, %3 ], [ %17, %16 ]
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = sub i64 %2, %5
%8 = tail call i64 @write(i32 %0, i8* %6, i64 %7)
switch i64 %8, label %14 [
i64 -1, label %9
i64 0, label %12
]
9: ; preds = %4
%10 = load i32, i32* @errno
%11 = icmp eq i32 %10, 128
br i1 %11, label %16, label %19
12: ; preds = %4
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %19
14: ; preds = %4
%15 = add i64 %8, %5
br label %16
16: ; preds = %9, %14
%17 = phi i64 [ %15, %14 ], [ %5, %9 ]
%18 = icmp ult i64 %17, %2
br i1 %18, label %4, label %19
19: ; preds = %9, %16, %12
%20 = phi i32 [ -1, %12 ], [ -1, %9 ], [ 0, %16 ]
ret i32 %20
}