#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, long,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(long);
int /*<<< orphan*/ perror(char const *);
int append_file(const char *input, FILE *fout) {
  FILE *fin;
  long len;
  uint8_t *buf;
  int ret = 0;
  fin = fopen(input, "rb");
  if (fin == NULL) {
    perror(input);
    return 1;
  }
  fseek(fin, 0, SEEK_END);
  len = ftell(fin);
  fseek(fin, 0, SEEK_SET);
  buf = malloc(len);
  if (buf == NULL) {
    ret = 1;
    goto err_malloc;
  }
  fread(buf, 1, len, fin);
  fwrite(buf, 1, len, fout);
  free(buf);
err_malloc:
  fclose(fin);
  return ret;
}


x86 assembly (clang, -O0):
<append_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11db <append_file+0x4b>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1278 <append_file+0xe8>
mov    -0x20(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1234 <append_file+0xa4>
movl   $0x1,-0x34(%rbp)
jmp    1269 <append_file+0xd9>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe5b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1244 <append_file+0xb4>
mov    %rax,%rbx
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     122a <append_file+0x9a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1230 <append_file+0xa0>
mov    $0x1,%r15d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   10a0 <perror@plt>
mov    $0x1,%r15d
jmp    1238 <append_file+0xa8>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @append_file(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @perror(i8* %15)
store i32 1, i32* %3
br label %46
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %6
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %7
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i64, i64* %7
%27 = call i32* @malloc(i64 %26)
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %31
30: ; preds = %17
store i32 1, i32* %9
br label %42
31: ; preds = %17
%32 = load i32*, i32** %8
%33 = load i64, i64* %7
%34 = load i32*, i32** %6
%35 = call i32 @fread(i32* %32, i32 1, i64 %33, i32* %34)
%36 = load i32*, i32** %8
%37 = load i64, i64* %7
%38 = load i32*, i32** %5
%39 = call i32 @fwrite(i32* %36, i32 1, i64 %37, i32* %38)
%40 = load i32*, i32** %8
%41 = call i32 @free(i32* %40)
br label %42
42: ; preds = %31, %30
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
%45 = load i32, i32* %9
store i32 %45, i32* %3
br label %46
46: ; preds = %42, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @append_file(i8* nocapture %0, i32* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* %0)
br label %22
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = tail call i32* @malloc(i64 %10)
%14 = icmp eq i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %7
%16 = tail call i32 @fread(i32* %13, i32 1, i64 %10, i32* %3)
%17 = tail call i32 @fwrite(i32* %13, i32 1, i64 %10, i32* %1)
%18 = tail call i32 @free(i32* %13)
br label %19
19: ; preds = %7, %15
%20 = phi i32 [ 0, %15 ], [ 1, %7 ]
%21 = tail call i32 @fclose(i32* %3)
br label %22
22: ; preds = %19, %5
%23 = phi i32 [ 1, %5 ], [ %20, %19 ]
ret i32 %23
}