#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*/ error(char *);
char inportb(int);
short inportw(int);
int /*<<< orphan*/ outportb(int, int);
void readhdd(unsigned char *location, unsigned long LBA,
             unsigned char sectorcount) {
  outportb(0x1F6, 0xE0 | ((LBA >> 24) & 0x0F));
  outportb(0x1F1, 0);
  outportb(0x1F2, sectorcount);
  outportb(0x1F3, (unsigned char)LBA);
  outportb(0x1F4, (unsigned char)(LBA >> 8));
  outportb(0x1F5, (unsigned char)(LBA >> 16));
  outportb(0x1F7, 0x20);
  while (1) {
    char val = inportb(0x1F7);
    if ((val >> 0) & 1) {
      char val = inportb(0x1F7);
      if (val & 0x80) {
        error("HDD: Bad sector");
      } else if (val & 0x40) {
        error("HDD: Uncorrectable data");
      } else if (val & 0x20) {
        error("HDD: No media");
      } else if (val & 0x10) {
        error("HDD: ID mark not found");
      } else if (val & 0x08) {
        error("HDD: No media");
      } else if (val & 0x04) {
        error("HDD: Command aborted");
      } else if (val & 0x02) {
        error("HDD: Track 0 not found");
      } else if (val & 0x01) {
        error("HDD: No address mark");
      } else {
        error("HDD error bit 0 is set");
      }
    }
    if ((val >> 3) & 1) {
      break;
    }
  }
  int i = 0;
  int z = 0;
  for (i = 0; i < (512 / 2); i++) {
    short d = inportw(0x1F0);
    location[z++] = d;
    location[z++] = (unsigned char)(d >> 8);
  }
}


x86 assembly (clang, -O0):
<readhdd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rax
shr    $0x18,%rax
and    $0xf,%rax
or     $0xe0,%rax
mov    %eax,%esi
mov    $0x1f6,%edi
call   1060 <outportb@plt>
mov    $0x1f1,%edi
xor    %esi,%esi
call   1060 <outportb@plt>
movzbl -0x11(%rbp),%esi
mov    $0x1f2,%edi
call   1060 <outportb@plt>
mov    -0x10(%rbp),%rax
movzbl %al,%esi
mov    $0x1f3,%edi
call   1060 <outportb@plt>
mov    -0x10(%rbp),%rax
shr    $0x8,%rax
movzbl %al,%esi
mov    $0x1f4,%edi
call   1060 <outportb@plt>
mov    -0x10(%rbp),%rax
shr    $0x10,%rax
movzbl %al,%esi
mov    $0x1f5,%edi
call   1060 <outportb@plt>
mov    $0x1f7,%edi
mov    $0x20,%esi
call   1060 <outportb@plt>
mov    $0x1f7,%edi
call   1040 <inportb@plt>
mov    %al,-0x12(%rbp)
movsbl -0x12(%rbp),%eax
sar    $0x0,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1342 <readhdd+0x202>
mov    $0x1f7,%edi
call   1040 <inportb@plt>
mov    %al,-0x13(%rbp)
movsbl -0x13(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     1227 <readhdd+0xe7>
lea    0xde3(%rip),%rdi
call   1050 <error@plt>
jmp    133d <readhdd+0x1fd>
movsbl -0x13(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     1248 <readhdd+0x108>
lea    0xdd2(%rip),%rdi
call   1050 <error@plt>
jmp    1338 <readhdd+0x1f8>
movsbl -0x13(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     1269 <readhdd+0x129>
lea    0xdc9(%rip),%rdi
call   1050 <error@plt>
jmp    1333 <readhdd+0x1f3>
movsbl -0x13(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
je     128a <readhdd+0x14a>
lea    0xdb6(%rip),%rdi
call   1050 <error@plt>
jmp    132e <readhdd+0x1ee>
movsbl -0x13(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     12ab <readhdd+0x16b>
lea    0xd87(%rip),%rdi
call   1050 <error@plt>
jmp    1329 <readhdd+0x1e9>
movsbl -0x13(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     12cc <readhdd+0x18c>
lea    0xd8b(%rip),%rdi
call   1050 <error@plt>
jmp    1324 <readhdd+0x1e4>
movsbl -0x13(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     12ed <readhdd+0x1ad>
lea    0xd7f(%rip),%rdi
call   1050 <error@plt>
jmp    131f <readhdd+0x1df>
movsbl -0x13(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     130e <readhdd+0x1ce>
lea    0xd75(%rip),%rdi
call   1050 <error@plt>
jmp    131a <readhdd+0x1da>
lea    0xd79(%rip),%rdi
call   1050 <error@plt>
jmp    131f <readhdd+0x1df>
jmp    1324 <readhdd+0x1e4>
jmp    1329 <readhdd+0x1e9>
jmp    132e <readhdd+0x1ee>
jmp    1333 <readhdd+0x1f3>
jmp    1338 <readhdd+0x1f8>
jmp    133d <readhdd+0x1fd>
jmp    1342 <readhdd+0x202>
movsbl -0x12(%rbp),%eax
sar    $0x3,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     135a <readhdd+0x21a>
jmp    135f <readhdd+0x21f>
jmp    11d7 <readhdd+0x97>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x100,-0x18(%rbp)
jge    13d6 <readhdd+0x296>
mov    $0x1f0,%edi
call   1030 <inportw@plt>
mov    %ax,-0x1e(%rbp)
mov    -0x1e(%rbp),%ax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movswl -0x1e(%rbp),%eax
sar    $0x8,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1374 <readhdd+0x234>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readhdd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
shr    $0x18,%esi
and    $0xf,%esi
or     $0xe0,%esi
mov    $0x1f6,%edi
call   1060 <outportb@plt>
mov    $0x1f1,%edi
xor    %esi,%esi
call   1060 <outportb@plt>
mov    $0x1f2,%edi
mov    %ebp,%esi
call   1060 <outportb@plt>
movzbl %bl,%esi
mov    $0x1f3,%edi
call   1060 <outportb@plt>
movzbl %bh,%esi
mov    $0x1f4,%edi
call   1060 <outportb@plt>
shr    $0x10,%ebx
movzbl %bl,%esi
mov    $0x1f5,%edi
call   1060 <outportb@plt>
mov    $0x1f7,%edi
mov    $0x20,%esi
call   1060 <outportb@plt>
lea    0xe3f(%rip),%r15
lea    0xe48(%rip),%r12
lea    0xe59(%rip),%r13
lea    0xe60(%rip),%rbp
jmp    11ea <readhdd+0xaa>
nopl   0x0(%rax,%rax,1)
call   1050 <error@plt>
test   $0x8,%bl
jne    1251 <readhdd+0x111>
mov    $0x1f7,%edi
call   1040 <inportb@plt>
mov    %eax,%ebx
test   $0x1,%al
je     11e5 <readhdd+0xa5>
mov    $0x1f7,%edi
call   1040 <inportb@plt>
mov    %r15,%rdi
test   $0x80,%al
jne    11e0 <readhdd+0xa0>
mov    %r12,%rdi
test   $0x40,%al
jne    11e0 <readhdd+0xa0>
mov    %r13,%rdi
test   $0x20,%al
jne    11e0 <readhdd+0xa0>
mov    %rbp,%rdi
test   $0x10,%al
jne    11e0 <readhdd+0xa0>
mov    %r13,%rdi
test   $0x8,%al
jne    11e0 <readhdd+0xa0>
lea    0xe1f(%rip),%rdi
test   $0x4,%al
jne    11e0 <readhdd+0xa0>
lea    0xe29(%rip),%rdi
test   $0x2,%al
jne    11e0 <readhdd+0xa0>
lea    0xe4a(%rip),%rdi
test   $0x1,%al
je     11e0 <readhdd+0xa0>
lea    0xe2a(%rip),%rdi
jmp    11e0 <readhdd+0xa0>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1f0,%edi
call   1030 <inportw@plt>
mov    %ax,(%r14,%rbx,2)
add    $0x1,%rbx
cmp    $0x100,%ebx
jne    1260 <readhdd+0x120>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"HDD: Bad sector\00"
@.str.1 = [24 x i8] c"HDD: Uncorrectable data\00"
@.str.2 = [14 x i8] c"HDD: No media\00"
@.str.3 = [23 x i8] c"HDD: ID mark not found\00"
@.str.4 = [21 x i8] c"HDD: Command aborted\00"
@.str.5 = [23 x i8] c"HDD: Track 0 not found\00"
@.str.6 = [21 x i8] c"HDD: No address mark\00"
@.str.7 = [23 x i8] c"HDD error bit 0 is set\00"
define void @readhdd(i8* %0, i64 %1, i8 zeroext %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
%11 = alloca i16
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8 %2, i8* %6
%12 = load i64, i64* %5
%13 = lshr i64 %12, 24
%14 = and i64 %13, 15
%15 = or i64 224, %14
%16 = trunc i64 %15 to i32
%17 = call i32 @outportb(i32 502, i32 %16)
%18 = call i32 @outportb(i32 497, i32 0)
%19 = load i8, i8* %6
%20 = zext i8 %19 to i32
%21 = call i32 @outportb(i32 498, i32 %20)
%22 = load i64, i64* %5
%23 = trunc i64 %22 to i8
%24 = zext i8 %23 to i32
%25 = call i32 @outportb(i32 499, i32 %24)
%26 = load i64, i64* %5
%27 = lshr i64 %26, 8
%28 = trunc i64 %27 to i8
%29 = zext i8 %28 to i32
%30 = call i32 @outportb(i32 500, i32 %29)
%31 = load i64, i64* %5
%32 = lshr i64 %31, 16
%33 = trunc i64 %32 to i8
%34 = zext i8 %33 to i32
%35 = call i32 @outportb(i32 501, i32 %34)
%36 = call i32 @outportb(i32 503, i32 32)
br label %37
37: ; preds = %3, %118
%38 = call signext i8 @inportb(i32 503)
store i8 %38, i8* %7
%39 = load i8, i8* %7
%40 = sext i8 %39 to i32
%41 = ashr i32 %40, 0
%42 = and i32 %41, 1
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %111
44: ; preds = %37
%45 = call signext i8 @inportb(i32 503)
store i8 %45, i8* %8
%46 = load i8, i8* %8
%47 = sext i8 %46 to i32
%48 = and i32 %47, 128
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %110
52: ; preds = %44
%53 = load i8, i8* %8
%54 = sext i8 %53 to i32
%55 = and i32 %54, 64
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %109
59: ; preds = %52
%60 = load i8, i8* %8
%61 = sext i8 %60 to i32
%62 = and i32 %61, 32
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %108
66: ; preds = %59
%67 = load i8, i8* %8
%68 = sext i8 %67 to i32
%69 = and i32 %68, 16
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %107
73: ; preds = %66
%74 = load i8, i8* %8
%75 = sext i8 %74 to i32
%76 = and i32 %75, 8
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %73
%79 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %106
80: ; preds = %73
%81 = load i8, i8* %8
%82 = sext i8 %81 to i32
%83 = and i32 %82, 4
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %80
%86 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %105
87: ; preds = %80
%88 = load i8, i8* %8
%89 = sext i8 %88 to i32
%90 = and i32 %89, 2
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %87
%93 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %104
94: ; preds = %87
%95 = load i8, i8* %8
%96 = sext i8 %95 to i32
%97 = and i32 %96, 1
%98 = icmp ne i32 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %94
%100 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %103
101: ; preds = %94
%102 = call i32 @error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %103
103: ; preds = %101, %99
br label %104
104: ; preds = %103, %92
br label %105
105: ; preds = %104, %85
br label %106
106: ; preds = %105, %78
br label %107
107: ; preds = %106, %71
br label %108
108: ; preds = %107, %64
br label %109
109: ; preds = %108, %57
br label %110
110: ; preds = %109, %50
br label %111
111: ; preds = %110, %37
%112 = load i8, i8* %7
%113 = sext i8 %112 to i32
%114 = ashr i32 %113, 3
%115 = and i32 %114, 1
%116 = icmp ne i32 %115, 0
br i1 %116, label %117, label %118
117: ; preds = %111
br label %119
118: ; preds = %111
br label %37
119: ; preds = %117
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %9
br label %120
120: ; preds = %141, %119
%121 = load i32, i32* %9
%122 = icmp slt i32 %121, 256
br i1 %122, label %123, label %144
123: ; preds = %120
%124 = call signext i16 @inportw(i32 496)
store i16 %124, i16* %11
%125 = load i16, i16* %11
%126 = trunc i16 %125 to i8
%127 = load i8*, i8** %4
%128 = load i32, i32* %10
%129 = add nsw i32 %128, 1
store i32 %129, i32* %10
%130 = sext i32 %128 to i64
%131 = getelementptr inbounds i8, i8* %127, i64 %130
store i8 %126, i8* %131
%132 = load i16, i16* %11
%133 = sext i16 %132 to i32
%134 = ashr i32 %133, 8
%135 = trunc i32 %134 to i8
%136 = load i8*, i8** %4
%137 = load i32, i32* %10
%138 = add nsw i32 %137, 1
store i32 %138, i32* %10
%139 = sext i32 %137 to i64
%140 = getelementptr inbounds i8, i8* %136, i64 %139
store i8 %135, i8* %140
br label %141
141: ; preds = %123
%142 = load i32, i32* %9
%143 = add nsw i32 %142, 1
store i32 %143, i32* %9
br label %120
144: ; preds = %120
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"HDD: Bad sector\00"
@.str.1 = [24 x i8] c"HDD: Uncorrectable data\00"
@.str.2 = [14 x i8] c"HDD: No media\00"
@.str.3 = [23 x i8] c"HDD: ID mark not found\00"
@.str.4 = [21 x i8] c"HDD: Command aborted\00"
@.str.5 = [23 x i8] c"HDD: Track 0 not found\00"
@.str.6 = [21 x i8] c"HDD: No address mark\00"
@.str.7 = [23 x i8] c"HDD error bit 0 is set\00"
define void @readhdd(i8* nocapture writeonly %0, i64 %1, i8 zeroext %2) {
%4 = trunc i64 %1 to i32
%5 = lshr i32 %4, 24
%6 = and i32 %5, 15
%7 = or i32 %6, 224
%8 = tail call i32 @outportb(i32 502, i32 %7)
%9 = tail call i32 @outportb(i32 497, i32 0)
%10 = zext i8 %2 to i32
%11 = tail call i32 @outportb(i32 498, i32 %10)
%12 = and i32 %4, 255
%13 = tail call i32 @outportb(i32 499, i32 %12)
%14 = lshr i32 %4, 8
%15 = and i32 %14, 255
%16 = tail call i32 @outportb(i32 500, i32 %15)
%17 = lshr i32 %4, 16
%18 = and i32 %17, 255
%19 = tail call i32 @outportb(i32 501, i32 %18)
%20 = tail call i32 @outportb(i32 503, i32 32)
br label %21
21: ; preds = %56, %3
%22 = tail call signext i8 @inportb(i32 503)
%23 = zext i8 %22 to i32
%24 = and i32 %23, 1
%25 = icmp eq i32 %24, 0
br i1 %25, label %56, label %26
26: ; preds = %21
%27 = tail call signext i8 @inportb(i32 503)
%28 = zext i8 %27 to i32
%29 = and i32 %28, 128
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %53
31: ; preds = %26
%32 = and i32 %28, 64
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %53
34: ; preds = %31
%35 = and i32 %28, 32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %53
37: ; preds = %34
%38 = and i32 %28, 16
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %53
40: ; preds = %37
%41 = and i32 %28, 8
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %40
%44 = and i32 %28, 4
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = and i32 %28, 2
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = and i32 %28, 1
%51 = icmp eq i32 %50, 0
%52 = select i1 %51, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0)
br label %53
53: ; preds = %49, %46, %43, %40, %37, %34, %31, %26
%54 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %26 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %31 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %34 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), %37 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %40 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), %43 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), %46 ], [ %52, %49 ]
%55 = tail call i32 @error(i8* %54)
br label %56
56: ; preds = %53, %21
%57 = and i32 %23, 8
%58 = icmp eq i32 %57, 0
br i1 %58, label %21, label %59
59: ; preds = %56, %59
%60 = phi i64 [ %68, %59 ], [ 0, %56 ]
%61 = phi i32 [ %70, %59 ], [ 0, %56 ]
%62 = tail call signext i16 @inportw(i32 496)
%63 = trunc i16 %62 to i8
%64 = or i64 %60, 1
%65 = getelementptr inbounds i8, i8* %0, i64 %60
store i8 %63, i8* %65
%66 = lshr i16 %62, 8
%67 = trunc i16 %66 to i8
%68 = add nuw nsw i64 %60, 2
%69 = getelementptr inbounds i8, i8* %0, i64 %64
store i8 %67, i8* %69
%70 = add nuw nsw i32 %61, 1
%71 = icmp eq i32 %70, 256
br i1 %71, label %72, label %59
72: ; preds = %59
ret void
}