#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 void FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(void *);
int /*<<< orphan*/ feof(void *);
void *fopen(char *, char *);
int fread(char *, int, int, void *);
int /*<<< orphan*/ fwrite(char *, int, int, void *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argnr, char *args[]) {
  FILE *output, *input;
  int i, bytes_read, sectors_read, bytes_from_file;
  char buffer[512];
  if (argnr < 4) {
    printf("Invalid number of parameters.\n\n");
    printf("USAGE: %s [output] [input 1] [input 2] ... [input n]\n", args[0]);
    printf("Example: %s a.img bootsect.bin kernel.bin\n", args[0]);
    exit(0);
  }
  output = fopen(args[1], "r");
  if (output != ((void *)0)) {
    buffer[0] = 0;
    fclose(output);
  }
  output = fopen(args[1], "wb");
  for (i = 2; i < argnr; i++) {
    input = fopen(args[i], "rb");
    if (input == ((void *)0)) {
      printf("Missing input file %s. Aborting operation...\n", args[i]);
      fclose(output);
      exit(1);
    }
    bytes_read = 512;
    bytes_from_file = 0;
    sectors_read = 0;
    while (bytes_read == 512 && !feof(input)) {
      bytes_read = fread(buffer, 1, 512, input);
      if (bytes_read == 0)
        break;
      if (bytes_read != 512)
        memset(buffer + bytes_read, 0, 512 - bytes_read);
      sectors_read++;
      fwrite(buffer, 1, 512, output);
      bytes_from_file += bytes_read;
    }
    printf("%d sectors, %d bytes read from file %s...\n", sectors_read,
           bytes_from_file, args[i]);
    fclose(input);
  }
  fclose(output);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11e2 <main+0x62>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe90(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1215 <main+0x95>
movb   $0x0,-0x230(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    13a6 <main+0x226>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe33(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1294 <main+0x114>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x200,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x200,-0x28(%rbp)
mov    %al,-0x231(%rbp)
jne    12d5 <main+0x155>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x231(%rbp)
mov    -0x231(%rbp),%al
test   $0x1,%al
jne    12e8 <main+0x168>
jmp    136f <main+0x1ef>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1030 <fread@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1314 <main+0x194>
jmp    136f <main+0x1ef>
cmpl   $0x200,-0x28(%rbp)
je     133e <main+0x1be>
lea    -0x230(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    $0x200,%edx
sub    -0x28(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   10a0 <fwrite@plt>
mov    -0x28(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    12a9 <main+0x129>
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1234 <main+0xb4>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jle    1339 <main+0x1a9>
mov    %edi,%ebx
mov    0x8(%r14),%rdi
lea    0xea7(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11d1 <main+0x41>
movb   $0x0,0x20(%rsp)
mov    %rax,%rdi
call   1050 <fclose@plt>
mov    0x8(%r14),%rdi
lea    0xe87(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,(%rsp)
mov    %ebx,%eax
mov    %rax,0x18(%rsp)
mov    $0x2,%ebx
lea    0x20(%rsp),%r12
mov    %r14,0x8(%rsp)
jmp    1239 <main+0xa9>
nopl   (%rax)
mov    0x8(%rsp),%r14
mov    0x10(%rsp),%rbx
mov    (%r14,%rbx,8),%rcx
lea    0xe82(%rip),%rdi
mov    %r13d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
add    $0x1,%rbx
cmp    0x18(%rsp),%rbx
je     12ed <main+0x15d>
mov    %rbx,0x10(%rsp)
mov    (%r14,%rbx,8),%rdi
lea    0xe1d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     130a <main+0x17a>
mov    %rax,%rbp
xor    %r15d,%r15d
xor    %r13d,%r13d
mov    (%rsp),%rbx
jmp    1299 <main+0x109>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r13d
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
add    %r14d,%r15d
cmp    $0x200,%r14d
jne    1200 <main+0x70>
mov    %rbp,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    1200 <main+0x70>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
mov    %eax,%r14d
cmp    $0x200,%eax
je     1270 <main+0xe0>
test   %r14d,%r14d
je     1200 <main+0x70>
movslq %r14d,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x20,%rdi
mov    $0x200,%edx
sub    %r14d,%edx
xor    %esi,%esi
call   1070 <memset@plt>
jmp    1270 <main+0xe0>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rax
mov    0x10(%rsp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd4a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd82(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%rsi
lea    0xcb1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r14),%rsi
lea    0xcd6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Invalid number of parameters.\0A\0A\00"
@.str.1 = [54 x i8] c"USAGE: %s [output] [input 1] [input 2] ... [input n]\0A\00"
@.str.2 = [43 x i8] c"Example: %s a.img bootsect.bin kernel.bin\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.6 = [46 x i8] c"Missing input file %s. Aborting operation...\0A\00"
@.str.7 = [43 x i8] c"%d sectors, %d bytes read from file %s...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %26
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i8* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
store i8 0, i8* %34
%35 = load i8*, i8** %6
%36 = call i32 @fclose(i8* %35)
br label %37
37: ; preds = %33, %26
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i8* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i8* %41, i8** %6
store i32 2, i32* %8
br label %42
42: ; preds = %114, %37
%43 = load i32, i32* %8
%44 = load i32, i32* %4
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %117
46: ; preds = %42
%47 = load i8**, i8*** %5
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8*, i8** %47, i64 %49
%51 = load i8*, i8** %50
%52 = call i8* @fopen(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i8* %52, i8** %7
%53 = load i8*, i8** %7
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %65
55: ; preds = %46
%56 = load i8**, i8*** %5
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i8* %60)
%62 = load i8*, i8** %6
%63 = call i32 @fclose(i8* %62)
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %46
store i32 512, i32* %9
store i32 0, i32* %11
store i32 0, i32* %10
br label %66
66: ; preds = %94, %65
%67 = load i32, i32* %9
%68 = icmp eq i32 %67, 512
br i1 %68, label %69, label %74
69: ; preds = %66
%70 = load i8*, i8** %7
%71 = call i32 @feof(i8* %70)
%72 = icmp ne i32 %71, 0
%73 = xor i1 %72, true
br label %74
74: ; preds = %69, %66
%75 = phi i1 [ false, %66 ], [ %73, %69 ]
br i1 %75, label %76, label %103
76: ; preds = %74
%77 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%78 = load i8*, i8** %7
%79 = call i32 @fread(i8* %77, i32 1, i32 512, i8* %78)
store i32 %79, i32* %9
%80 = load i32, i32* %9
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %76
br label %103
83: ; preds = %76
%84 = load i32, i32* %9
%85 = icmp ne i32 %84, 512
br i1 %85, label %86, label %94
86: ; preds = %83
%87 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%88 = load i32, i32* %9
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8, i8* %87, i64 %89
%91 = load i32, i32* %9
%92 = sub nsw i32 512, %91
%93 = call i32 @memset(i8* %90, i32 0, i32 %92)
br label %94
94: ; preds = %86, %83
%95 = load i32, i32* %10
%96 = add nsw i32 %95, 1
store i32 %96, i32* %10
%97 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%98 = load i8*, i8** %6
%99 = call i32 @fwrite(i8* %97, i32 1, i32 512, i8* %98)
%100 = load i32, i32* %9
%101 = load i32, i32* %11
%102 = add nsw i32 %101, %100
store i32 %102, i32* %11
br label %66
103: ; preds = %82, %74
%104 = load i32, i32* %10
%105 = load i32, i32* %11
%106 = load i8**, i8*** %5
%107 = load i32, i32* %8
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i8*, i8** %106, i64 %108
%110 = load i8*, i8** %109
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %104, i32 %105, i8* %110)
%112 = load i8*, i8** %7
%113 = call i32 @fclose(i8* %112)
br label %114
114: ; preds = %103
%115 = load i32, i32* %8
%116 = add nsw i32 %115, 1
store i32 %116, i32* %8
br label %42
117: ; preds = %42
%118 = load i8*, i8** %6
%119 = call i32 @fclose(i8* %118)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [54 x i8] c"USAGE: %s [output] [input 1] [input 2] ... [input n]\0A\00"
@.str.2 = [43 x i8] c"Example: %s a.img bootsect.bin kernel.bin\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [3 x i8] c"rb\00"
@.str.6 = [46 x i8] c"Missing input file %s. Aborting operation...\0A\00"
@.str.7 = [43 x i8] c"%d sectors, %d bytes read from file %s...\0A\00"
@str = [31 x i8] c"Invalid number of parameters.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i8* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %13
store i8 0, i8* %4
%19 = tail call i32 @fclose(i8* %16)
br label %20
20: ; preds = %13, %18
%21 = load i8*, i8** %14
%22 = tail call i8* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%23 = zext i32 %0 to i64
br label %24
24: ; preds = %20, %52
%25 = phi i64 [ 2, %20 ], [ %58, %52 ]
%26 = getelementptr inbounds i8*, i8** %1, i64 %25
%27 = load i8*, i8** %26
%28 = call i8* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i8*, i8** %26
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i8* %31)
%33 = call i32 @fclose(i8* %22)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %24, %47
%36 = phi i32 [ %50, %47 ], [ 0, %24 ]
%37 = phi i32 [ %48, %47 ], [ 0, %24 ]
%38 = call i32 @feof(i8* %28)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %35
%41 = call i32 @fread(i8* %4, i32 1, i32 512, i8* %28)
switch i32 %41, label %42 [
i32 0, label %52
i32 512, label %47
]
42: ; preds = %40
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %43
%45 = sub nsw i32 512, %41
%46 = call i32 @memset(i8* %44, i32 0, i32 %45)
br label %47
47: ; preds = %40, %42
%48 = add nuw nsw i32 %37, 1
%49 = call i32 @fwrite(i8* %4, i32 1, i32 512, i8* %22)
%50 = add nsw i32 %41, %36
%51 = icmp eq i32 %41, 512
br i1 %51, label %35, label %52
52: ; preds = %40, %47, %35
%53 = phi i32 [ %37, %40 ], [ %48, %47 ], [ %37, %35 ]
%54 = phi i32 [ %36, %40 ], [ %50, %47 ], [ %36, %35 ]
%55 = load i8*, i8** %26
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %53, i32 %54, i8* %55)
%57 = call i32 @fclose(i8* %28)
%58 = add nuw nsw i64 %25, 1
%59 = icmp eq i64 %58, %23
br i1 %59, label %60, label %24
60: ; preds = %52
%61 = call i32 @fclose(i8* %22)
ret i32 0
}