#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ die(char *, size_t, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ write(int, char *, size_t);
void write_buf(int fd, char *buf, size_t size) {
  size_t progress = 0;
  ssize_t result;
  while (progress < size) {
    result = write(fd, buf + progress, size - progress);
    if (result < 0) {
      if ((errno == EAGAIN) || (errno == EINTR)) {
        continue;
      }
      die("write of %ld bytes failed: %s\n", size - progress, strerror(errno));
    }
    progress += result;
  }
}


x86 assembly (clang, -O0):
<write_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f7 <write_buf+0xc7>
mov    -0x4(%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   $0x0,-0x28(%rbp)
jge    11e6 <write_buf+0xb6>
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
je     11b4 <write_buf+0x84>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <write_buf+0x89>
jmp    114b <write_buf+0x1b>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe1f(%rip),%rdi
call   1040 <die@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    114b <write_buf+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,(%rsp)
mov    %rdx,0x8(%rsp)
test   %rdx,%rdx
je     11d3 <write_buf+0xa3>
mov    %edi,%r13d
xor    %ecx,%ecx
mov    0x2e84(%rip),%r12
mov    0x2e65(%rip),%r14
mov    0x8(%rsp),%rbx
mov    (%rsp),%rbp
jmp    118a <write_buf+0x5a>
xchg   %ax,%ax
mov    0x10(%rsp),%rcx
add    %r15,%rcx
mov    (%rsp),%rax
lea    (%rax,%rcx,1),%rbp
mov    0x8(%rsp),%rbx
sub    %rcx,%rbx
jbe    11d3 <write_buf+0xa3>
mov    %rcx,0x10(%rsp)
nop
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %rax,%r15
test   %rax,%rax
jns    1170 <write_buf+0x40>
mov    (%r12),%rdi
cmp    (%r14),%rdi
je     1190 <write_buf+0x60>
mov    0x2e22(%rip),%rax
cmp    (%rax),%rdi
je     1190 <write_buf+0x60>
call   1050 <strerror@plt>
lea    0xe39(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <die@plt>
jmp    1170 <write_buf+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [31 x i8] c"write of %ld bytes failed: %s\0A\00"
define void @write_buf(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %27, label %5
5: ; preds = %3, %22
%6 = phi i64 [ %26, %22 ], [ %2, %3 ]
%7 = phi i8* [ %25, %22 ], [ %1, %3 ]
%8 = phi i64 [ %23, %22 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %12
%10 = tail call i64 @write(i32 %0, i8* %7, i64 %6)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EAGAIN
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %9, label %19
19: ; preds = %12
%20 = tail call i32 @strerror(i64 %13)
%21 = tail call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i64 %6, i32 %20)
br label %22
22: ; preds = %9, %19
%23 = add i64 %10, %8
%24 = icmp ult i64 %23, %2
%25 = getelementptr inbounds i8, i8* %1, i64 %23
%26 = sub i64 %2, %23
br i1 %24, label %5, label %27
27: ; preds = %22, %3
ret void
}