#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 inb(int);
short inw(int);
int /*<<< orphan*/ outb(int, int);
void read_sector(void *buf, int lba) {
  outb(0x1F6, 0x40);
  outb(0x1F2, 0);
  outb(0x1F3, lba >> 24);
  outb(0x1F4, 0);
  outb(0x1F5, 0);
  outb(0x1F2, 1);
  outb(0x1F3, lba);
  outb(0x1F4, lba >> 8);
  outb(0x1F5, lba >> 16);
  outb(0x1F7, 0x24);
  while (inb(0x1F7) & 0x80)
    ;
  for (int i = 0; i < 256; i++) {
    ((short *)buf)[i] = inw(0x1F0);
  }
}


x86 assembly (clang, -O0):
<read_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x1f6,%edi
mov    $0x40,%esi
call   1040 <outb@plt>
mov    $0x1f2,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    -0xc(%rbp),%esi
sar    $0x18,%esi
mov    $0x1f3,%edi
call   1040 <outb@plt>
mov    $0x1f4,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    $0x1f5,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    $0x1f2,%edi
mov    $0x1,%esi
call   1040 <outb@plt>
mov    -0xc(%rbp),%esi
mov    $0x1f3,%edi
call   1040 <outb@plt>
mov    -0xc(%rbp),%esi
sar    $0x8,%esi
mov    $0x1f4,%edi
call   1040 <outb@plt>
mov    -0xc(%rbp),%esi
sar    $0x10,%esi
mov    $0x1f5,%edi
call   1040 <outb@plt>
mov    $0x1f7,%edi
mov    $0x24,%esi
call   1040 <outb@plt>
mov    $0x1f7,%edi
call   1050 <inb@plt>
and    $0x80,%eax
cmp    $0x0,%eax
je     11ea <read_sector+0xba>
jmp    11cd <read_sector+0x9d>
movl   $0x0,-0x10(%rbp)
cmpl   $0x100,-0x10(%rbp)
jge    1225 <read_sector+0xf5>
mov    $0x1f0,%edi
call   1030 <inw@plt>
mov    %ax,%dx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11f1 <read_sector+0xc1>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sector>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x1f6,%edi
mov    $0x40,%esi
call   1040 <outb@plt>
mov    $0x1f2,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    %ebp,%esi
sar    $0x18,%esi
mov    $0x1f3,%edi
call   1040 <outb@plt>
mov    $0x1f4,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    $0x1f5,%edi
xor    %esi,%esi
call   1040 <outb@plt>
mov    $0x1f2,%edi
mov    $0x1,%esi
call   1040 <outb@plt>
mov    $0x1f3,%edi
mov    %ebp,%esi
call   1040 <outb@plt>
mov    %ebp,%esi
sar    $0x8,%esi
mov    $0x1f4,%edi
call   1040 <outb@plt>
sar    $0x10,%ebp
mov    $0x1f5,%edi
mov    %ebp,%esi
call   1040 <outb@plt>
mov    $0x1f7,%edi
mov    $0x24,%esi
call   1040 <outb@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1f7,%edi
call   1050 <inb@plt>
test   $0x80,%al
jne    11d0 <read_sector+0xa0>
xor    %ebp,%ebp
mov    $0x1f0,%edi
call   1030 <inw@plt>
mov    %ax,(%rbx,%rbp,2)
add    $0x1,%rbp
cmp    $0x100,%rbp
jne    11e0 <read_sector+0xb0>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @read_sector(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = call i32 @outb(i32 502, i32 64)
%7 = call i32 @outb(i32 498, i32 0)
%8 = load i32, i32* %4
%9 = ashr i32 %8, 24
%10 = call i32 @outb(i32 499, i32 %9)
%11 = call i32 @outb(i32 500, i32 0)
%12 = call i32 @outb(i32 501, i32 0)
%13 = call i32 @outb(i32 498, i32 1)
%14 = load i32, i32* %4
%15 = call i32 @outb(i32 499, i32 %14)
%16 = load i32, i32* %4
%17 = ashr i32 %16, 8
%18 = call i32 @outb(i32 500, i32 %17)
%19 = load i32, i32* %4
%20 = ashr i32 %19, 16
%21 = call i32 @outb(i32 501, i32 %20)
%22 = call i32 @outb(i32 503, i32 36)
br label %23
23: ; preds = %27, %2
%24 = call i32 @inb(i32 503)
%25 = and i32 %24, 128
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %23
28: ; preds = %23
store i32 0, i32* %5
br label %29
29: ; preds = %39, %28
%30 = load i32, i32* %5
%31 = icmp slt i32 %30, 256
br i1 %31, label %32, label %42
32: ; preds = %29
%33 = call signext i16 @inw(i32 496)
%34 = load i8*, i8** %3
%35 = bitcast i8* %34 to i16*
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i16, i16* %35, i64 %37
store i16 %33, i16* %38
br label %39
39: ; preds = %32
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %29
42: ; preds = %29
ret void
}

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