#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 int /*<<< orphan*/ uint8_t;
struct stat {
  scalar_t__ st_size;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(scalar_t__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pabort(char *);
scalar_t__ read(int, int /*<<< orphan*/ *, scalar_t__);
int stat(char *, struct stat *);
int /*<<< orphan*/ transfer(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                            scalar_t__);
void transfer_file(int fd, char *filename) {
  ssize_t bytes;
  struct stat sb;
  int tx_fd;
  uint8_t *tx;
  uint8_t *rx;
  if (stat(filename, &sb) == -1)
    pabort("can't stat input file");
  tx_fd = open(filename, O_RDONLY);
  if (tx_fd < 0)
    pabort("can't open input file");
  tx = malloc(sb.st_size);
  if (!tx)
    pabort("can't allocate tx buffer");
  rx = malloc(sb.st_size);
  if (!rx)
    pabort("can't allocate rx buffer");
  bytes = read(tx_fd, tx, sb.st_size);
  if (bytes != sb.st_size)
    pabort("failed to read input file");
  transfer(fd, tx, rx, sb.st_size);
  free(rx);
  free(tx);
  close(tx_fd);
}


x86 assembly (clang, -O0):
<transfer_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b1 <transfer_file+0x31>
lea    0xe54(%rip),%rdi
call   1090 <pabort@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11dc <transfer_file+0x5c>
lea    0xe3f(%rip),%rdi
call   1090 <pabort@plt>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1200 <transfer_file+0x80>
lea    0xe31(%rip),%rdi
call   1090 <pabort@plt>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1224 <transfer_file+0xa4>
lea    0xe26(%rip),%rdi
call   1090 <pabort@plt>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1252 <transfer_file+0xd2>
lea    0xe11(%rip),%rdi
call   1090 <pabort@plt>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   10a0 <transfer@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b0 <transfer_file+0x30>
lea    0xe55(%rip),%rdi
call   1090 <pabort@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11d4 <transfer_file+0x54>
lea    0xe47(%rip),%rdi
call   1090 <pabort@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f5 <transfer_file+0x75>
lea    0xe3c(%rip),%rdi
call   1090 <pabort@plt>
mov    %rbp,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1211 <transfer_file+0x91>
lea    0xe39(%rip),%rdi
call   1090 <pabort@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1050 <read@plt>
cmp    %rbp,%rax
je     1230 <transfer_file+0xb0>
lea    0xe33(%rip),%rdi
call   1090 <pabort@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   10a0 <transfer@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [22 x i8] c"can't stat input file\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"can't open input file\00"
@.str.2 = [25 x i8] c"can't allocate tx buffer\00"
@.str.3 = [25 x i8] c"can't allocate rx buffer\00"
@.str.4 = [26 x i8] c"failed to read input file\00"
define void @transfer_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i32 @stat(i8* %10, %struct.stat* %6)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @pabort(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i8*, i8** %4
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @pabort(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i64, i64* %24
%26 = call i32* @malloc(i64 %25)
store i32* %26, i32** %8
%27 = load i32*, i32** %8
%28 = icmp ne i32* %27, null
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = call i32 @pabort(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%33 = load i64, i64* %32
%34 = call i32* @malloc(i64 %33)
store i32* %34, i32** %9
%35 = load i32*, i32** %9
%36 = icmp ne i32* %35, null
br i1 %36, label %39, label %37
37: ; preds = %31
%38 = call i32 @pabort(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %31
%40 = load i32, i32* %7
%41 = load i32*, i32** %8
%42 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%43 = load i64, i64* %42
%44 = call i64 @read(i32 %40, i32* %41, i64 %43)
store i64 %44, i64* %5
%45 = load i64, i64* %5
%46 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%47 = load i64, i64* %46
%48 = icmp ne i64 %45, %47
br i1 %48, label %49, label %51
49: ; preds = %39
%50 = call i32 @pabort(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %39
%52 = load i32, i32* %3
%53 = load i32*, i32** %8
%54 = load i32*, i32** %9
%55 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%56 = load i64, i64* %55
%57 = call i32 @transfer(i32 %52, i32* %53, i32* %54, i64 %56)
%58 = load i32*, i32** %9
%59 = call i32 @free(i32* %58)
%60 = load i32*, i32** %8
%61 = call i32 @free(i32* %60)
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [22 x i8] c"can't stat input file\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"can't open input file\00"
@.str.2 = [25 x i8] c"can't allocate tx buffer\00"
@.str.3 = [25 x i8] c"can't allocate rx buffer\00"
@.str.4 = [26 x i8] c"failed to read input file\00"
define void @transfer_file(i32 %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %1, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @pabort(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %1, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @pabort(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i64, i64* %16
%18 = tail call i32* @malloc(i64 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @pabort(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = tail call i32* @malloc(i64 %17)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @pabort(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = tail call i64 @read(i32 %11, i32* %18, i64 %17)
%29 = icmp eq i64 %28, %17
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = tail call i32 @pabort(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = tail call i32 @transfer(i32 %0, i32* %18, i32* %23, i64 %17)
%34 = tail call i32 @free(i32* %23)
%35 = tail call i32 @free(i32* %18)
%36 = tail call i32 @close(i32 %11)
ret void
}