#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 /*<<< orphan*/ perror(char *);
size_t write(int, char *, size_t);
ssize_t drain_buf(char *buf, size_t bufsize, int fd) {
  ssize_t this_write = 0, count = 0;
  do {
    this_write = write(fd, buf + count, bufsize - count);
    if (this_write == -1) {
      perror("write");
      return -1;
    }
    count += this_write;
  } while (count < bufsize && this_write > 0);
  return count;
}


x86 assembly (clang, -O0):
<drain_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1183 <drain_buf+0x63>
lea    0xe8f(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c0 <drain_buf+0xa0>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x18(%rbp),%rcx
mov    %al,-0x31(%rbp)
jae    11ad <drain_buf+0x8d>
cmpq   $0x0,-0x28(%rbp)
seta   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1143 <drain_buf+0x23>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drain_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r15,%rbx,1),%rsi
mov    %rbp,%rdx
sub    %rbx,%rdx
mov    %r14d,%edi
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     1173 <drain_buf+0x53>
add    %rax,%rbx
test   %rax,%rax
je     1165 <drain_buf+0x45>
cmp    %rbp,%rbx
jb     1140 <drain_buf+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe86(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffffffffffff,%rbx
jmp    1165 <drain_buf+0x45>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i64 @drain_buf(i8* %0, i64 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i64 0, i64* %8
store i64 0, i64* %9
br label %10
10: ; preds = %34, %3
%11 = load i32, i32* %7
%12 = load i8*, i8** %5
%13 = load i64, i64* %9
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i64, i64* %6
%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 %23
21: ; preds = %10
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %4
br label %38
23: ; preds = %10
%24 = load i64, i64* %8
%25 = load i64, i64* %9
%26 = add i64 %25, %24
store i64 %26, i64* %9
br label %27
27: ; preds = %23
%28 = load i64, i64* %9
%29 = load i64, i64* %6
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i64, i64* %8
%33 = icmp ugt i64 %32, 0
br label %34
34: ; preds = %31, %27
%35 = phi i1 [ false, %27 ], [ %33, %31 ]
br i1 %35, label %10, label %36
36: ; preds = %34
%37 = load i64, i64* %9
store i64 %37, i64* %4
br label %38
38: ; preds = %36, %21
%39 = load i64, i64* %4
ret i64 %39
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i64 @drain_buf(i8* nocapture %0, i64 %1, i32 %2) {
br label %4
4: ; preds = %12, %3
%5 = phi i64 [ 0, %3 ], [ %13, %12 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = sub i64 %1, %5
%8 = tail call i64 @write(i32 %2, i8* %6, i64 %7)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %4
%13 = add i64 %8, %5
%14 = icmp ult i64 %13, %1
%15 = icmp ne i64 %8, 0
%16 = and i1 %15, %14
br i1 %16, label %4, label %17
17: ; preds = %12, %10
%18 = phi i64 [ -1, %10 ], [ %13, %12 ]
ret i64 %18
}