#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 ide_read(unsigned char, int);
unsigned char ide_polling(unsigned char channel, unsigned int advanced_check) {
  for (int i = 0; i < 4; i++)
    ide_read(channel, 0x0C);
  while (ide_read(channel, 0x07) & 0x80)
    ;
  if (advanced_check) {
    unsigned char state = ide_read(channel, 0x07);
    if (state & 0x01)
      return 2;
    if (state & 0x20)
      return 1;
    if ((state & 0x8) == 0)
      return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ide_polling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x2(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    114e <ide_polling+0x3e>
mov    $0xc,%esi
movzbl -0x2(%rbp),%edi
call   1030 <ide_read@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1128 <ide_polling+0x18>
jmp    1153 <ide_polling+0x43>
mov    $0x7,%esi
movzbl -0x2(%rbp),%edi
call   1030 <ide_read@plt>
and    $0x80,%eax
cmp    $0x0,%eax
je     1174 <ide_polling+0x64>
jmp    1153 <ide_polling+0x43>
cmpl   $0x0,-0x8(%rbp)
je     11df <ide_polling+0xcf>
mov    $0x7,%esi
movzbl -0x2(%rbp),%edi
call   1030 <ide_read@plt>
mov    %al,-0xd(%rbp)
movzbl -0xd(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11a8 <ide_polling+0x98>
movb   $0x2,-0x1(%rbp)
jmp    11e3 <ide_polling+0xd3>
movzbl -0xd(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
je     11c1 <ide_polling+0xb1>
movb   $0x1,-0x1(%rbp)
jmp    11e3 <ide_polling+0xd3>
movzbl -0xd(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
jne    11da <ide_polling+0xca>
movb   $0x3,-0x1(%rbp)
jmp    11e3 <ide_polling+0xd3>
jmp    11df <ide_polling+0xcf>
movb   $0x0,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ide_polling>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0xc,%esi
call   1030 <ide_read@plt>
mov    %ebp,%edi
mov    $0xc,%esi
call   1030 <ide_read@plt>
mov    %ebp,%edi
mov    $0xc,%esi
call   1030 <ide_read@plt>
mov    %ebp,%edi
mov    $0xc,%esi
call   1030 <ide_read@plt>
movzbl %bpl,%ebp
nopl   0x0(%rax)
mov    %ebp,%edi
mov    $0x7,%esi
call   1030 <ide_read@plt>
test   $0x80,%al
jne    1150 <ide_polling+0x40>
test   %ebx,%ebx
je     1187 <ide_polling+0x77>
mov    %ebp,%edi
mov    $0x7,%esi
call   1030 <ide_read@plt>
mov    %eax,%ecx
mov    $0x2,%al
test   $0x1,%cl
jne    1189 <ide_polling+0x79>
mov    $0x1,%al
test   $0x20,%cl
jne    1189 <ide_polling+0x79>
mov    $0x3,%al
test   $0x8,%cl
je     1189 <ide_polling+0x79>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @ide_polling(i8 zeroext %0, i32 %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8 %0, i8* %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %8
8: ; preds = %14, %2
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 4
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i8, i8* %4
%13 = call i32 @ide_read(i8 zeroext %12, i32 12)
br label %14
14: ; preds = %11
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
store i32 %16, i32* %6
br label %8
17: ; preds = %8
br label %18
18: ; preds = %23, %17
%19 = load i8, i8* %4
%20 = call i32 @ide_read(i8 zeroext %19, i32 7)
%21 = and i32 %20, 128
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %18
24: ; preds = %18
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %49
27: ; preds = %24
%28 = load i8, i8* %4
%29 = call i32 @ide_read(i8 zeroext %28, i32 7)
%30 = trunc i32 %29 to i8
store i8 %30, i8* %7
%31 = load i8, i8* %7
%32 = zext i8 %31 to i32
%33 = and i32 %32, 1
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
store i8 2, i8* %3
br label %50
36: ; preds = %27
%37 = load i8, i8* %7
%38 = zext i8 %37 to i32
%39 = and i32 %38, 32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
store i8 1, i8* %3
br label %50
42: ; preds = %36
%43 = load i8, i8* %7
%44 = zext i8 %43 to i32
%45 = and i32 %44, 8
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
store i8 3, i8* %3
br label %50
48: ; preds = %42
br label %49
49: ; preds = %48, %24
store i8 0, i8* %3
br label %50
50: ; preds = %49, %47, %41, %35
%51 = load i8, i8* %3
ret i8 %51
}

LLVM IR (-O2):
define zeroext i8 @ide_polling(i8 zeroext %0, i32 %1) {
%3 = tail call i32 @ide_read(i8 zeroext %0, i32 12)
%4 = tail call i32 @ide_read(i8 zeroext %0, i32 12)
%5 = tail call i32 @ide_read(i8 zeroext %0, i32 12)
%6 = tail call i32 @ide_read(i8 zeroext %0, i32 12)
br label %7
7: ; preds = %2, %7
%8 = tail call i32 @ide_read(i8 zeroext %0, i32 7)
%9 = and i32 %8, 128
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %7
11: ; preds = %7
%12 = icmp eq i32 %1, 0
br i1 %12, label %23, label %13
13: ; preds = %11
%14 = tail call i32 @ide_read(i8 zeroext %0, i32 7)
%15 = and i32 %14, 1
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = and i32 %14, 32
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = and i32 %14, 8
%22 = icmp eq i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %20, %11
br label %24
24: ; preds = %17, %13, %20, %23
%25 = phi i8 [ 3, %20 ], [ 0, %23 ], [ 1, %17 ], [ 2, %13 ]
ret i8 %25
}