#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*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned short *, int);
int main(int argc, char **argv) {
  unsigned short int buffer[256];
  int i, fd;
  if (argc != 2) {
    printf(
        "Convertor from binary sector image 512 byte to C-source text to "
        "stdout. sizeof short int=%li bytes\r\nUsage: toc bootsector.bin\r\n",
        sizeof(unsigned short int));
    return 1;
  }
  for (i = 0; i < 256; i++)
    buffer[i] = 0;
  fd = open(argv[1], 0);
  if (fd == -1) {
    printf("Can't open '%s'\r\n", argv[1]);
    return 2;
  }
  i = read(fd, buffer, 512);
  if (i != 512) {
    printf("Read error '%s'\r\n", argv[1]);
    return 3;
  }
  close(fd);
  printf("/* Boot sector for boot from HDD */\r\n\r\nunsigned short int "
         "boothdd [256] = {");
  for (i = 0; i < 256; i++) {
    printf("0x%X", buffer[i]);
    if (i < 255)
      printf(",");
  }
  printf("};\r\n\r\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1182 <main+0x42>
lea    0xe96(%rip),%rdi
mov    $0x2,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <main+0x1a9>
movl   $0x0,-0x214(%rbp)
cmpl   $0x100,-0x214(%rbp)
jge    11c1 <main+0x81>
movslq -0x214(%rbp),%rax
movw   $0x0,-0x210(%rbp,%rax,2)
mov    -0x214(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x214(%rbp)
jmp    118c <main+0x4c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x218(%rbp)
cmpl   $0xffffffff,-0x218(%rbp)
jne    1205 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12e9 <main+0x1a9>
mov    -0x218(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x200,-0x214(%rbp)
je     1254 <main+0x114>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    12e9 <main+0x1a9>
mov    -0x218(%rbp),%edi
call   1040 <close@plt>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x214(%rbp)
cmpl   $0x100,-0x214(%rbp)
jge    12db <main+0x19b>
movslq -0x214(%rbp),%rax
movzwl -0x210(%rbp,%rax,2),%esi
ff
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xff,-0x214(%rbp)
jge    12c2 <main+0x182>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c7 <main+0x187>
mov    -0x214(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x214(%rbp)
jmp    1277 <main+0x137>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
cmp    $0x2,%edi
jne    1235 <main+0xc5>
mov    %rsi,%rbx
mov    %rsp,%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     124f <main+0xdf>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1080 <read@plt>
cmp    $0x200,%eax
jne    1268 <main+0xf8>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xed2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzwl (%rsp),%esi
lea    0xf0c(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x2c,%edi
call   1030 <putchar@plt>
movzwl (%rsp,%rbp,2),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    $0x100,%rbp
jne    1200 <main+0x90>
lea    0xec9(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    127f <main+0x10f>
lea    0xdc4(%rip),%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    127f <main+0x10f>
mov    0x8(%rbx),%rsi
lea    0xe26(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%eax
jmp    127f <main+0x10f>
mov    0x8(%rbx),%rsi
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [128 x i8] c"Convertor from binary sector image 512 byte to C-source text to stdout. sizeof short int=%li bytes\0D\0AUsage: toc bootsector.bin\0D\0A\00"
@.str.1 = [18 x i8] c"Can't open '%s'\0D\0A\00"
@.str.2 = [18 x i8] c"Read error '%s'\0D\0A\00"
@.str.3 = [76 x i8] c"/* Boot sector for boot from HDD */\0D\0A\0D\0Aunsigned short int boothdd [256] = {\00"
@.str.4 = [5 x i8] c"0x%X\00"
@.str.5 = [2 x i8] c",\00"
@.str.6 = [7 x i8] c"};\0D\0A\0D\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i16]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @.str, i64 0, i64 0), i64 2)
store i32 1, i32* %3
br label %71
13: ; preds = %2
store i32 0, i32* %7
br label %14
14: ; preds = %21, %13
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 256
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [256 x i16], [256 x i16]* %6, i64 0, i64 %19
store i16 0, i16* %20
br label %21
21: ; preds = %17
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
br label %14
24: ; preds = %14
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @open(i8* %27, i32 0)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %24
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 2, i32* %3
br label %71
36: ; preds = %24
%37 = load i32, i32* %8
%38 = getelementptr inbounds [256 x i16], [256 x i16]* %6, i64 0, i64 0
%39 = call i32 @read(i32 %37, i16* %38, i32 512)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp ne i32 %40, 512
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %45)
store i32 3, i32* %3
br label %71
47: ; preds = %36
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %7
br label %51
51: ; preds = %66, %47
%52 = load i32, i32* %7
%53 = icmp slt i32 %52, 256
br i1 %53, label %54, label %69
54: ; preds = %51
%55 = load i32, i32* %7
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [256 x i16], [256 x i16]* %6, i64 0, i64 %56
%58 = load i16, i16* %57
%59 = zext i16 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %7
%62 = icmp slt i32 %61, 255
br i1 %62, label %63, label %65
63: ; preds = %54
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %63, %54
br label %66
66: ; preds = %65
%67 = load i32, i32* %7
%68 = add nsw i32 %67, 1
store i32 %68, i32* %7
br label %51
69: ; preds = %51
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %71
71: ; preds = %69, %42, %31, %11
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [128 x i8] c"Convertor from binary sector image 512 byte to C-source text to stdout. sizeof short int=%li bytes\0D\0AUsage: toc bootsector.bin\0D\0A\00"
@.str.1 = [18 x i8] c"Can't open '%s'\0D\0A\00"
@.str.2 = [18 x i8] c"Read error '%s'\0D\0A\00"
@.str.3 = [76 x i8] c"/* Boot sector for boot from HDD */\0D\0A\0D\0Aunsigned short int boothdd [256] = {\00"
@.str.4 = [5 x i8] c"0x%X\00"
@str = [6 x i8] c"};\0D\0A\0D\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i16]
%4 = bitcast [256 x i16]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @open(i8* %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %13, label %16
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([128 x i8], [128 x i8]* @.str, i64 0, i64 0), i64 2)
br label %41
13: ; preds = %6
%14 = load i8*, i8** %7
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %41
16: ; preds = %6
%17 = getelementptr inbounds [256 x i16], [256 x i16]* %3, i64 0, i64 0
%18 = call i32 @read(i32 %9, i16* %17, i32 512)
%19 = icmp eq i32 %18, 512
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %41
23: ; preds = %16
%24 = tail call i32 @close(i32 %9)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([76 x i8], [76 x i8]* @.str.3, i64 0, i64 0))
%26 = getelementptr inbounds [256 x i16], [256 x i16]* %3, i64 0, i64 0
%27 = load i16, i16* %26
%28 = zext i16 %27 to i32
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %23, %30
%31 = phi i64 [ 0, %23 ], [ %33, %30 ]
%32 = tail call i32 @putchar(i32 44)
%33 = add nuw nsw i64 %31, 1
%34 = getelementptr inbounds [256 x i16], [256 x i16]* %3, i64 0, i64 %33
%35 = load i16, i16* %34
%36 = zext i16 %35 to i32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = icmp eq i64 %33, 255
br i1 %38, label %39, label %30
39: ; preds = %30
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %39, %20, %13, %11
%42 = phi i32 [ 1, %11 ], [ 2, %13 ], [ 3, %20 ], [ 0, %39 ]
ret i32 %42
}