#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);
int /*<<< orphan*/ out_byte(int, int);
int /*<<< orphan*/ out_word(int, unsigned short);
void atapio_write_sectors(int lba, char sector_count, char *buffer) {
  out_byte(0x1F0, 0x00);
  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, 0x30);
  unsigned int offset = 0, i = 0;
  while (sector_count--) {
    atapio_wait_for_rdy(0x1F7);
    atapio_wait_for_drq(0x1F7);
    for (i = 0; i < 256;
         out_word(0x1F0, *((unsigned short *)buffer + offset + i++)))
      ;
    offset += 256;
  }
  return;
}


x86 assembly (clang, -O0):
<atapio_write_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>
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
sar    $0x8,%eax
movzbl %al,%esi
mov    $0x1f4,%edi
call   1060 <out_byte@plt>
mov    -0x4(%rbp),%eax
sar    $0x10,%eax
movzbl %al,%esi
mov    $0x1f5,%edi
call   1060 <out_byte@plt>
mov    -0x4(%rbp),%esi
sar    $0x18,%esi
and    $0xf,%esi
or     $0xe0,%esi
mov    $0x1f6,%edi
call   1060 <out_byte@plt>
mov    $0x1f7,%edi
mov    $0x30,%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     1265 <atapio_write_sectors+0x125>
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    1255 <atapio_write_sectors+0x115>
jmp    1227 <atapio_write_sectors+0xe7>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
mov    %ecx,%ecx
mov    $0x1f0,%edi
movzwl (%rax,%rcx,2),%esi
call   1050 <out_word@plt>
jmp    1215 <atapio_write_sectors+0xd5>
mov    -0x14(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x14(%rbp)
jmp    11e7 <atapio_write_sectors+0xa7>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atapio_write_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>
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    $0x30,%esi
call   1060 <out_byte@plt>
test   %r14b,%r14b
je     1228 <atapio_write_sectors+0xe8>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
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)
movzwl (%rbx,%rbp,2),%esi
add    $0x1,%rbp
mov    $0x1f0,%edi
call   1050 <out_word@plt>
cmp    $0x100,%rbp
jne    1200 <atapio_write_sectors+0xc0>
add    $0x200,%rbx
add    $0xff,%r15b
jne    11e0 <atapio_write_sectors+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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