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

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fstat(int, struct stat *);
void *malloc(int /*<<< orphan*/);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, void *, int /*<<< orphan*/);
void copy_file(const char *source_file, const char *target_file) {
  struct stat file_stat;
  int source_fd, target_fd;
  void *file_contents;
  source_fd = open(source_file, O_RDONLY, 0);
  target_fd = open(target_file, O_CREAT | O_WRONLY, S_IRWXU);
  fstat(source_fd, &file_stat);
  file_contents = malloc(file_stat.st_size);
  read(source_fd, file_contents, file_stat.st_size);
  write(target_fd, file_contents, file_stat.st_size);
  close(source_fd);
  close(target_fd);
  free(file_contents);
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <read@plt>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%r15d
mov    0x2e64(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %r15d,%edi
call   1090 <fstat@plt>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRWXU
%17 = call i32 @open(i8* %12, i32 %15, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %6
%19 = call i32 @fstat(i32 %18, %struct.stat* %5)
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %8
%23 = load i32, i32* %6
%24 = load i8*, i8** %8
%25 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @read(i32 %23, i8* %24, i32 %26)
%28 = load i32, i32* %7
%29 = load i8*, i8** %8
%30 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @write(i32 %28, i8* %29, i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
%37 = load i8*, i8** %8
%38 = call i32 @free(i8* %37)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
define void @copy_file(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5, i32 0)
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRWXU
%11 = tail call i32 @open(i8* %1, i32 %9, i32 %10)
%12 = call i32 @fstat(i32 %6, %struct.stat* %3)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i8* @malloc(i32 %14)
%16 = tail call i32 @read(i32 %6, i8* %15, i32 %14)
%17 = tail call i32 @write(i32 %11, i8* %15, i32 %14)
%18 = tail call i32 @close(i32 %6)
%19 = tail call i32 @close(i32 %11)
%20 = tail call i32 @free(i8* %15)
ret void
}