#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetsize(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ pad_file(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *flp;
  FILE *file_sys;
  FILE *curr_bin;
  int total_sectors = 0, i;
  int binaries[1024];
  if (argc < 4) {
    fprintf(stderr, "ERROR: Usage: build floppy_name, boot_bin, kernel_bin, "
                    "program_bins...\n");
    return -1;
  }
  int sectors;
  for (i = 2; i < argc; i++) {
    curr_bin = fopen(argv[i], "rw");
    if (curr_bin == NULL) {
      fprintf(stderr, "ERROR: Cannot open file %s\n", argv[i]);
      return -1;
    }
    if (i == 2 && fgetsize(curr_bin) != 512) {
      fprintf(stderr, "ERROR: Bootloader must be 512 bytes\n");
      return -1;
    }
    pad_file(curr_bin);
    sectors = fgetsize(curr_bin) / 512;
    total_sectors += sectors;
    binaries[i] = total_sectors;
    fclose(curr_bin);
  }
  printf("%d\n", total_sectors);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11ad <main+0x4d>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <main+0x157>
movl   $0x2,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12a8 <main+0x148>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1216 <main+0xb6>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <main+0x157>
cmpl   $0x2,-0x30(%rbp)
jne    1257 <main+0xf7>
mov    -0x28(%rbp),%rdi
call   1050 <fgetsize@plt>
cmp    $0x200,%eax
je     1257 <main+0xf7>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <main+0x157>
mov    -0x28(%rbp),%rdi
call   1070 <pad_file@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fgetsize@plt>
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1034(%rbp)
mov    -0x1034(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
movslq -0x30(%rbp),%rax
mov    %ecx,-0x1030(%rbp,%rax,4)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11b4 <main+0x54>
mov    -0x2c(%rbp),%esi
lea    0xdda(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x4,%edi
jge    1191 <main+0x31>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1254 <main+0xf4>
mov    %rsi,%r15
mov    %edi,%r12d
add    $0xfffffffffffffffe,%r12
xor    %ebp,%ebp
lea    0xea4(%rip),%r14
xor    %r13d,%r13d
jmp    11e2 <main+0x82>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1070 <pad_file@plt>
mov    %rbx,%rdi
call   1050 <fgetsize@plt>
lea    0x1ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x9,%ecx
add    %ecx,%r13d
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
je     1243 <main+0xe3>
mov    0x10(%r15,%rbp,8),%rdi
mov    %r14,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1220 <main+0xc0>
mov    %rax,%rbx
test   %rbp,%rbp
jne    11b0 <main+0x50>
mov    %rbx,%rdi
call   1050 <fgetsize@plt>
cmp    $0x200,%eax
je     11b0 <main+0x50>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
jmp    1180 <main+0x20>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    0x10(%r15,%rbp,8),%rdx
lea    0xe16(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1254 <main+0xf4>
lea    0xe42(%rip),%rdi
mov    %r13d,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [72 x i8] c"ERROR: Usage: build floppy_name, boot_bin, kernel_bin, program_bins...\0A\00"
@.str.1 = [3 x i8] c"rw\00"
@.str.2 = [28 x i8] c"ERROR: Cannot open file %s\0A\00"
@.str.3 = [37 x i8] c"ERROR: Bootloader must be 512 bytes\0A\00"
@.str.4 = [4 x i8] c"%d\0A\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 i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i32]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
18: ; preds = %2
store i32 2, i32* %10
br label %19
19: ; preds = %65, %18
%20 = load i32, i32* %10
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %68
23: ; preds = %19
%24 = load i8**, i8*** %5
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %8
%30 = load i32*, i32** %8
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %40
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = load i8**, i8*** %5
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 -1, i32* %3
br label %71
40: ; preds = %23
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, 2
br i1 %42, label %43, label %50
43: ; preds = %40
%44 = load i32*, i32** %8
%45 = call i32 @fgetsize(i32* %44)
%46 = icmp ne i32 %45, 512
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
50: ; preds = %43, %40
%51 = load i32*, i32** %8
%52 = call i32 @pad_file(i32* %51)
%53 = load i32*, i32** %8
%54 = call i32 @fgetsize(i32* %53)
%55 = sdiv i32 %54, 512
store i32 %55, i32* %12
%56 = load i32, i32* %12
%57 = load i32, i32* %9
%58 = add nsw i32 %57, %56
store i32 %58, i32* %9
%59 = load i32, i32* %9
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [1024 x i32], [1024 x i32]* %11, i64 0, i64 %61
store i32 %59, i32* %62
%63 = load i32*, i32** %8
%64 = call i32 @fclose(i32* %63)
br label %65
65: ; preds = %50
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %10
br label %19
68: ; preds = %19
%69 = load i32, i32* %9
%70 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %68, %47, %32, %15
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [72 x i8] c"ERROR: Usage: build floppy_name, boot_bin, kernel_bin, program_bins...\0A\00"
@.str.1 = [3 x i8] c"rw\00"
@.str.2 = [28 x i8] c"ERROR: Cannot open file %s\0A\00"
@.str.3 = [37 x i8] c"ERROR: Bootloader must be 512 bytes\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = zext i32 %0 to i64
br label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
br label %40
9: ; preds = %4, %30
%10 = phi i64 [ 2, %4 ], [ %36, %30 ]
%11 = phi i32 [ 0, %4 ], [ %34, %30 ]
%12 = getelementptr inbounds i8*, i8** %1, i64 %10
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = and i64 %10, 4294967295
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %18
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %40
22: ; preds = %9
%23 = icmp eq i64 %10, 2
br i1 %23, label %24, label %30
24: ; preds = %22
%25 = tail call i32 @fgetsize(i32* %14)
%26 = icmp eq i32 %25, 512
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %40
30: ; preds = %24, %22
%31 = tail call i32 @pad_file(i32* %14)
%32 = tail call i32 @fgetsize(i32* %14)
%33 = sdiv i32 %32, 512
%34 = add nsw i32 %33, %11
%35 = tail call i32 @fclose(i32* %14)
%36 = add nuw nsw i64 %10, 1
%37 = icmp eq i64 %36, %5
br i1 %37, label %38, label %9
38: ; preds = %30
%39 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %40
40: ; preds = %16, %27, %38, %6
%41 = phi i32 [ -1, %6 ], [ -1, %16 ], [ -1, %27 ], [ 0, %38 ]
ret i32 %41
}