#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*/ ide_dump_registers();
int ide_wait_dma();
int /*<<< orphan*/ ide_wait_irq();
int ide_wait_ready();
int /*<<< orphan*/ printf(char *, ...);
int ide_read_dma(char *buf, int buflen) {
  int status;
  ((*((volatile unsigned long *)(0xA05F7000 + 0x414))) = (1));
  ((*((volatile unsigned long *)(0xA05F7000 + 0x418))) = (1));
  printf("Started DMA\n");
  ide_dump_registers();
  ide_wait_irq();
  printf("After IRQ\n");
  ide_dump_registers();
  ((*((volatile unsigned long *)(0xA05F7000 + 0x414))) = (0));
  status = ide_wait_dma();
  printf("After DMA finished\n");
  ide_dump_registers();
  if (status != 0) {
    return -1;
  }
  status = (*((volatile unsigned long *)(0xA05F6900)));
  if ((status & (1 << 14)) == 0) {
    printf("DMARQ cleared already\n");
  } else {
    (unsigned int)(*((volatile unsigned char *)(0xA05F7000 + 0x09C)));
    if ((status & (1 << 14)) == 0) {
      printf("DMARQ cleared successfully\n");
    } else {
      printf("DMARQ not cleared: %08X\n",
             (*((volatile unsigned long *)(0xA05F6900))));
    }
  }
  status = ide_wait_ready();
  printf("After IDE ready\n");
  ide_dump_registers();
  if (status != 0) {
    return -1;
  }
  return (*((volatile unsigned long *)(0xA05F7000 + 0x4F8)));
}


x86 assembly (clang, -O0):
<ide_read_dma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0xa05f7414,%eax
movq   $0x1,(%rax)
mov    $0xa05f7418,%eax
movq   $0x1,(%rax)
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <ide_dump_registers@plt>
mov    $0x0,%al
call   1070 <ide_wait_irq@plt>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <ide_dump_registers@plt>
mov    $0xa05f7414,%eax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1050 <ide_wait_dma@plt>
mov    %eax,-0x18(%rbp)
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <ide_dump_registers@plt>
cmpl   $0x0,-0x18(%rbp)
je     11e9 <ide_read_dma+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <ide_read_dma+0x14e>
mov    $0xa05f6900,%eax
mov    (%rax),%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0x4000,%eax
cmp    $0x0,%eax
jne    1218 <ide_read_dma+0xc8>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125e <ide_read_dma+0x10e>
mov    $0xa05f709c,%eax
mov    (%rax),%al
mov    -0x18(%rbp),%eax
and    $0x4000,%eax
cmp    $0x0,%eax
jne    1243 <ide_read_dma+0xf3>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1259 <ide_read_dma+0x109>
mov    $0xa05f6900,%eax
mov    (%rax),%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125e <ide_read_dma+0x10e>
mov    $0x0,%al
call   1030 <ide_wait_ready@plt>
mov    %eax,-0x18(%rbp)
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <ide_dump_registers@plt>
cmpl   $0x0,-0x18(%rbp)
je     1293 <ide_read_dma+0x143>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <ide_read_dma+0x14e>
mov    $0xa05f74f8,%eax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ide_read_dma>:
push   %rbp
push   %r14
push   %rbx
mov    $0xa05f6900,%r14d
movq   $0x1,0xb14(%r14)
movq   $0x1,0xb18(%r14)
lea    0xe92(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <ide_dump_registers@plt>
xor    %eax,%eax
call   1080 <ide_wait_irq@plt>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <ide_dump_registers@plt>
movq   $0x0,0xb14(%r14)
xor    %eax,%eax
call   1060 <ide_wait_dma@plt>
mov    %eax,%ebp
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <ide_dump_registers@plt>
mov    $0xffffffff,%ebx
test   %ebp,%ebp
jne    1233 <ide_read_dma+0xd3>
testq  $0x4000,(%r14)
jne    11f4 <ide_read_dma+0x94>
lea    0xe65(%rip),%rdi
call   1040 <puts@plt>
jmp    120c <ide_read_dma+0xac>
mov    0x79c(%r14),%al
mov    (%r14),%rsi
lea    0xdfb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <ide_wait_ready@plt>
mov    %eax,%ebp
lea    0xe26(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <ide_dump_registers@plt>
test   %ebp,%ebp
jne    1233 <ide_read_dma+0xd3>
mov    0xbf8(%r14),%rbx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Started DMA\0A\00"
@.str.1 = [11 x i8] c"After IRQ\0A\00"
@.str.2 = [20 x i8] c"After DMA finished\0A\00"
@.str.3 = [23 x i8] c"DMARQ cleared already\0A\00"
@.str.4 = [28 x i8] c"DMARQ cleared successfully\0A\00"
@.str.5 = [25 x i8] c"DMARQ not cleared: %08X\0A\00"
@.str.6 = [17 x i8] c"After IDE ready\0A\00"
define i32 @ide_read_dma(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store volatile i64 1, i64* inttoptr (i64 2690610196 to i64*)
store volatile i64 1, i64* inttoptr (i64 2690610200 to i64*)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (...) @ide_dump_registers()
%9 = call i32 (...) @ide_wait_irq()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 (...) @ide_dump_registers()
store volatile i64 0, i64* inttoptr (i64 2690610196 to i64*)
%12 = call i32 (...) @ide_wait_dma()
store i32 %12, i32* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 (...) @ide_dump_registers()
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %48
18: ; preds = %2
%19 = load volatile i64, i64* inttoptr (i64 2690607360 to i64*)
%20 = trunc i64 %19 to i32
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = and i32 %21, 16384
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %38
26: ; preds = %18
%27 = load volatile i8, i8* inttoptr (i64 2690609308 to i8*)
%28 = zext i8 %27 to i32
%29 = load i32, i32* %6
%30 = and i32 %29, 16384
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %37
34: ; preds = %26
%35 = load volatile i64, i64* inttoptr (i64 2690607360 to i64*)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %35)
br label %37
37: ; preds = %34, %32
br label %38
38: ; preds = %37, %24
%39 = call i32 (...) @ide_wait_ready()
store i32 %39, i32* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 (...) @ide_dump_registers()
%42 = load i32, i32* %6
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38
store i32 -1, i32* %3
br label %48
45: ; preds = %38
%46 = load volatile i64, i64* inttoptr (i64 2690610424 to i64*)
%47 = trunc i64 %46 to i32
store i32 %47, i32* %3
br label %48
48: ; preds = %45, %44, %17
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str.5 = [25 x i8] c"DMARQ not cleared: %08X\0A\00"
@str = [12 x i8] c"Started DMA\00"
@str.7 = [10 x i8] c"After IRQ\00"
@str.8 = [19 x i8] c"After DMA finished\00"
@str.9 = [16 x i8] c"After IDE ready\00"
@str.10 = [22 x i8] c"DMARQ cleared already\00"
define i32 @ide_read_dma(i8* nocapture readnone %0, i32 %1) {
store volatile i64 1, i64* inttoptr (i64 2690610196 to i64*)
store volatile i64 1, i64* inttoptr (i64 2690610200 to i64*)
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @ide_dump_registers()
%5 = tail call i32 (...) @ide_wait_irq()
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0))
%7 = tail call i32 (...) @ide_dump_registers()
store volatile i64 0, i64* inttoptr (i64 2690610196 to i64*)
%8 = tail call i32 (...) @ide_wait_dma()
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
%10 = tail call i32 (...) @ide_dump_registers()
%11 = icmp eq i32 %8, 0
br i1 %11, label %12, label %30
12: ; preds = %2
%13 = load volatile i64, i64* inttoptr (i64 2690607360 to i64*)
%14 = and i64 %13, 16384
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
br label %22
18: ; preds = %12
%19 = load volatile i8, i8* inttoptr (i64 2690609308 to i8*)
%20 = load volatile i64, i64* inttoptr (i64 2690607360 to i64*)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %20)
br label %22
22: ; preds = %18, %16
%23 = tail call i32 (...) @ide_wait_ready()
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%25 = tail call i32 (...) @ide_dump_registers()
%26 = icmp eq i32 %23, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load volatile i64, i64* inttoptr (i64 2690610424 to i64*)
%29 = trunc i64 %28 to i32
br label %30
30: ; preds = %22, %2, %27
%31 = phi i32 [ %29, %27 ], [ -1, %2 ], [ -1, %22 ]
ret i32 %31
}