#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 read_port(int);
int /*<<< orphan*/ write_port(int, int);
int /*<<< orphan*/ writef(char *, ...);
int ata_pio_write(int lba, char sect_count, char *buf) {
  int err = 0;
  int count;
  int lba3 = (lba >> 24) & 0x0F;
  int lba2 = (lba >> 16) & 0xFF;
  int lba1 = (lba >> 8) & 0xFF;
  int lba0 = (lba >> 0) & 0xFF;
  char *p;
  write_port(0x1F6, lba3 | 0xE0);
  write_port(0x1F5, lba2);
  write_port(0x1F4, lba1);
  write_port(0x1F3, lba0);
  write_port(0x1F7, 0x30);
  count = 0;
  while (!((err = read_port(0x1F7)) & 1 << 3) && count < 20)
    writef("[%d] return: %d\n", count++, err);
  writef("ready!\n");
  count = 0;
  p = buf;
  while (count++ < 512)
    write_port(0x1F0, (*p++) ?: 0);
  return 1;
}


x86 assembly (clang, -O0):
<ata_pio_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x18,%eax
and    $0xf,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%esi
or     $0xe0,%esi
mov    $0x1f6,%edi
call   1040 <write_port@plt>
mov    -0x20(%rbp),%esi
mov    $0x1f5,%edi
call   1040 <write_port@plt>
mov    -0x24(%rbp),%esi
mov    $0x1f4,%edi
call   1040 <write_port@plt>
mov    -0x28(%rbp),%esi
mov    $0x1f3,%edi
call   1040 <write_port@plt>
mov    $0x1f7,%edi
mov    $0x30,%esi
call   1040 <write_port@plt>
movl   $0x0,-0x18(%rbp)
mov    $0x1f7,%edi
call   1030 <read_port@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
and    $0x8,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
jne    11fc <ata_pio_write+0xcc>
cmpl   $0x14,-0x18(%rbp)
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    120c <ata_pio_write+0xdc>
jmp    122d <ata_pio_write+0xfd>
mov    -0x18(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <writef@plt>
jmp    11d2 <ata_pio_write+0xa2>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <writef@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x200,%eax
jge    12a5 <ata_pio_write+0x175>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
je     1289 <ata_pio_write+0x159>
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1293 <ata_pio_write+0x163>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    1293 <ata_pio_write+0x163>
mov    -0x3c(%rbp),%esi
mov    $0x1f0,%edi
call   1040 <write_port@plt>
jmp    124a <ata_pio_write+0x11a>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ata_pio_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %edi,%eax
mov    %edi,%ecx
movzbl %ah,%ebx
movzbl %al,%r14d
mov    %edi,%esi
shr    $0x18,%esi
and    $0xf,%esi
shr    $0x10,%ecx
movzbl %cl,%ebp
or     $0xe0,%esi
mov    $0x1f6,%edi
call   1040 <write_port@plt>
mov    $0x1f5,%edi
mov    %ebp,%esi
call   1040 <write_port@plt>
mov    $0x1f4,%edi
mov    %ebx,%esi
call   1040 <write_port@plt>
mov    $0x1f3,%edi
mov    %r14d,%esi
call   1040 <write_port@plt>
mov    $0x1f7,%edi
mov    $0x30,%esi
call   1040 <write_port@plt>
mov    $0x1f7,%edi
call   1030 <read_port@plt>
test   $0x8,%al
jne    11d6 <ata_pio_write+0xa6>
lea    0xe54(%rip),%r14
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <writef@plt>
mov    $0x1f7,%edi
call   1030 <read_port@plt>
test   $0x8,%al
jne    11d6 <ata_pio_write+0xa6>
lea    0x1(%rbx),%ecx
cmp    $0x13,%ebx
mov    %ecx,%ebx
jb     11b0 <ata_pio_write+0x80>
lea    0xe34(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <writef@plt>
cs nopw 0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%esi
mov    $0x1f0,%edi
call   1040 <write_port@plt>
add    $0x1,%rbx
cmp    $0x200,%ebx
jne    11f0 <ata_pio_write+0xc0>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"[%d] return: %d\0A\00"
@.str.1 = [8 x i8] c"ready!\0A\00"
define i32 @ata_pio_write(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i32 %0, i32* %4
store i8 %1, i8* %5
store i8* %2, i8** %6
store i32 0, i32* %7
%14 = load i32, i32* %4
%15 = ashr i32 %14, 24
%16 = and i32 %15, 15
store i32 %16, i32* %9
%17 = load i32, i32* %4
%18 = ashr i32 %17, 16
%19 = and i32 %18, 255
store i32 %19, i32* %10
%20 = load i32, i32* %4
%21 = ashr i32 %20, 8
%22 = and i32 %21, 255
store i32 %22, i32* %11
%23 = load i32, i32* %4
%24 = ashr i32 %23, 0
%25 = and i32 %24, 255
store i32 %25, i32* %12
%26 = load i32, i32* %9
%27 = or i32 %26, 224
%28 = call i32 @write_port(i32 502, i32 %27)
%29 = load i32, i32* %10
%30 = call i32 @write_port(i32 501, i32 %29)
%31 = load i32, i32* %11
%32 = call i32 @write_port(i32 500, i32 %31)
%33 = load i32, i32* %12
%34 = call i32 @write_port(i32 499, i32 %33)
%35 = call i32 @write_port(i32 503, i32 48)
store i32 0, i32* %8
br label %36
36: ; preds = %45, %3
%37 = call i32 @read_port(i32 503)
store i32 %37, i32* %7
%38 = and i32 %37, 8
%39 = icmp ne i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 20
br label %43
43: ; preds = %40, %36
%44 = phi i1 [ false, %36 ], [ %42, %40 ]
br i1 %44, label %45, label %50
45: ; preds = %43
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
%48 = load i32, i32* %7
%49 = call i32 (i8*, ...) @writef(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %48)
br label %36
50: ; preds = %43
%51 = call i32 (i8*, ...) @writef(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %8
%52 = load i8*, i8** %6
store i8* %52, i8** %13
br label %53
53: ; preds = %65, %50
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
%56 = icmp slt i32 %54, 512
br i1 %56, label %57, label %68
57: ; preds = %53
%58 = load i8*, i8** %13
%59 = getelementptr inbounds i8, i8* %58, i32 1
store i8* %59, i8** %13
%60 = load i8, i8* %58
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %57
br label %65
64: ; preds = %57
br label %65
65: ; preds = %64, %63
%66 = phi i32 [ %61, %63 ], [ 0, %64 ]
%67 = call i32 @write_port(i32 496, i32 %66)
br label %53
68: ; preds = %53
ret i32 1
}

LLVM IR (-O2):
@.str = [17 x i8] c"[%d] return: %d\0A\00"
@.str.1 = [8 x i8] c"ready!\0A\00"
define i32 @ata_pio_write(i32 %0, i8 signext %1, i8* nocapture %2) {
%4 = lshr i32 %0, 24
%5 = and i32 %4, 15
%6 = lshr i32 %0, 16
%7 = and i32 %6, 255
%8 = lshr i32 %0, 8
%9 = and i32 %8, 255
%10 = and i32 %0, 255
%11 = or i32 %5, 224
%12 = tail call i32 @write_port(i32 502, i32 %11)
%13 = tail call i32 @write_port(i32 501, i32 %7)
%14 = tail call i32 @write_port(i32 500, i32 %9)
%15 = tail call i32 @write_port(i32 499, i32 %10)
%16 = tail call i32 @write_port(i32 503, i32 48)
%17 = tail call i32 @read_port(i32 503)
%18 = and i32 %17, 8
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %3, %20
%21 = phi i32 [ %25, %20 ], [ %17, %3 ]
%22 = phi i32 [ %23, %20 ], [ 0, %3 ]
%23 = add nuw nsw i32 %22, 1
%24 = tail call i32 (i8*, ...) @writef(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %21)
%25 = tail call i32 @read_port(i32 503)
%26 = and i32 %25, 8
%27 = icmp eq i32 %26, 0
%28 = icmp ult i32 %22, 19
%29 = and i1 %27, %28
br i1 %29, label %20, label %30
30: ; preds = %20, %3
%31 = tail call i32 (i8*, ...) @writef(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %32
%33 = phi i8* [ %2, %30 ], [ %36, %32 ]
%34 = phi i32 [ 0, %30 ], [ %35, %32 ]
%35 = add nuw nsw i32 %34, 1
%36 = getelementptr inbounds i8, i8* %33, i64 1
%37 = load i8, i8* %33
%38 = sext i8 %37 to i32
%39 = tail call i32 @write_port(i32 496, i32 %38)
%40 = icmp eq i32 %35, 512
br i1 %40, label %41, label %32
41: ; preds = %32
ret i32 1
}