#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*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sendfile(int, int, int /*<<< orphan*/ *, int);
int append_file(int ofd, char *filename, off_t pos) {
  lseek(ofd, pos, SEEK_SET);
  int fd = open(filename, O_RDONLY);
  int size = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  sendfile(ofd, fd, NULL, size);
  close(fd);
  return size;
}


x86 assembly (clang, -O0):
<append_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e71(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1050 <sendfile@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e7c(%rip),%r15
mov    (%r15),%eax
mov    %edx,%esi
mov    %eax,%edx
call   1030 <lseek@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,%ebp
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %r14d,%edi
mov    %ebx,%esi
xor    %edx,%edx
mov    %ebp,%ecx
call   1050 <sendfile@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
define i32 @append_file(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = load i32, i32* %6
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @lseek(i32 %9, i32 %10, i32 %11)
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = load i32, i32* @SEEK_END
%18 = call i32 @lseek(i32 %16, i32 0, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %7
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @lseek(i32 %19, i32 0, i32 %20)
%22 = load i32, i32* %4
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = call i32 @sendfile(i32 %22, i32 %23, i32* null, i32 %24)
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %8
ret i32 %28
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
define i32 @append_file(i32 %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @SEEK_SET
%5 = tail call i32 @lseek(i32 %0, i32 %2, i32 %4)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %1, i32 %6)
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @lseek(i32 %7, i32 0, i32 %8)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %7, i32 0, i32 %10)
%12 = tail call i32 @sendfile(i32 %0, i32 %7, i32* null, i32 %9)
%13 = tail call i32 @close(i32 %7)
ret i32 %9
}