#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 */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ sync();
size_t write(int, char *, size_t);
int flush_buf(int fd, char *buf, size_t size, size_t write_size) {
  ssize_t r;
  while (write_size <= size) {
    if ((r = write(fd, buf, write_size)) != write_size) {
      if (r < 0)
        perror("write");
      else
        fprintf(stderr, "short write (%d < %d)\n", r, write_size);
      return EXIT_FAILURE;
    }
    sync();
    buf += write_size;
    size -= write_size;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<flush_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11f4 <flush_buf+0xb4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmp    -0x20(%rbp),%rax
je     11cd <flush_buf+0x8d>
cmpq   $0x0,-0x28(%rbp)
jae    119f <flush_buf+0x5f>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
jmp    11bc <flush_buf+0x7c>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe4f(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <flush_buf+0xc0>
mov    $0x0,%al
call   1050 <sync@plt>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1157 <flush_buf+0x17>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flush_buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e91(%rip),%r12
cmp    %rcx,%rdx
jb     1198 <flush_buf+0x68>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
cmp    %r14,%rax
jne    1176 <flush_buf+0x46>
xor    %eax,%eax
call   1050 <sync@plt>
add    %r14,%rbx
sub    %r14,%r15
cmp    %r14,%r15
jae    1150 <flush_buf+0x20>
jmp    1198 <flush_buf+0x68>
mov    0x2e63(%rip),%rcx
mov    (%rcx),%edi
lea    0xe7a(%rip),%rsi
mov    %rax,%rdx
mov    %r14,%rcx
call   1040 <fprintf@plt>
mov    0x2e40(%rip),%r12
mov    (%r12),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"short write (%d < %d)\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @flush_buf(i32 %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
br label %11
11: ; preds = %34, %4
%12 = load i64, i64* %9
%13 = load i64, i64* %8
%14 = icmp ule i64 %12, %13
br i1 %14, label %15, label %42
15: ; preds = %11
%16 = load i32, i32* %6
%17 = load i8*, i8** %7
%18 = load i64, i64* %9
%19 = call i64 @write(i32 %16, i8* %17, i64 %18)
store i64 %19, i64* %10
%20 = load i64, i64* %9
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %34
22: ; preds = %15
%23 = load i64, i64* %10
%24 = icmp ult i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %32
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i64, i64* %10
%30 = load i64, i64* %9
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %29, i64 %30)
br label %32
32: ; preds = %27, %25
%33 = load i32, i32* @EXIT_FAILURE
store i32 %33, i32* %5
br label %44
34: ; preds = %15
%35 = call i32 (...) @sync()
%36 = load i64, i64* %9
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 %36
store i8* %38, i8** %7
%39 = load i64, i64* %9
%40 = load i64, i64* %8
%41 = sub i64 %40, %39
store i64 %41, i64* %8
br label %11
42: ; preds = %11
%43 = load i32, i32* @EXIT_SUCCESS
store i32 %43, i32* %5
br label %44
44: ; preds = %42, %32
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [23 x i8] c"short write (%d < %d)\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @flush_buf(i32 %0, i8* nocapture %1, i64 %2, i64 %3) {
%5 = icmp ult i64 %2, %3
br i1 %5, label %19, label %6
6: ; preds = %4, %14
%7 = phi i8* [ %16, %14 ], [ %1, %4 ]
%8 = phi i64 [ %17, %14 ], [ %2, %4 ]
%9 = tail call i64 @write(i32 %0, i8* %7, i64 %3)
%10 = icmp eq i64 %9, %3
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %9, i64 %3)
br label %19
14: ; preds = %6
%15 = tail call i32 (...) @sync()
%16 = getelementptr inbounds i8, i8* %7, i64 %3
%17 = sub i64 %8, %3
%18 = icmp ult i64 %17, %3
br i1 %18, label %19, label %6
19: ; preds = %14, %4, %11
%20 = phi i32* [ @EXIT_FAILURE, %11 ], [ @EXIT_SUCCESS, %4 ], [ @EXIT_SUCCESS, %14 ]
%21 = load i32, i32* %20
ret i32 %21
}