#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 size_t ssize_t;

/* Variables and functions */
size_t write(int, char *, size_t);
int write_blocking(int fd, char *buf, size_t bufsz) {
  ssize_t ret, written;
  written = 0;
  do {
    ret = write(fd, buf + written, bufsz - written);
    if (ret == -1)
      return -1;
    written += ret;
  } while (written < bufsz);
  return 0;
}


x86 assembly (clang, -O0):
<write_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    115e <write_blocking+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    117f <write_blocking+0x6f>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     112b <write_blocking+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_blocking>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     1154 <write_blocking+0x44>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1130 <write_blocking+0x20>
xor    %eax,%eax
jmp    1159 <write_blocking+0x49>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_blocking(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* %9
br label %10
10: ; preds = %26, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i64, i64* %9
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i64, i64* %7
%16 = load i64, i64* %9
%17 = sub i64 %15, %16
%18 = call i64 @write(i32 %11, i8* %14, i64 %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %10
store i32 -1, i32* %4
br label %31
22: ; preds = %10
%23 = load i64, i64* %8
%24 = load i64, i64* %9
%25 = add i64 %24, %23
store i64 %25, i64* %9
br label %26
26: ; preds = %22
%27 = load i64, i64* %9
%28 = load i64, i64* %7
%29 = icmp ult i64 %27, %28
br i1 %29, label %10, label %30
30: ; preds = %26
store i32 0, i32* %4
br label %31
31: ; preds = %30, %21
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
define i32 @write_blocking(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %10, %3
%5 = phi i64 [ 0, %3 ], [ %11, %10 ]
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = sub i64 %2, %5
%8 = tail call i64 @write(i32 %0, i8* %6, i64 %7)
%9 = icmp eq i64 %8, -1
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = add i64 %8, %5
%12 = icmp ult i64 %11, %2
br i1 %12, label %4, label %13
13: ; preds = %10, %4
%14 = phi i32 [ -1, %4 ], [ 0, %10 ]
ret i32 %14
}