#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 */
typedef int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ int_disable();
int /*<<< orphan*/ int_enable();
int /*<<< orphan*/ int_enable_hlt_disable();
int read_byte(int, int);
int /*<<< orphan*/ write_byte(int, int, int);
uint8_t floppy_wait_for_interrupt_or_timeout(void) {
  int_disable();
  for (;;) {
    uint8_t val8 = read_byte(0x0040, 0x0040);
    if (val8 == 0) {
      int_enable();
      return 0;
    }
    val8 = read_byte(0x0040, 0x003e);
    if (val8 & 0x80) {
      write_byte(0x0040, 0x003e, val8 & 0x7f);
      return val8;
    }
    int_enable_hlt_disable();
  }
}


x86 assembly (clang, -O0):
<floppy_wait_for_interrupt_or_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <int_disable@plt>
mov    $0x40,%esi
mov    %esi,%edi
call   1030 <read_byte@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    118b <floppy_wait_for_interrupt_or_timeout+0x3b>
mov    $0x0,%al
call   1050 <int_enable@plt>
movl   $0x0,-0x4(%rbp)
jmp    11da <floppy_wait_for_interrupt_or_timeout+0x8a>
mov    $0x40,%edi
mov    $0x3e,%esi
call   1030 <read_byte@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11ce <floppy_wait_for_interrupt_or_timeout+0x7e>
mov    -0x8(%rbp),%edx
and    $0x7f,%edx
mov    $0x40,%edi
mov    $0x3e,%esi
call   1070 <write_byte@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <floppy_wait_for_interrupt_or_timeout+0x8a>
mov    $0x0,%al
call   1040 <int_enable_hlt_disable@plt>
jmp    115f <floppy_wait_for_interrupt_or_timeout+0xf>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<floppy_wait_for_interrupt_or_timeout>:
push   %rbp
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <int_disable@plt>
nopl   0x0(%rax)
mov    $0x40,%edi
mov    $0x40,%esi
call   1030 <read_byte@plt>
test   %eax,%eax
je     1193 <floppy_wait_for_interrupt_or_timeout+0x43>
mov    $0x40,%edi
mov    $0x3e,%esi
call   1030 <read_byte@plt>
mov    %eax,%ebp
test   $0x80,%bpl
jne    119c <floppy_wait_for_interrupt_or_timeout+0x4c>
xor    %eax,%eax
call   1040 <int_enable_hlt_disable@plt>
jmp    1160 <floppy_wait_for_interrupt_or_timeout+0x10>
xor    %eax,%eax
call   1050 <int_enable@plt>
jmp    11b2 <floppy_wait_for_interrupt_or_timeout+0x62>
mov    %ebp,%edx
and    $0x7f,%edx
mov    $0x40,%edi
mov    $0x3e,%esi
call   1070 <write_byte@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @floppy_wait_for_interrupt_or_timeout() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @int_disable()
br label %4
4: ; preds = %20, %0
%5 = call i32 @read_byte(i32 64, i32 64)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = call i32 (...) @int_enable()
store i32 0, i32* %1
br label %22
10: ; preds = %4
%11 = call i32 @read_byte(i32 64, i32 62)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = and i32 %12, 128
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* %2
%17 = and i32 %16, 127
%18 = call i32 @write_byte(i32 64, i32 62, i32 %17)
%19 = load i32, i32* %2
store i32 %19, i32* %1
br label %22
20: ; preds = %10
%21 = call i32 (...) @int_enable_hlt_disable()
br label %4
22: ; preds = %15, %8
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
define i32 @floppy_wait_for_interrupt_or_timeout() {
%1 = tail call i32 (...) @int_disable()
%2 = tail call i32 @read_byte(i32 64, i32 64)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %13, %0
%5 = tail call i32 (...) @int_enable()
br label %17
6: ; preds = %0, %13
%7 = tail call i32 @read_byte(i32 64, i32 62)
%8 = and i32 %7, 128
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = and i32 %7, 127
%12 = tail call i32 @write_byte(i32 64, i32 62, i32 %11)
br label %17
13: ; preds = %6
%14 = tail call i32 (...) @int_enable_hlt_disable()
%15 = tail call i32 @read_byte(i32 64, i32 64)
%16 = icmp eq i32 %15, 0
br i1 %16, label %4, label %6
17: ; preds = %4, %10
%18 = phi i32 [ %7, %10 ], [ 0, %4 ]
ret i32 %18
}