#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 EPERM;
int O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ display_fat16(char *, int);
int getopt(int, char **, char *);
char *malloc(int);
int open(char *, int);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strdup(int /*<<< orphan*/);
scalar_t__ swap_buffer(char *, int);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ write_new_boot(char *, int, int, int);
int main(int argc, char *argv[]) {
  int c, devf, swap, bytes_to_read, rw, mode;
  char *dev;
  char *buf;
  rw = swap = 0;
  dev = NULL;
  bytes_to_read = 512;
  mode = O_RDONLY;
  while (-1 != (c = getopt(argc, argv, "d:hsw"))) {
    switch (c) {
    case 'h':
      usage();
      return 1;
    case 'd':
      dev = strdup(optarg);
      break;
    case 's':
      swap = 1;
      break;
    case 'w':
      rw = 1;
      mode = O_RDWR;
      break;
    }
  }
  if (NULL == dev) {
    usage();
    return 1;
  }
  if (swap && (bytes_to_read % 2)) {
    printf("With swap, even number of bytes must be used (%d).", bytes_to_read);
    return 1;
  }
  if (NULL == (buf = malloc(bytes_to_read + 2))) {
    printf("Unable to allocate for %d bytes\n", bytes_to_read);
    return 1;
  }
  if ((devf = open(dev, mode)) < 0) {
    if (EPERM == devf)
      printf(
          "You lack sufficient permissions to open '%s'. Perhaps use sudo?\n",
          dev);
    printf("Unable to open '%s'\n", dev);
    return 1;
  }
  if ((c = read(devf, buf, bytes_to_read)) != bytes_to_read) {
    printf("Unable to read %d bytes", bytes_to_read);
    return 1;
  }
  if (swap) {
    if (swap_buffer(buf, bytes_to_read)) {
      printf("swap buffer failed.\n");
      return 1;
    }
  }
  display_fat16(buf, bytes_to_read);
  if (rw) {
    write_new_boot(buf, bytes_to_read, devf, swap);
  }
  close(devf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x200,-0x20(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe53(%rip),%rdx
call   10c0 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     128b <main+0xdb>
mov    -0x14(%rbp),%eax
add    $0xffffff9c,%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x13,%eax
ja     1286 <main+0xd6>
mov    -0x40(%rbp),%rax
lea    0xdcc(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    1427 <main+0x277>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10d0 <strdup@plt>
mov    %rax,-0x30(%rbp)
jmp    1286 <main+0xd6>
movl   $0x1,-0x1c(%rbp)
jmp    1286 <main+0xd6>
movl   $0x1,-0x24(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11ef <main+0x3f>
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    12aa <main+0xfa>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    1427 <main+0x277>
cmpl   $0x0,-0x1c(%rbp)
je     12e5 <main+0x135>
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     12e5 <main+0x135>
mov    -0x20(%rbp),%esi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1427 <main+0x277>
mov    -0x20(%rbp),%edi
add    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    131f <main+0x16f>
mov    -0x20(%rbp),%esi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1427 <main+0x277>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1379 <main+0x1c9>
mov    0x2c82(%rip),%rax
mov    (%rax),%eax
cmp    -0x18(%rbp),%eax
jne    135b <main+0x1ab>
mov    -0x30(%rbp),%rsi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rsi
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1427 <main+0x277>
mov    -0x18(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    -0x20(%rbp),%eax
je     13b1 <main+0x201>
mov    -0x20(%rbp),%esi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1427 <main+0x277>
cmpl   $0x0,-0x1c(%rbp)
je     13f0 <main+0x240>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <swap_buffer@plt>
cmp    $0x0,%rax
je     13eb <main+0x23b>
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1427 <main+0x277>
jmp    13f0 <main+0x240>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1080 <display_fat16@plt>
cmpl   $0x0,-0x24(%rbp)
je     1418 <main+0x268>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   10a0 <write_new_boot@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2dfe(%rip),%rax
mov    (%rax),%r15d
xor    %eax,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%al
mov    %eax,0xc(%rsp)
lea    0xe5f(%rip),%r13
lea    0xe08(%rip),%r14
movl   $0x0,0x8(%rsp)
xor    %r12d,%r12d
jmp    1215 <main+0x55>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0xffffffff,%eax
je     126e <main+0xae>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r13,%rdx
call   10d0 <getopt@plt>
lea    -0x64(%rax),%ecx
cmp    $0x13,%ecx
ja     1210 <main+0x50>
movslq (%r14,%rcx,4),%rax
add    %r14,%rax
jmp    *%rax
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10e0 <strdup@plt>
mov    %rax,%r12
jmp    1215 <main+0x55>
mov    $0x1,%eax
mov    %rax,0x10(%rsp)
movl   $0x0,0xc(%rsp)
jmp    1215 <main+0x55>
mov    0x2d7f(%rip),%rax
mov    (%rax),%r15d
movl   $0x1,0x8(%rsp)
jmp    1215 <main+0x55>
test   %r12,%r12
je     12d7 <main+0x117>
mov    $0x202,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12f2 <main+0x132>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %r15d,%esi
call   10c0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12fb <main+0x13b>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
cmp    $0x200,%eax
jne    132a <main+0x16a>
testb  $0x1,0xc(%rsp)
jne    133f <main+0x17f>
mov    %rbp,%rdi
mov    $0x200,%esi
call   1070 <swap_buffer@plt>
test   %rax,%rax
je     133f <main+0x17f>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
jmp    12de <main+0x11e>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5d(%rip),%rdi
jmp    1331 <main+0x171>
mov    0x2cbe(%rip),%rax
cmp    %ebx,(%rax)
jne    1317 <main+0x157>
lea    0xd6a(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xd9a(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12de <main+0x11e>
lea    0xd9c(%rip),%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12de <main+0x11e>
mov    %rbp,%rdi
mov    $0x200,%esi
call   1090 <display_fat16@plt>
cmpl   $0x0,0x8(%rsp)
je     1367 <main+0x1a7>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %ebx,%edx
mov    0x10(%rsp),%rcx
call   10b0 <write_new_boot@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    12e3 <main+0x123>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [6 x i8] c"d:hsw\00"
@optarg = global i32 0
@O_RDWR = global i32 0
@.str.1 = [51 x i8] c"With swap, even number of bytes must be used (%d).\00"
@.str.2 = [33 x i8] c"Unable to allocate for %d bytes\0A\00"
@EPERM = global i32 0
@.str.3 = [65 x i8] c"You lack sufficient permissions to open '%s'. Perhaps use sudo?\0A\00"
@.str.4 = [21 x i8] c"Unable to open '%s'\0A\00"
@.str.5 = [24 x i8] c"Unable to read %d bytes\00"
@.str.6 = [21 x i8] c"swap buffer failed.\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 i32
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %10
store i8* null, i8** %12
store i32 512, i32* %9
%14 = load i32, i32* @O_RDONLY
store i32 %14, i32* %11
br label %15
15: ; preds = %30, %2
%16 = load i32, i32* %4
%17 = load i8**, i8*** %5
%18 = call i32 @getopt(i32 %16, i8** %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* %6
%19 = icmp ne i32 -1, %18
br i1 %19, label %20, label %31
20: ; preds = %15
%21 = load i32, i32* %6
switch i32 %21, label %30 [
i32 104, label %22
i32 100, label %24
i32 115, label %27
i32 119, label %28
]
22: ; preds = %20
%23 = call i32 (...) @usage()
store i32 1, i32* %3
br label %105
24: ; preds = %20
%25 = load i32, i32* @optarg
%26 = call i8* @strdup(i32 %25)
store i8* %26, i8** %12
br label %30
27: ; preds = %20
store i32 1, i32* %8
br label %30
28: ; preds = %20
store i32 1, i32* %10
%29 = load i32, i32* @O_RDWR
store i32 %29, i32* %11
br label %30
30: ; preds = %20, %28, %27, %24
br label %15
31: ; preds = %15
%32 = load i8*, i8** %12
%33 = icmp eq i8* null, %32
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (...) @usage()
store i32 1, i32* %3
br label %105
36: ; preds = %31
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32, i32* %9
%41 = srem i32 %40, 2
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* %9
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %44)
store i32 1, i32* %3
br label %105
46: ; preds = %39, %36
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 2
%49 = call i8* @malloc(i32 %48)
store i8* %49, i8** %13
%50 = icmp eq i8* null, %49
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = load i32, i32* %9
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %52)
store i32 1, i32* %3
br label %105
54: ; preds = %46
%55 = load i8*, i8** %12
%56 = load i32, i32* %11
%57 = call i32 @open(i8* %55, i32 %56)
store i32 %57, i32* %7
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %54
%60 = load i32, i32* @EPERM
%61 = load i32, i32* %7
%62 = icmp eq i32 %60, %61
br i1 %62, label %63, label %66
63: ; preds = %59
%64 = load i8*, i8** %12
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %63, %59
%67 = load i8*, i8** %12
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %67)
store i32 1, i32* %3
br label %105
69: ; preds = %54
%70 = load i32, i32* %7
%71 = load i8*, i8** %13
%72 = load i32, i32* %9
%73 = call i32 @read(i32 %70, i8* %71, i32 %72)
store i32 %73, i32* %6
%74 = load i32, i32* %9
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %79
76: ; preds = %69
%77 = load i32, i32* %9
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %77)
store i32 1, i32* %3
br label %105
79: ; preds = %69
%80 = load i32, i32* %8
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %90
82: ; preds = %79
%83 = load i8*, i8** %13
%84 = load i32, i32* %9
%85 = call i64 @swap_buffer(i8* %83, i32 %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %82
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %105
89: ; preds = %82
br label %90
90: ; preds = %89, %79
%91 = load i8*, i8** %13
%92 = load i32, i32* %9
%93 = call i32 @display_fat16(i8* %91, i32 %92)
%94 = load i32, i32* %10
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %102
96: ; preds = %90
%97 = load i8*, i8** %13
%98 = load i32, i32* %9
%99 = load i32, i32* %7
%100 = load i32, i32* %8
%101 = call i32 @write_new_boot(i8* %97, i32 %98, i32 %99, i32 %100)
br label %102
102: ; preds = %96, %90
%103 = load i32, i32* %7
%104 = call i32 @close(i32 %103)
store i32 0, i32* %3
br label %105
105: ; preds = %102, %87, %76, %66, %51, %43, %34, %22
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [6 x i8] c"d:hsw\00"
@optarg = global i32 0
@O_RDWR = global i32 0
@.str.2 = [33 x i8] c"Unable to allocate for %d bytes\0A\00"
@EPERM = global i32 0
@.str.3 = [65 x i8] c"You lack sufficient permissions to open '%s'. Perhaps use sudo?\0A\00"
@.str.4 = [21 x i8] c"Unable to open '%s'\0A\00"
@.str.5 = [24 x i8] c"Unable to read %d bytes\00"
@str = [20 x i8] c"swap buffer failed.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @O_RDONLY
br label %4
4: ; preds = %16, %2
%5 = phi i1 [ true, %2 ], [ false, %16 ]
%6 = phi i32 [ 0, %2 ], [ 1, %16 ]
%7 = phi i32 [ 0, %2 ], [ %11, %16 ]
%8 = phi i32 [ %3, %2 ], [ %12, %16 ]
%9 = phi i8* [ null, %2 ], [ %15, %16 ]
br label %10
10: ; preds = %4, %23
%11 = phi i32 [ %7, %4 ], [ 1, %23 ]
%12 = phi i32 [ %8, %4 ], [ %24, %23 ]
%13 = phi i8* [ %9, %4 ], [ %15, %23 ]
br label %14
14: ; preds = %10, %20
%15 = phi i8* [ %13, %10 ], [ %22, %20 ]
br label %16
16: ; preds = %14, %16
%17 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %17, label %16 [
i32 -1, label %25
i32 104, label %18
i32 100, label %20
i32 115, label %4
i32 119, label %23
]
18: ; preds = %16
%19 = tail call i32 (...) @usage()
br label %62
20: ; preds = %16
%21 = load i32, i32* @optarg
%22 = tail call i8* @strdup(i32 %21)
br label %14
23: ; preds = %16
%24 = load i32, i32* @O_RDWR
br label %10
25: ; preds = %16
%26 = icmp eq i8* %15, null
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 (...) @usage()
br label %62
29: ; preds = %25
%30 = tail call dereferenceable_or_null(514) i8* @malloc(i32 514)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 512)
br label %62
34: ; preds = %29
%35 = tail call i32 @open(i8* %15, i32 %12)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i32, i32* @EPERM
%39 = icmp eq i32 %38, %35
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %15)
br label %42
42: ; preds = %40, %37
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %15)
br label %62
44: ; preds = %34
%45 = tail call i32 @read(i32 %35, i8* %30, i32 512)
%46 = icmp eq i32 %45, 512
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 512)
br label %62
49: ; preds = %44
br i1 %5, label %55, label %50
50: ; preds = %49
%51 = tail call i64 @swap_buffer(i8* %30, i32 512)
%52 = icmp eq i64 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %50
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %62
55: ; preds = %50, %49
%56 = tail call i32 @display_fat16(i8* %30, i32 512)
%57 = icmp eq i32 %11, 0
br i1 %57, label %60, label %58
58: ; preds = %55
%59 = tail call i32 @write_new_boot(i8* %30, i32 512, i32 %35, i32 %6)
br label %60
60: ; preds = %58, %55
%61 = tail call i32 @close(i32 %35)
br label %62
62: ; preds = %60, %53, %47, %42, %32, %27, %18
%63 = phi i32 [ 1, %18 ], [ 1, %27 ], [ 1, %32 ], [ 1, %42 ], [ 1, %47 ], [ 1, %53 ], [ 0, %60 ]
ret i32 %63
}