#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  printf("*************** Boot Sign Utility ***************\n");
  off_t size;
  int fd;
  if (argc != 2) {
    return 1;
  }
  fd = open(argv[1], O_RDWR);
  assert(fd > -1);
  size = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  if (size < 510) {
    printf("Boot sector size: %d\n", (int)size);
    char val[] = {0x55, 0xAA};
    lseek(fd, 510, SEEK_SET);
    write(fd, val, 2);
  } else if (size == 512) {
    char sign[2];
    lseek(fd, 510, SEEK_SET);
    read(fd, sign, 2);
    if (sign[0] == (char)0x55 && sign[1] == (char)0xAA) {
      printf("Boot sector is already signed.\n");
      return 0;
    }
    printf("ERROR: Boot sector is too large!\n");
    return 1;
  } else {
    printf("Error: Boot sector is too large!\n");
    printf("Size: %d bytes\n", (int)size);
    return 1;
  }
  printf("Boot signature complete.\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
je     11aa <main+0x3a>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x1ac>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x18(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmpl   $0x1fe,-0x14(%rbp)
jge    1252 <main+0xe2>
mov    -0x14(%rbp),%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0xeb5(%rip),%ax
mov    %ax,-0x1a(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    $0x1fe,%esi
call   1050 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
jmp    12ff <main+0x18f>
cmpl   $0x200,-0x14(%rbp)
jne    12d4 <main+0x164>
mov    -0x18(%rbp),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    $0x1fe,%esi
call   1050 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x2,%edx
call   1070 <read@plt>
movsbl -0x1c(%rbp),%eax
cmp    $0x55,%eax
jne    12ba <main+0x14a>
movsbl -0x1b(%rbp),%eax
cmp    $0xffffffaa,%eax
jne    12ba <main+0x14a>
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    131c <main+0x1ac>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x1ac>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131c <main+0x1ac>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r15d
cmp    $0x2,%ebx
jne    12c0 <main+0x140>
mov    0x8(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1090 <assert@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%r14d
mov    0x2dea(%rip),%rbx
mov    (%rbx),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0x1fd,%r14d
jg     1246 <main+0xc6>
lea    0xdff(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movw   $0xaa55,0x6(%rsp)
mov    (%rbx),%edx
mov    %ebp,%edi
mov    $0x1fe,%esi
call   1060 <lseek@plt>
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <write@plt>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12c0 <main+0x140>
cmp    $0x200,%r14d
jne    129d <main+0x11d>
mov    (%rbx),%edx
mov    %ebp,%edi
mov    $0x1fe,%esi
call   1060 <lseek@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%al
mov    0x5(%rsp),%cl
xor    $0x55,%al
xor    $0xaa,%cl
xor    %r15d,%r15d
or     %al,%cl
setne  %r15b
lea    0xe0f(%rip),%rax
lea    0xde7(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
jmp    12c0 <main+0x140>
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
lea    0xd66(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"*************** Boot Sign Utility ***************\0A\00"
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [22 x i8] c"Boot sector size: %d\0A\00"
@__const.main.val = [2 x i8] c"U\AA"
@.str.2 = [32 x i8] c"Boot sector is already signed.\0A\00"
@.str.3 = [34 x i8] c"ERROR: Boot sector is too large!\0A\00"
@.str.4 = [34 x i8] c"Error: Boot sector is too large!\0A\00"
@.str.5 = [16 x i8] c"Size: %d bytes\0A\00"
@.str.6 = [26 x i8] c"Boot signature complete.\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 [2 x i8]
%9 = alloca [2 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 1, i32* %3
br label %73
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, -1
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* %7
%25 = load i32, i32* @SEEK_END
%26 = call i32 @lseek(i32 %24, i32 0, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %7
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @lseek(i32 %27, i32 0, i32 %28)
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 510
br i1 %31, label %32, label %42
32: ; preds = %14
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = bitcast [2 x i8]* %8 to i8*
%36 = load i32, i32* %7
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @lseek(i32 %36, i32 510, i32 %37)
%39 = load i32, i32* %7
%40 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%41 = call i32 @write(i32 %39, i8* %40, i32 2)
br label %69
42: ; preds = %14
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, 512
br i1 %44, label %45, label %65
45: ; preds = %42
%46 = load i32, i32* %7
%47 = load i32, i32* @SEEK_SET
%48 = call i32 @lseek(i32 %46, i32 510, i32 %47)
%49 = load i32, i32* %7
%50 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 2)
%52 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 85
br i1 %55, label %56, label %63
56: ; preds = %45
%57 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 1
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, -86
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %73
63: ; preds = %56, %45
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %73
65: ; preds = %42
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%67 = load i32, i32* %6
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %67)
store i32 1, i32* %3
br label %73
69: ; preds = %32
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %69, %65, %63, %61, %13
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [22 x i8] c"Boot sector size: %d\0A\00"
@.str.5 = [16 x i8] c"Size: %d bytes\0A\00"
@str = [50 x i8] c"*************** Boot Sign Utility ***************\00"
@str.7 = [33 x i8] c"Error: Boot sector is too large!\00"
@str.8 = [33 x i8] c"ERROR: Boot sector is too large!\00"
@str.9 = [31 x i8] c"Boot sector is already signed.\00"
@str.10 = [25 x i8] c"Boot signature complete.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = alloca [2 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%6 = icmp eq i32 %0, 2
br i1 %6, label %7, label %48
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = xor i32 %11, -1
%13 = lshr i32 %12, 31
%14 = tail call i32 @assert(i32 %13)
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @lseek(i32 %11, i32 0, i32 %15)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @lseek(i32 %11, i32 0, i32 %17)
%19 = icmp slt i32 %16, 510
br i1 %19, label %20, label %28
20: ; preds = %7
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%22 = bitcast i16* %3 to i8*
store i16 -21931, i16* %3
%23 = load i32, i32* @SEEK_SET
%24 = tail call i32 @lseek(i32 %11, i32 510, i32 %23)
%25 = call i32 @write(i32 %11, i8* %22, i32 2)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.10, i64 0, i64 0))
%27 = tail call i32 @close(i32 %11)
br label %48
28: ; preds = %7
%29 = icmp eq i32 %16, 512
br i1 %29, label %30, label %45
30: ; preds = %28
%31 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @lseek(i32 %11, i32 510, i32 %32)
%34 = call i32 @read(i32 %11, i8* %31, i32 2)
%35 = load i8, i8* %31
%36 = icmp eq i8 %35, 85
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, -86
%40 = select i1 %36, i1 %39, i1 false
%41 = select i1 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @str.9, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0)
%42 = xor i1 %40, true
%43 = zext i1 %42 to i32
%44 = tail call i32 @puts(i8* dereferenceable(1) %41)
br label %48
45: ; preds = %28
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %16)
br label %48
48: ; preds = %2, %20, %45, %30
%49 = phi i32 [ 0, %20 ], [ %43, %30 ], [ 1, %45 ], [ 1, %2 ]
ret i32 %49
}