#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*/ EXIT_FAILURE;
int O_APPEND;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free_resources(int *, int);
int open(char *, int);
int /*<<< orphan*/ open_error_exit();
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char **, int);
scalar_t__ write(int, char **, scalar_t__);
void append_file(char *src, char *dst) {
  int fds[1024];
  int ind = 0;
  int rfd = open(src, O_RDONLY);
  if (rfd < 0)
    open_error_exit();
  int wfd = open(dst, O_WRONLY | O_APPEND);
  if (wfd < 0) {
    close(rfd);
    open_error_exit();
  }
  fds[ind++] = rfd;
  fds[ind++] = wfd;
  char buffer[4096];
  ssize_t bytes;
  while ((bytes = read(rfd, &buffer, 4096)) > 0) {
    ssize_t written = write(wfd, &buffer, bytes);
    if (bytes != written) {
      free_resources(fds, ind - 1);
      perror("Writing to file");
      exit(EXIT_FAILURE);
    }
  }
  free_resources(fds, ind - 1);
}


x86 assembly (clang, -O0):
<append_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1014(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    11c9 <append_file+0x49>
mov    $0x0,%al
call   10a0 <open_error_exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    1209 <append_file+0x89>
mov    -0x1018(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   10a0 <open_error_exit@plt>
mov    -0x1018(%rbp),%ecx
mov    -0x1014(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x1014(%rbp)
cltq
mov    %ecx,-0x1010(%rbp,%rax,4)
mov    -0x101c(%rbp),%ecx
mov    -0x1014(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x1014(%rbp)
cltq
mov    %ecx,-0x1010(%rbp,%rax,4)
mov    -0x1018(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %rax,-0x2028(%rbp)
cmp    $0x0,%rax
jle    12d9 <append_file+0x159>
mov    -0x101c(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x2028(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x2030(%rbp)
mov    -0x2028(%rbp),%rax
cmp    -0x2030(%rbp),%rax
je     12d4 <append_file+0x154>
lea    -0x1010(%rbp),%rdi
mov    -0x1014(%rbp),%esi
sub    $0x1,%esi
call   1040 <free_resources@plt>
lea    0xd3f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1249 <append_file+0xc9>
lea    -0x1010(%rbp),%rdi
mov    -0x1014(%rbp),%esi
sub    $0x1,%esi
call   1040 <free_resources@plt>
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%rbp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11ac <append_file+0x2c>
xor    %eax,%eax
call   10a0 <open_error_exit@plt>
mov    0x2e15(%rip),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11dc <append_file+0x5c>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
call   10a0 <open_error_exit@plt>
mov    %r14d,(%rsp)
mov    %r15d,0x4(%rsp)
lea    0x1000(%rsp),%rbx
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1242 <append_file+0xc2>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rax,%rbp
je     11f0 <append_file+0x70>
mov    %rsp,%rdi
mov    $0x1,%esi
call   1040 <free_resources@plt>
lea    0xdd1(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
call   1040 <free_resources@plt>
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str = [16 x i8] c"Writing to file\00"
@EXIT_FAILURE = global i32 0
define void @append_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4096 x i8]
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (...) @open_error_exit()
br label %19
19: ; preds = %17, %2
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_APPEND
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
%30 = call i32 (...) @open_error_exit()
br label %31
31: ; preds = %27, %19
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %35
store i32 %32, i32* %36
%37 = load i32, i32* %8
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %40
store i32 %37, i32* %41
br label %42
42: ; preds = %63, %31
%43 = load i32, i32* %7
%44 = bitcast [4096 x i8]* %9 to i8**
%45 = call i64 @read(i32 %43, i8** %44, i32 4096)
store i64 %45, i64* %10
%46 = icmp sgt i64 %45, 0
br i1 %46, label %47, label %64
47: ; preds = %42
%48 = load i32, i32* %8
%49 = bitcast [4096 x i8]* %9 to i8**
%50 = load i64, i64* %10
%51 = call i64 @write(i32 %48, i8** %49, i64 %50)
store i64 %51, i64* %11
%52 = load i64, i64* %10
%53 = load i64, i64* %11
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %63
55: ; preds = %47
%56 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%57 = load i32, i32* %6
%58 = sub nsw i32 %57, 1
%59 = call i32 @free_resources(i32* %56, i32 %58)
%60 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %47
br label %42
64: ; preds = %42
%65 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%66 = load i32, i32* %6
%67 = sub nsw i32 %66, 1
%68 = call i32 @free_resources(i32* %65, i32 %67)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str = [16 x i8] c"Writing to file\00"
@EXIT_FAILURE = global i32 0
define void @append_file(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [1024 x i32]
%4 = alloca [4096 x i8]
%5 = bitcast [1024 x i32]* %3 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (...) @open_error_exit()
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_APPEND
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %1, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @close(i32 %7)
%19 = tail call i32 (...) @open_error_exit()
br label %20
20: ; preds = %17, %11
%21 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 0
store i32 %7, i32* %21
%22 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 1
store i32 %15, i32* %22
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%24 = bitcast [4096 x i8]* %4 to i8**
br label %25
25: ; preds = %28, %20
%26 = call i64 @read(i32 %7, i8** %24, i32 4096)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = call i64 @write(i32 %15, i8** %24, i64 %26)
%30 = icmp eq i64 %26, %29
br i1 %30, label %25, label %31
31: ; preds = %28
%32 = call i32 @free_resources(i32* %21, i32 1)
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %25
%37 = call i32 @free_resources(i32* %21, i32 1)
ret void
}