#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*/ atapio_wait_for_drq(int);
int /*<<< orphan*/ atapio_wait_for_rdy(int);
unsigned short in_word(int);
int /*<<< orphan*/ out_byte(int, int);
void atapio_read_sectors(unsigned int lba, char sector_count, char *buffer) {
  out_byte(0x1F0, 0x00);
  out_byte(0x1F2, (unsigned char)sector_count);
  out_byte(0x1F3, (unsigned char)lba);
  out_byte(0x1F4, (unsigned char)(lba >> 8));
  out_byte(0x1F5, (unsigned char)(lba >> 16));
  out_byte(0x1F6, 0xE0 | 0x40 | ((lba >> 24) & 0x0F));
  out_byte(0x1F7, 0x20);
  unsigned int offset = 0, i = 0;
  while (sector_count--) {
    atapio_wait_for_rdy(0x1F7);
    atapio_wait_for_drq(0x1F7);
    for (i = 0; i < 256; i++) {
      *((unsigned short *)buffer + offset + i) = in_word(0x1F0);
    }
    offset += 256;
  }
  return;
}


x86 assembly (clang, -O0):
<atapio_read_sectors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %rdx,-0x10(%rbp)
mov    $0x1f0,%edi
xor    %esi,%esi
call   1060 <out_byte@plt>
movzbl -0x5(%rbp),%esi
mov    $0x1f2,%edi
call   1060 <out_byte@plt>
mov    -0x4(%rbp),%eax
movzbl %al,%esi
mov    $0x1f3,%edi
call   1060 <out_byte@plt>
mov    -0x4(%rbp),%eax
shr    $0x8,%eax
movzbl %al,%esi
mov    $0x1f4,%edi
call   1060 <out_byte@plt>
mov    -0x4(%rbp),%eax
shr    $0x10,%eax
movzbl %al,%esi
mov    $0x1f5,%edi
call   1060 <out_byte@plt>
mov    -0x4(%rbp),%esi
shr    $0x18,%esi
and    $0xf,%esi
or     $0xe0,%esi
mov    $0x1f6,%edi
call   1060 <out_byte@plt>
mov    $0x1f7,%edi
mov    $0x20,%esi
call   1060 <out_byte@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x5(%rbp),%al
mov    %al,%cl
add    $0xff,%cl
mov    %cl,-0x5(%rbp)
cmp    $0x0,%al
je     1256 <atapio_read_sectors+0x116>
mov    $0x1f7,%edi
call   1040 <atapio_wait_for_rdy@plt>
mov    $0x1f7,%edi
call   1030 <atapio_wait_for_drq@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x100,-0x18(%rbp)
jae    1246 <atapio_read_sectors+0x106>
mov    $0x1f0,%edi
call   1050 <in_word@plt>
mov    %ax,%dx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%ecx
mov    %dx,(%rax,%rcx,2)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1209 <atapio_read_sectors+0xc9>
mov    -0x14(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x14(%rbp)
jmp    11db <atapio_read_sectors+0x9b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atapio_read_sectors>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %edi,%ebp
mov    $0x1f0,%edi
xor    %esi,%esi
call   1060 <out_byte@plt>
movzbl %r15b,%r14d
mov    $0x1f2,%edi
mov    %r14d,%esi
call   1060 <out_byte@plt>
movzbl %bpl,%esi
mov    $0x1f3,%edi
call   1060 <out_byte@plt>
mov    %ebp,%eax
movzbl %ah,%esi
mov    $0x1f4,%edi
call   1060 <out_byte@plt>
mov    %ebp,%eax
shr    $0x10,%eax
movzbl %al,%esi
mov    $0x1f5,%edi
call   1060 <out_byte@plt>
shr    $0x18,%ebp
and    $0xf,%ebp
or     $0xe0,%ebp
mov    $0x1f6,%edi
mov    %ebp,%esi
call   1060 <out_byte@plt>
mov    $0x1f7,%edi
mov    $0x20,%esi
call   1060 <out_byte@plt>
test   %r14b,%r14b
je     1218 <atapio_read_sectors+0xd8>
nopw   0x0(%rax,%rax,1)
mov    $0x1f7,%edi
call   1040 <atapio_wait_for_rdy@plt>
mov    $0x1f7,%edi
call   1030 <atapio_wait_for_drq@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    $0x1f0,%edi
call   1050 <in_word@plt>
mov    %ax,(%rbx,%rbp,2)
add    $0x1,%rbp
cmp    $0x100,%rbp
jne    11f0 <atapio_read_sectors+0xb0>
add    $0x200,%rbx
add    $0xff,%r15b
jne    11d0 <atapio_read_sectors+0x90>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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