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

/* Variables and functions */
int /*<<< orphan*/ HDIO_DRIVE_CMD;
int O_NONBLOCK;
int O_RDONLY;
int WIN_IDENTIFY;
unsigned char WIN_PIDENTIFY;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char **);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int readMBR(char *deviceName, unsigned short *data) {
  int fd;
  __u16 *id;
  unsigned char args[4 + 512] = {
      WIN_IDENTIFY,
      0,
      0,
      1,
  };
  unsigned i;
  fd = open(deviceName, O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    printf("Can't open %s\n", deviceName);
    return errno;
  }
  if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
    args[0] = WIN_PIDENTIFY;
    if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
      printf(" HDIO_DRIVE_CMD(identify) failed\n");
      return errno;
    }
  }
  id = (__u16 *)&args[4];
  for (i = 0; i < (256 / 8); ++i) {
    printf("%04x %04x %04x %04x %04x %04x %04x %04x\n", id[0], id[1], id[2],
           id[3], id[4], id[5], id[6], id[7]);
    id += 8;
  }
  memcpy((unsigned char *)data, &args[4], 512);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<readMBR>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x204,%edx
call   1040 <memset@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x230(%rbp)
movb   $0x1,-0x22d(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e7 <readMBR+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1322 <readMBR+0x1c2>
mov    -0x1c(%rbp),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    -0x230(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     125e <readMBR+0xfe>
mov    0x2db0(%rip),%rax
mov    (%rax),%al
mov    %al,-0x230(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    -0x230(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     1259 <readMBR+0xf9>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1322 <readMBR+0x1c2>
jmp    125e <readMBR+0xfe>
lea    -0x230(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x234(%rbp)
cmpl   $0x20,-0x234(%rbp)
jae    12fa <readMBR+0x19a>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%r8d
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%r9d
mov    -0x28(%rbp),%rax
mov    0x14(%rax),%r11d
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%r10d
mov    -0x28(%rbp),%rax
mov    0x1c(%rax),%eax
lea    0xd6b(%rip),%rdi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x28(%rbp)
mov    -0x234(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x234(%rbp)
jmp    1277 <readMBR+0x117>
mov    -0x18(%rbp),%rdi
lea    -0x230(%rbp),%rsi
add    $0x4,%rsi
mov    $0x200,%edx
call   1070 <memcpy@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMBR>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x204,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%al
mov    %al,(%rsp)
movb   $0x1,0x3(%rsp)
mov    0x2e32(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     120c <readMBR+0x9c>
mov    %eax,%r12d
mov    0x2e09(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
je     1228 <readMBR+0xb8>
mov    0x2dd9(%rip),%rax
mov    (%rax),%al
mov    %al,(%rsp)
mov    (%rbx),%esi
mov    %rsp,%rdx
mov    %r12d,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
je     1228 <readMBR+0xb8>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
jmp    121d <readMBR+0xad>
lea    0xded(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
jmp    12a1 <readMBR+0x131>
lea    0x4(%rsp),%r14
lea    0xddb(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    0x4(%rsp,%rbp,1),%esi
mov    0x8(%rsp,%rbp,1),%edx
mov    0xc(%rsp,%rbp,1),%ecx
mov    0x10(%rsp,%rbp,1),%r8d
mov    0x14(%rsp,%rbp,1),%r9d
mov    0x18(%rsp,%rbp,1),%r10d
mov    0x1c(%rsp,%rbp,1),%r11d
mov    0x20(%rsp,%rbp,1),%ebx
sub    $0x8,%rsp
mov    %r13,%rdi
xor    %eax,%eax
push   %rbx
push   %r11
push   %r10
call   1040 <printf@plt>
add    $0x20,%rsp
add    $0x20,%rbp
cmp    $0x400,%ebp
jne    1240 <readMBR+0xd0>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x200,%edx
call   1080 <memcpy@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@WIN_IDENTIFY = global i32 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@errno = global i32 0
@HDIO_DRIVE_CMD = global i32 0
@WIN_PIDENTIFY = global i8 0
@.str.1 = [34 x i8] c" HDIO_DRIVE_CMD(identify) failed\0A\00"
@.str.2 = [41 x i8] c"%04x %04x %04x %04x %04x %04x %04x %04x\0A\00"
define i32 @readMBR(i8* %0, i16* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [516 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i16* %1, i16** %5
%10 = bitcast [516 x i8]* %8 to i8*
%11 = getelementptr inbounds [516 x i8], [516 x i8]* %8, i64 0, i64 0
%12 = load i32, i32* @WIN_IDENTIFY
%13 = trunc i32 %12 to i8
store i8 %13, i8* %11
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8 1, i8* %16
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @errno
store i32 %27, i32* %3
br label %90
28: ; preds = %2
%29 = load i32, i32* %6
%30 = load i32, i32* @HDIO_DRIVE_CMD
%31 = bitcast [516 x i8]* %8 to i8**
%32 = call i64 @ioctl(i32 %29, i32 %30, i8** %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %28
%35 = load i8, i8* @WIN_PIDENTIFY
%36 = getelementptr inbounds [516 x i8], [516 x i8]* %8, i64 0, i64 0
store i8 %35, i8* %36
%37 = load i32, i32* %6
%38 = load i32, i32* @HDIO_DRIVE_CMD
%39 = bitcast [516 x i8]* %8 to i8**
%40 = call i64 @ioctl(i32 %37, i32 %38, i8** %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %34
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%44 = load i32, i32* @errno
store i32 %44, i32* %3
br label %90
45: ; preds = %34
br label %46
46: ; preds = %45, %28
%47 = getelementptr inbounds [516 x i8], [516 x i8]* %8, i64 0, i64 4
%48 = bitcast i8* %47 to i32*
store i32* %48, i32** %7
store i32 0, i32* %9
br label %49
49: ; preds = %80, %46
%50 = load i32, i32* %9
%51 = icmp ult i32 %50, 32
br i1 %51, label %52, label %83
52: ; preds = %49
%53 = load i32*, i32** %7
%54 = getelementptr inbounds i32, i32* %53, i64 0
%55 = load i32, i32* %54
%56 = load i32*, i32** %7
%57 = getelementptr inbounds i32, i32* %56, i64 1
%58 = load i32, i32* %57
%59 = load i32*, i32** %7
%60 = getelementptr inbounds i32, i32* %59, i64 2
%61 = load i32, i32* %60
%62 = load i32*, i32** %7
%63 = getelementptr inbounds i32, i32* %62, i64 3
%64 = load i32, i32* %63
%65 = load i32*, i32** %7
%66 = getelementptr inbounds i32, i32* %65, i64 4
%67 = load i32, i32* %66
%68 = load i32*, i32** %7
%69 = getelementptr inbounds i32, i32* %68, i64 5
%70 = load i32, i32* %69
%71 = load i32*, i32** %7
%72 = getelementptr inbounds i32, i32* %71, i64 6
%73 = load i32, i32* %72
%74 = load i32*, i32** %7
%75 = getelementptr inbounds i32, i32* %74, i64 7
%76 = load i32, i32* %75
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %55, i32 %58, i32 %61, i32 %64, i32 %67, i32 %70, i32 %73, i32 %76)
%78 = load i32*, i32** %7
%79 = getelementptr inbounds i32, i32* %78, i64 8
store i32* %79, i32** %7
br label %80
80: ; preds = %52
%81 = load i32, i32* %9
%82 = add i32 %81, 1
store i32 %82, i32* %9
br label %49
83: ; preds = %49
%84 = load i16*, i16** %5
%85 = bitcast i16* %84 to i8*
%86 = getelementptr inbounds [516 x i8], [516 x i8]* %8, i64 0, i64 4
%87 = call i32 @memcpy(i8* %85, i8* %86, i32 512)
%88 = load i32, i32* %6
%89 = call i32 @close(i32 %88)
store i32 0, i32* %3
br label %90
90: ; preds = %83, %42, %24
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@WIN_IDENTIFY = global i32 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [15 x i8] c"Can't open %s\0A\00"
@errno = global i32 0
@HDIO_DRIVE_CMD = global i32 0
@WIN_PIDENTIFY = global i8 0
@.str.2 = [41 x i8] c"%04x %04x %04x %04x %04x %04x %04x %04x\0A\00"
@str = [33 x i8] c" HDIO_DRIVE_CMD(identify) failed\00"
define i32 @readMBR(i8* %0, i16* %1) {
%3 = alloca [516 x i8]
%4 = getelementptr inbounds [516 x i8], [516 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @WIN_IDENTIFY
%6 = trunc i32 %5 to i8
store i8 %6, i8* %4
%7 = getelementptr inbounds [516 x i8], [516 x i8]* %3, i64 0, i64 3
store i8 1, i8* %7
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0)
%15 = load i32, i32* @errno
br label %58
16: ; preds = %2
%17 = load i32, i32* @HDIO_DRIVE_CMD
%18 = bitcast [516 x i8]* %3 to i8**
%19 = call i64 @ioctl(i32 %11, i32 %17, i8** %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %16
%22 = load i8, i8* @WIN_PIDENTIFY
store i8 %22, i8* %4
%23 = load i32, i32* @HDIO_DRIVE_CMD
%24 = call i64 @ioctl(i32 %11, i32 %23, i8** %18)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%28 = load i32, i32* @errno
br label %58
29: ; preds = %21, %16
%30 = getelementptr inbounds [516 x i8], [516 x i8]* %3, i64 0, i64 4
%31 = bitcast i8* %30 to i32*
br label %32
32: ; preds = %29, %32
%33 = phi i32 [ 0, %29 ], [ %52, %32 ]
%34 = phi i32* [ %31, %29 ], [ %51, %32 ]
%35 = load i32, i32* %34
%36 = getelementptr inbounds i32, i32* %34, i64 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds i32, i32* %34, i64 2
%39 = load i32, i32* %38
%40 = getelementptr inbounds i32, i32* %34, i64 3
%41 = load i32, i32* %40
%42 = getelementptr inbounds i32, i32* %34, i64 4
%43 = load i32, i32* %42
%44 = getelementptr inbounds i32, i32* %34, i64 5
%45 = load i32, i32* %44
%46 = getelementptr inbounds i32, i32* %34, i64 6
%47 = load i32, i32* %46
%48 = getelementptr inbounds i32, i32* %34, i64 7
%49 = load i32, i32* %48
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %37, i32 %39, i32 %41, i32 %43, i32 %45, i32 %47, i32 %49)
%51 = getelementptr inbounds i32, i32* %34, i64 8
%52 = add nuw nsw i32 %33, 1
%53 = icmp eq i32 %52, 32
br i1 %53, label %54, label %32
54: ; preds = %32
%55 = bitcast i16* %1 to i8*
%56 = call i32 @memcpy(i8* %55, i8* %30, i32 512)
%57 = call i32 @close(i32 %11)
br label %58
58: ; preds = %54, %26, %13
%59 = phi i32 [ %15, %13 ], [ %28, %26 ], [ 0, %54 ]
ret i32 %59
}