#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*/ SEEK_SET;
int /*<<< orphan*/ lseek64(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int append_file(int size, int dst, int src) {
  char buf[size];
  int r;
  lseek64(src, 0, SEEK_SET);
  do {
    r = read(src, buf, size);
    if (r < 0)
      return r;
    r = write(dst, buf, r);
    if (r < 0)
      return r;
  } while (r);
  return 0;
}


x86 assembly (clang, -O0):
<append_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek64@plt>
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a8 <append_file+0x78>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11f3 <append_file+0xc3>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d6 <append_file+0xa6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11f3 <append_file+0xc3>
jmp    11db <append_file+0xab>
cmpl   $0x0,-0x24(%rbp)
jne    117a <append_file+0x4a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%r12d
mov    %edi,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1050 <lseek64@plt>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1197 <append_file+0x67>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1197 <append_file+0x67>
jne    1170 <append_file+0x40>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @append_file(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* %5
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = load i32, i32* %7
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek64(i32 %16, i32 0, i32 %17)
br label %19
19: ; preds = %36, %3
%20 = load i32, i32* %7
%21 = load i32, i32* %5
%22 = call i32 @read(i32 %20, i8* %15, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = load i32, i32* %10
store i32 %26, i32* %4
store i32 1, i32* %11
br label %40
27: ; preds = %19
%28 = load i32, i32* %6
%29 = load i32, i32* %10
%30 = call i32 @write(i32 %28, i8* %15, i32 %29)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = load i32, i32* %10
store i32 %34, i32* %4
store i32 1, i32* %11
br label %40
35: ; preds = %27
br label %36
36: ; preds = %35
%37 = load i32, i32* %10
%38 = icmp ne i32 %37, 0
br i1 %38, label %19, label %39
39: ; preds = %36
store i32 0, i32* %4
store i32 1, i32* %11
br label %40
40: ; preds = %39, %33, %25
%41 = load i8*, i8** %8
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @append_file(i32 %0, i32 %1, i32 %2) {
%4 = zext i32 %0 to i64
%5 = alloca i8, i64 %4
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek64(i32 %2, i32 0, i32 %6)
br label %8
8: ; preds = %14, %3
%9 = call i32 @read(i32 %2, i8* %5, i32 %0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = call i32 @write(i32 %1, i8* %5, i32 %9)
%13 = icmp slt i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = icmp eq i32 %12, 0
br i1 %15, label %16, label %8
16: ; preds = %14, %11, %8
%17 = phi i32 [ %9, %8 ], [ %12, %11 ], [ 0, %14 ]
ret i32 %17
}