#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
void copy_system(char const *filename, char *buf) {
  int c, i, fd;
  if ((fd = open(filename, O_RDONLY, 0)) < 0)
    die("Unable to open 'system'");
  for (i = 0; (c = read(fd, buf, 1024)) > 0; i += c)
    if (write(1, buf, c) != c)
      die("Write call failed");
  close(fd);
  fprintf(stderr, "'system' is %d bytes\n", i);
  if (i > 0x2000 * 16)
    die("'system' is too big");
}


x86 assembly (clang, -O0):
<copy_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    119c <copy_system+0x3c>
lea    0xe69(%rip),%rdi
call   1080 <die@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11f9 <copy_system+0x99>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     11e6 <copy_system+0x86>
lea    0xe37(%rip),%rdi
call   1080 <die@plt>
jmp    11eb <copy_system+0x8b>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a3 <copy_system+0x43>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe16(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x20000,-0x18(%rbp)
jle    1232 <copy_system+0xd2>
lea    0xe13(%rip),%rdi
call   1080 <die@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_system>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1190 <copy_system+0x30>
lea    0xe75(%rip),%rdi
call   1080 <die@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11f5 <copy_system+0x95>
mov    %eax,%ebp
xor    %ebx,%ebx
lea    0xe69(%rip),%r12
jmp    11d8 <copy_system+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %ebp,%ebx
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11f5 <copy_system+0x95>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11c0 <copy_system+0x60>
mov    %r12,%rdi
call   1080 <die@plt>
jmp    11c0 <copy_system+0x60>
mov    %r15d,%edi
call   1040 <close@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
cmp    $0x20001,%ebx
jl     1230 <copy_system+0xd0>
lea    0xe1d(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <die@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Unable to open 'system'\00"
@.str.1 = [18 x i8] c"Write call failed\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"'system' is %d bytes\0A\00"
@.str.3 = [20 x i8] c"'system' is too big\00"
define void @copy_system(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
store i32 0, i32* %6
br label %15
15: ; preds = %29, %14
%16 = load i32, i32* %7
%17 = load i8*, i8** %4
%18 = call i32 @read(i32 %16, i8* %17, i32 1024)
store i32 %18, i32* %5
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = call i32 @write(i32 1, i8* %21, i32 %22)
%24 = load i32, i32* %5
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
br label %29
29: ; preds = %28
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = add nsw i32 %31, %30
store i32 %32, i32* %6
br label %15
33: ; preds = %15
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* @stderr
%37 = load i32, i32* %6
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %6
%40 = icmp sgt i32 %39, 131072
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %33
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Unable to open 'system'\00"
@.str.1 = [18 x i8] c"Write call failed\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"'system' is %d bytes\0A\00"
@.str.3 = [20 x i8] c"'system' is too big\00"
define void @copy_system(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = tail call i32 @read(i32 %4, i8* %1, i32 1024)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %8, %18
%12 = phi i32 [ %20, %18 ], [ %9, %8 ]
%13 = phi i32 [ %19, %18 ], [ 0, %8 ]
%14 = tail call i32 @write(i32 1, i8* %1, i32 %12)
%15 = icmp eq i32 %14, %12
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %11, %16
%19 = add nsw i32 %12, %13
%20 = tail call i32 @read(i32 %4, i8* %1, i32 1024)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %11, label %22
22: ; preds = %18, %8
%23 = phi i32 [ 0, %8 ], [ %19, %18 ]
%24 = tail call i32 @close(i32 %4)
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%27 = icmp sgt i32 %23, 131072
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %22
ret void
}