#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_setup(char const *filename, char *buf) {
  int c, i, fd;
  if ((fd = open(filename, O_RDONLY, 0)) < 0)
    die("Unable to open 'setup'");
  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, "'setup' is %d bytes\n", i);
  if (i > 4 * 512)
    die("'setup' exceeds "
        "SETUP_SECTS"
        " sectors");
  for (c = 0; c < 1024; c++)
    buf[c] = 0;
  while (i < 4 * 512) {
    c = 4 * 512 - i;
    if (c > 1024)
      c = 1024;
    if (write(1, buf, c) != c)
      die("Write call failed");
    i += c;
  }
}


x86 assembly (clang, -O0):
<copy_setup>:
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_setup+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_setup+0x99>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     11e6 <copy_setup+0x86>
lea    0xe36(%rip),%rdi
call   1080 <die@plt>
jmp    11eb <copy_setup+0x8b>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a3 <copy_setup+0x43>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe15(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x800,-0x18(%rbp)
jle    1232 <copy_setup+0xd2>
lea    0xe11(%rip),%rdi
call   1080 <die@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x400,-0x14(%rbp)
jge    1260 <copy_setup+0x100>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1239 <copy_setup+0xd9>
jmp    1265 <copy_setup+0x105>
cmpl   $0x800,-0x18(%rbp)
jge    12c5 <copy_setup+0x165>
mov    $0x800,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x400,-0x14(%rbp)
jle    1291 <copy_setup+0x131>
movl   $0x400,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     12b7 <copy_setup+0x157>
lea    0xd65(%rip),%rdi
call   1080 <die@plt>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1265 <copy_setup+0x105>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_setup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11a0 <copy_setup+0x30>
lea    0xe65(%rip),%rdi
call   1090 <die@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1205 <copy_setup+0x95>
mov    %eax,%ebx
xor    %ebp,%ebp
lea    0xe58(%rip),%r12
jmp    11e8 <copy_setup+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %ebx,%ebp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1205 <copy_setup+0x95>
mov    $0x1,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11d0 <copy_setup+0x60>
mov    %r12,%rdi
call   1090 <die@plt>
jmp    11d0 <copy_setup+0x60>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
mov    %ebp,%edx
call   1070 <fprintf@plt>
cmp    $0x801,%ebp
jl     1249 <copy_setup+0xd9>
lea    0xe0b(%rip),%rdi
call   1090 <die@plt>
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
jmp    1260 <copy_setup+0xf0>
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
cmp    $0x800,%ebp
jne    1269 <copy_setup+0xf9>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x400,%r12d
lea    0xda1(%rip),%r14
jmp    128a <copy_setup+0x11a>
nopl   0x0(%rax,%rax,1)
add    %ebx,%ebp
cmp    $0x800,%ebp
jge    1260 <copy_setup+0xf0>
mov    $0x800,%ebx
sub    %ebp,%ebx
cmp    $0x400,%ebx
cmovae %r12d,%ebx
mov    $0x1,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1280 <copy_setup+0x110>
mov    %r14,%rdi
call   1090 <die@plt>
jmp    1280 <copy_setup+0x110>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [23 x i8] c"Unable to open 'setup'\00"
@.str.1 = [18 x i8] c"Write call failed\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"'setup' is %d bytes\0A\00"
@.str.3 = [36 x i8] c"'setup' exceeds SETUP_SECTS sectors\00"
define void @copy_setup(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 ([23 x i8], [23 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 ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %6
%40 = icmp sgt i32 %39, 2048
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %33
store i32 0, i32* %5
br label %44
44: ; preds = %52, %43
%45 = load i32, i32* %5
%46 = icmp slt i32 %45, 1024
br i1 %46, label %47, label %55
47: ; preds = %44
%48 = load i8*, i8** %4
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %47
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %44
55: ; preds = %44
br label %56
56: ; preds = %73, %55
%57 = load i32, i32* %6
%58 = icmp slt i32 %57, 2048
br i1 %58, label %59, label %77
59: ; preds = %56
%60 = load i32, i32* %6
%61 = sub nsw i32 2048, %60
store i32 %61, i32* %5
%62 = load i32, i32* %5
%63 = icmp sgt i32 %62, 1024
br i1 %63, label %64, label %65
64: ; preds = %59
store i32 1024, i32* %5
br label %65
65: ; preds = %64, %59
%66 = load i8*, i8** %4
%67 = load i32, i32* %5
%68 = call i32 @write(i32 1, i8* %66, i32 %67)
%69 = load i32, i32* %5
%70 = icmp ne i32 %68, %69
br i1 %70, label %71, label %73
71: ; preds = %65
%72 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %73
73: ; preds = %71, %65
%74 = load i32, i32* %5
%75 = load i32, i32* %6
%76 = add nsw i32 %75, %74
store i32 %76, i32* %6
br label %56
77: ; preds = %56
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [23 x i8] c"Unable to open 'setup'\00"
@.str.1 = [18 x i8] c"Write call failed\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"'setup' is %d bytes\0A\00"
@.str.3 = [36 x i8] c"'setup' exceeds SETUP_SECTS sectors\00"
define void @copy_setup(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 ([23 x i8], [23 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 ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%27 = icmp sgt i32 %23, 2048
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %44
30: ; preds = %22
%31 = icmp eq i32 %23, 2048
br i1 %31, label %44, label %32
32: ; preds = %30, %41
%33 = phi i32 [ %42, %41 ], [ %23, %30 ]
%34 = sub nsw i32 2048, %33
%35 = icmp ult i32 %34, 1024
%36 = select i1 %35, i32 %34, i32 1024
%37 = tail call i32 @write(i32 1, i8* %1, i32 %36)
%38 = icmp eq i32 %37, %36
br i1 %38, label %41, label %39
39: ; preds = %32
%40 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
%42 = add nsw i32 %36, %33
%43 = icmp slt i32 %42, 2048
br i1 %43, label %32, label %44
44: ; preds = %41, %28, %30
ret void
}