#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 */
unsigned char inb(int);
unsigned short inw(int);
int /*<<< orphan*/ outb(int, int);
int read_sector(unsigned char *sector, unsigned char drive, unsigned int lba) {
  unsigned short *s = (unsigned short *)sector;
  outb(0x1F6, 0xE0 | (drive << 4) | ((lba >> 24) & 0x0F));
  outb(0x1F1, 0);
  outb(0x1F2, 1);
  outb(0x1F3, lba & 0xFF);
  outb(0x1F4, (lba >> 8) & 0xFF);
  outb(0x1F5, (lba >> 16) & 0xFF);
  outb(0x1F7, 0x20);
  unsigned char status;
  while ((status = inb(0x1F7))) {
    if (!(status & 0x80) && (status & 0x08))
      break;
  }
  int i;
  for (i = 0; i < 256; i++) {
    s[i] = inw(0x1F0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movzbl -0x9(%rbp),%esi
shl    $0x4,%esi
or     $0xe0,%esi
mov    -0x10(%rbp),%eax
shr    $0x18,%eax
and    $0xf,%eax
or     %eax,%esi
mov    $0x1f6,%edi
call   1040 <outb@plt>
mov    $0x1f1,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    $0x1f2,%edi
mov    $0x1,%esi
call   1040 <outb@plt>
mov    -0x10(%rbp),%esi
and    $0xff,%esi
mov    $0x1f3,%edi
call   1040 <outb@plt>
mov    -0x10(%rbp),%esi
shr    $0x8,%esi
and    $0xff,%esi
mov    $0x1f4,%edi
call   1040 <outb@plt>
mov    -0x10(%rbp),%esi
shr    $0x10,%esi
and    $0xff,%esi
mov    $0x1f5,%edi
call   1040 <outb@plt>
mov    $0x1f7,%edi
mov    $0x20,%esi
call   1040 <outb@plt>
mov    $0x1f7,%edi
call   1050 <inb@plt>
mov    %al,-0x19(%rbp)
cmp    $0x0,%al
je     1219 <read_sector+0xe9>
movzbl -0x19(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1214 <read_sector+0xe4>
movzbl -0x19(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1214 <read_sector+0xe4>
jmp    1219 <read_sector+0xe9>
jmp    11d8 <read_sector+0xa8>
movl   $0x0,-0x20(%rbp)
cmpl   $0x100,-0x20(%rbp)
jge    1254 <read_sector+0x124>
mov    $0x1f0,%edi
call   1030 <inw@plt>
mov    %ax,%dx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1220 <read_sector+0xf0>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sector>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rdi,%r14
shl    $0x4,%esi
mov    %edx,%eax
shr    $0x18,%eax
and    $0xf,%eax
or     %eax,%esi
or     $0xe0,%esi
mov    $0x1f6,%edi
call   1040 <outb@plt>
mov    $0x1f1,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    $0x1f2,%edi
mov    $0x1,%esi
call   1040 <outb@plt>
movzbl %bl,%esi
mov    $0x1f3,%edi
call   1040 <outb@plt>
movzbl %bh,%esi
mov    $0x1f4,%edi
call   1040 <outb@plt>
shr    $0x10,%ebx
movzbl %bl,%esi
mov    $0x1f5,%edi
call   1040 <outb@plt>
mov    $0x1f7,%edi
mov    $0x20,%esi
call   1040 <outb@plt>
nopw   0x0(%rax,%rax,1)
mov    $0x1f7,%edi
call   1050 <inb@plt>
test   %al,%al
je     11c4 <read_sector+0x94>
and    $0x88,%al
cmp    $0x8,%al
jne    11b0 <read_sector+0x80>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    $0x1f0,%edi
call   1030 <inw@plt>
mov    %ax,(%r14,%rbx,2)
add    $0x1,%rbx
cmp    $0x100,%rbx
jne    11d0 <read_sector+0xa0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @read_sector(i8* %0, i8 zeroext %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i16*
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %4
store i8 %1, i8* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = bitcast i8* %10 to i16*
store i16* %11, i16** %7
%12 = load i8, i8* %5
%13 = zext i8 %12 to i32
%14 = shl i32 %13, 4
%15 = or i32 224, %14
%16 = load i32, i32* %6
%17 = lshr i32 %16, 24
%18 = and i32 %17, 15
%19 = or i32 %15, %18
%20 = call i32 @outb(i32 502, i32 %19)
%21 = call i32 @outb(i32 497, i32 0)
%22 = call i32 @outb(i32 498, i32 1)
%23 = load i32, i32* %6
%24 = and i32 %23, 255
%25 = call i32 @outb(i32 499, i32 %24)
%26 = load i32, i32* %6
%27 = lshr i32 %26, 8
%28 = and i32 %27, 255
%29 = call i32 @outb(i32 500, i32 %28)
%30 = load i32, i32* %6
%31 = lshr i32 %30, 16
%32 = and i32 %31, 255
%33 = call i32 @outb(i32 501, i32 %32)
%34 = call i32 @outb(i32 503, i32 32)
br label %35
35: ; preds = %49, %3
%36 = call zeroext i8 @inb(i32 503)
store i8 %36, i8* %8
%37 = icmp ne i8 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %35
%39 = load i8, i8* %8
%40 = zext i8 %39 to i32
%41 = and i32 %40, 128
%42 = icmp ne i32 %41, 0
br i1 %42, label %49, label %43
43: ; preds = %38
%44 = load i8, i8* %8
%45 = zext i8 %44 to i32
%46 = and i32 %45, 8
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %50
49: ; preds = %43, %38
br label %35
50: ; preds = %48, %35
store i32 0, i32* %9
br label %51
51: ; preds = %60, %50
%52 = load i32, i32* %9
%53 = icmp slt i32 %52, 256
br i1 %53, label %54, label %63
54: ; preds = %51
%55 = call zeroext i16 @inw(i32 496)
%56 = load i16*, i16** %7
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i16, i16* %56, i64 %58
store i16 %55, i16* %59
br label %60
60: ; preds = %54
%61 = load i32, i32* %9
%62 = add nsw i32 %61, 1
store i32 %62, i32* %9
br label %51
63: ; preds = %51
ret i32 0
}

LLVM IR (-O2):
define i32 @read_sector(i8* nocapture writeonly %0, i8 zeroext %1, i32 %2) {
%4 = zext i8 %1 to i32
%5 = shl nuw nsw i32 %4, 4
%6 = lshr i32 %2, 24
%7 = and i32 %6, 15
%8 = or i32 %7, %5
%9 = or i32 %8, 224
%10 = tail call i32 @outb(i32 502, i32 %9)
%11 = tail call i32 @outb(i32 497, i32 0)
%12 = tail call i32 @outb(i32 498, i32 1)
%13 = and i32 %2, 255
%14 = tail call i32 @outb(i32 499, i32 %13)
%15 = lshr i32 %2, 8
%16 = and i32 %15, 255
%17 = tail call i32 @outb(i32 500, i32 %16)
%18 = lshr i32 %2, 16
%19 = and i32 %18, 255
%20 = tail call i32 @outb(i32 501, i32 %19)
%21 = tail call i32 @outb(i32 503, i32 32)
br label %22
22: ; preds = %22, %3
%23 = tail call zeroext i8 @inb(i32 503)
%24 = icmp eq i8 %23, 0
%25 = and i8 %23, -120
%26 = icmp eq i8 %25, 8
%27 = or i1 %24, %26
br i1 %27, label %28, label %22
28: ; preds = %22
%29 = bitcast i8* %0 to i16*
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ 0, %28 ], [ %34, %30 ]
%32 = tail call zeroext i16 @inw(i32 496)
%33 = getelementptr inbounds i16, i16* %29, i64 %31
store i16 %32, i16* %33
%34 = add nuw nsw i64 %31, 1
%35 = icmp eq i64 %34, 256
br i1 %35, label %36, label %30
36: ; preds = %30
ret i32 0
}