#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 */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int const, char *, int);
scalar_t__ write_all(int const, char *, scalar_t__);
ssize_t pass_through(const int fd_in, const int fd_out) {
  char *buf = NULL;
  ssize_t br = 0;
  ssize_t total_bytes = 0;
  buf = malloc(32 * 1024);
  if (buf == NULL) {
    perror("Error from malloc");
    abort();
  }
  do {
    memset(buf, 0, 32 * 1024);
    br = read(fd_in, buf, 32 * 1024);
    if (br > 0) {
      total_bytes += write_all(fd_out, buf, br);
    } else if (br < 0) {
      perror("Error reading data");
    }
  } while (br > 0);
  free(buf);
  return total_bytes;
}


x86 assembly (clang, -O0):
<pass_through>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x8000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c2 <pass_through+0x52>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    11c7 <pass_through+0x57>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8000,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8000,%edx
call   1060 <read@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1214 <pass_through+0xa4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <write_all@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1230 <pass_through+0xc0>
cmpq   $0x0,-0x18(%rbp)
jge    122b <pass_through+0xbb>
lea    0xdec(%rip),%rdi
call   1090 <perror@plt>
jmp    1230 <pass_through+0xc0>
jmp    1235 <pass_through+0xc5>
cmpq   $0x0,-0x18(%rbp)
jg     11c7 <pass_through+0x57>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pass_through>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%ebp
mov    $0x8000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     121d <pass_through+0xad>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x8000,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x8000,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    11f4 <pass_through+0x84>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1070 <write_all@plt>
add    %rax,%rbx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8000,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x8000,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     11c0 <pass_through+0x50>
test   %rax,%rax
js     120f <pass_through+0x9f>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
jmp    11f9 <pass_through+0x89>
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Error from malloc\00"
@.str.1 = [19 x i8] c"Error reading data\00"
define i64 @pass_through(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
store i8* null, i8** %5
store i64 0, i64* %6
store i64 0, i64* %7
%8 = call i8* @malloc(i32 32768)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (...) @abort()
unreachable
14: ; preds = %2
br label %15
15: ; preds = %37, %14
%16 = load i8*, i8** %5
%17 = call i32 @memset(i8* %16, i32 0, i32 32768)
%18 = load i32, i32* %3
%19 = load i8*, i8** %5
%20 = call i64 @read(i32 %18, i8* %19, i32 32768)
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %15
%24 = load i32, i32* %4
%25 = load i8*, i8** %5
%26 = load i64, i64* %6
%27 = call i64 @write_all(i32 %24, i8* %25, i64 %26)
%28 = load i64, i64* %7
%29 = add nsw i64 %28, %27
store i64 %29, i64* %7
br label %36
30: ; preds = %15
%31 = load i64, i64* %6
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
br label %36
36: ; preds = %35, %23
br label %37
37: ; preds = %36
%38 = load i64, i64* %6
%39 = icmp sgt i64 %38, 0
br i1 %39, label %15, label %40
40: ; preds = %37
%41 = load i8*, i8** %5
%42 = call i32 @free(i8* %41)
%43 = load i64, i64* %7
ret i64 %43
}

LLVM IR (-O2):
@.str = [18 x i8] c"Error from malloc\00"
@.str.1 = [19 x i8] c"Error reading data\00"
define i64 @pass_through(i32 %0, i32 %1) {
%3 = tail call dereferenceable_or_null(32768) i8* @malloc(i32 32768)
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = tail call i32 @memset(i8* %3, i32 0, i32 32768)
%7 = tail call i64 @read(i32 %0, i8* %3, i32 32768)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %18, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %18, %5
%13 = phi i64 [ 0, %5 ], [ %22, %18 ]
%14 = phi i64 [ %7, %5 ], [ %24, %18 ]
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %26
18: ; preds = %5, %18
%19 = phi i64 [ %24, %18 ], [ %7, %5 ]
%20 = phi i64 [ %22, %18 ], [ 0, %5 ]
%21 = tail call i64 @write_all(i32 %1, i8* %3, i64 %19)
%22 = add nsw i64 %21, %20
%23 = tail call i32 @memset(i8* %3, i32 0, i32 32768)
%24 = tail call i64 @read(i32 %0, i8* %3, i32 32768)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %18, label %12
26: ; preds = %16, %12
%27 = tail call i32 @free(i8* %3)
ret i64 %13
}