#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 st_size;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ fstat(int, struct stat *);
char *malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  int nwritten;
  char *buf;
  struct stat st;
  if (argc != 3) {
    printf("Usage: %s read_filename write_filename\n", argv[0]);
    exit(EXIT_SUCCESS);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("open: ");
    exit(EXIT_FAILURE);
  }
  if (fstat(fd, &st)) {
    perror("stat");
    exit(EXIT_FAILURE);
  }
  buf = malloc(st.st_size);
  if (buf == NULL) {
    fprintf(stderr, "allocation failure with size %d\n", st.st_size);
    exit(EXIT_FAILURE);
  }
  nwritten = read(fd, buf, st.st_size);
  if (nwritten != st.st_size) {
    perror("read");
    exit(EXIT_FAILURE);
  }
  close(fd);
  fd = open(argv[2], O_CREAT | O_RDWR, 0666);
  if (fd < 0) {
    perror("open (writing)");
    exit(EXIT_FAILURE);
  }
  nwritten = write(fd, buf, st.st_size);
  if (nwritten != st.st_size) {
    perror("write");
    exit(EXIT_FAILURE);
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11e1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1220 <main+0x80>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
je     1250 <main+0xb0>
lea    0xdf2(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    128d <main+0xed>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xdba(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     12c5 <main+0x125>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
mov    0x2cfb(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    131a <main+0x17a>
lea    0xd53(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     1352 <main+0x1b2>
lea    0xd2a(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2c57(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x3,%edi
jne    1268 <main+0xc8>
mov    0x8(%r14),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1285 <main+0xe5>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
jne    128e <main+0xee>
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1297 <main+0xf7>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
cmp    %ebx,%eax
jne    12b0 <main+0x110>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x10(%r14),%rdi
mov    0x2dd8(%rip),%rax
mov    0x2db9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     12b9 <main+0x119>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    12c2 <main+0x122>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd8e(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xd9c(%rip),%rdi
jmp    12c9 <main+0x129>
lea    0xd9a(%rip),%rdi
jmp    12c9 <main+0x129>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
mov    %ebx,%edx
call   1070 <fprintf@plt>
jmp    12ce <main+0x12e>
lea    0xd9e(%rip),%rdi
jmp    12c9 <main+0x129>
lea    0xd9a(%rip),%rdi
jmp    12c9 <main+0x129>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [40 x i8] c"Usage: %s read_filename write_filename\0A\00"
@EXIT_SUCCESS = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open: \00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"allocation failure with size %d\0A\00"
@.str.4 = [5 x i8] c"read\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.5 = [15 x i8] c"open (writing)\00"
@.str.6 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_SUCCESS
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %19
%32 = load i32, i32* %6
%33 = call i64 @fstat(i32 %32, %struct.stat* %9)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %31
%40 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %8
%43 = load i8*, i8** %8
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %52
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %39
%53 = load i32, i32* %6
%54 = load i8*, i8** %8
%55 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @read(i32 %53, i8* %54, i32 %56)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%60 = load i32, i32* %59
%61 = icmp ne i32 %58, %60
br i1 %61, label %62, label %66
62: ; preds = %52
%63 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %52
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 2
%71 = load i8*, i8** %70
%72 = load i32, i32* @O_CREAT
%73 = load i32, i32* @O_RDWR
%74 = or i32 %72, %73
%75 = call i32 (i8*, i32, ...) @open(i8* %71, i32 %74, i32 438)
store i32 %75, i32* %6
%76 = load i32, i32* %6
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %66
%79 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %66
%83 = load i32, i32* %6
%84 = load i8*, i8** %8
%85 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%86 = load i32, i32* %85
%87 = call i32 @write(i32 %83, i8* %84, i32 %86)
store i32 %87, i32* %7
%88 = load i32, i32* %7
%89 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%90 = load i32, i32* %89
%91 = icmp ne i32 %88, %90
br i1 %91, label %92, label %96
92: ; preds = %82
%93 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%94 = load i32, i32* @EXIT_FAILURE
%95 = call i32 @exit(i32 %94)
unreachable
96: ; preds = %82
%97 = load i32, i32* %6
%98 = call i32 @close(i32 %97)
%99 = load i32, i32* @EXIT_SUCCESS
ret i32 %99
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [40 x i8] c"Usage: %s read_filename write_filename\0A\00"
@EXIT_SUCCESS = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open: \00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"allocation failure with size %d\0A\00"
@.str.4 = [5 x i8] c"read\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.5 = [15 x i8] c"open (writing)\00"
@.str.6 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_SUCCESS
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %11
%22 = call i64 @fstat(i32 %15, %struct.stat* %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i8* @malloc(i32 %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %28
%39 = tail call i32 @read(i32 %15, i8* %31, i32 %30)
%40 = icmp eq i32 %39, %30
br i1 %40, label %45, label %41
41: ; preds = %38
%42 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %38
%46 = tail call i32 @close(i32 %15)
%47 = getelementptr inbounds i8*, i8** %1, i64 2
%48 = load i8*, i8** %47
%49 = load i32, i32* @O_CREAT
%50 = load i32, i32* @O_RDWR
%51 = or i32 %50, %49
%52 = tail call i32 (i8*, i32, ...) @open(i8* %48, i32 %51, i32 438)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %45
%55 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = tail call i32 @exit(i32 %56)
unreachable
58: ; preds = %45
%59 = tail call i32 @write(i32 %52, i8* %31, i32 %30)
%60 = icmp eq i32 %59, %30
br i1 %60, label %65, label %61
61: ; preds = %58
%62 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = tail call i32 @exit(i32 %63)
unreachable
65: ; preds = %58
%66 = tail call i32 @close(i32 %52)
%67 = load i32, i32* @EXIT_SUCCESS
ret i32 %67
}