#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 */
int /*<<< orphan*/ close(int);
int read(int, char *, int);
int write_blocking(int, char *, int);
void pump_ex(int input, int output, int close_output) {
  char buf[4096];
  int len;
  while ((len = read(input, buf, 4096)) > 0) {
    if (write_blocking(output, buf, len) == -1)
      break;
  }
  close(input);
  if (close_output)
    close(output);
}


x86 assembly (clang, -O0):
<pump_ex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jle    118f <pump_ex+0x5f>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1050 <write_blocking@plt>
cmp    $0xffffffff,%eax
jne    118a <pump_ex+0x5a>
jmp    118f <pump_ex+0x5f>
jmp    1144 <pump_ex+0x14>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0xc(%rbp)
je     11a9 <pump_ex+0x79>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pump_ex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebp
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1175 <pump_ex+0x45>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write_blocking@plt>
cmp    $0xffffffff,%eax
jne    1150 <pump_ex+0x20>
mov    %ebp,%edi
call   1030 <close@plt>
test   %r14d,%r14d
je     1189 <pump_ex+0x59>
mov    %r15d,%edi
call   1030 <close@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @pump_ex(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
br label %9
9: ; preds = %21, %3
%10 = load i32, i32* %4
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 4096)
store i32 %12, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = load i32, i32* %5
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%17 = load i32, i32* %8
%18 = call i32 @write_blocking(i32 %15, i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %14
br label %22
21: ; preds = %14
br label %9
22: ; preds = %20, %9
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %22
ret void
}

LLVM IR (-O2):
define void @pump_ex(i32 %0, i32 %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
br label %6
6: ; preds = %9, %3
%7 = call i32 @read(i32 %0, i8* %5, i32 4096)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = call i32 @write_blocking(i32 %1, i8* %5, i32 %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %6
12: ; preds = %9, %6
%13 = call i32 @close(i32 %0)
%14 = icmp eq i32 %2, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @close(i32 %1)
br label %17
17: ; preds = %15, %12
ret void
}