#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;
typedef int uint16_t;

/* Variables and functions */
int floppy_wait_for_interrupt();
int inp(int);
int /*<<< orphan*/ outp(int, int);
int read_byte(int, int);
int /*<<< orphan*/ write_byte(int, int, int);
void floppy_prepare_controller(uint16_t drive) {
  uint8_t val8, dor, prev_reset;
  val8 = read_byte(0x0040, 0x003e);
  val8 &= 0x7f;
  write_byte(0x0040, 0x003e, val8);
  prev_reset = inp(0x03f2) & 0x04;
  if (drive)
    dor = 0x20;
  else
    dor = 0x10;
  dor |= 0x0c;
  dor |= drive;
  outp(0x03f2, dor);
  write_byte(0x0040, 0x0040, 37);
  val8 = read_byte(0x0040, 0x008b);
  val8 >>= 6;
  outp(0x03f7, val8);
  do {
    val8 = inp(0x3f4);
  } while ((val8 & 0xc0) != 0x80);
  if (prev_reset == 0) {
    val8 = floppy_wait_for_interrupt();
    write_byte(0x0040, 0x003e, val8);
  }
}


x86 assembly (clang, -O0):
<floppy_prepare_controller>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x40,%edi
mov    $0x3e,%esi
call   1030 <read_byte@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edx
mov    $0x40,%edi
mov    $0x3e,%esi
call   1070 <write_byte@plt>
mov    $0x3f2,%edi
call   1060 <inp@plt>
and    $0x4,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11ae <floppy_prepare_controller+0x5e>
movl   $0x20,-0xc(%rbp)
jmp    11b5 <floppy_prepare_controller+0x65>
movl   $0x10,-0xc(%rbp)
mov    -0xc(%rbp),%eax
or     $0xc,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    $0x3f2,%edi
call   1040 <outp@plt>
mov    $0x40,%esi
mov    $0x25,%edx
mov    %esi,%edi
call   1070 <write_byte@plt>
mov    $0x40,%edi
mov    $0x8b,%esi
call   1030 <read_byte@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x6,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    $0x3f7,%edi
call   1040 <outp@plt>
mov    $0x3f4,%edi
call   1060 <inp@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    120d <floppy_prepare_controller+0xbd>
cmpl   $0x0,-0x10(%rbp)
jne    1253 <floppy_prepare_controller+0x103>
mov    $0x0,%al
call   1050 <floppy_wait_for_interrupt@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edx
mov    $0x40,%edi
mov    $0x3e,%esi
call   1070 <write_byte@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<floppy_prepare_controller>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x40,%edi
mov    $0x3e,%esi
call   1030 <read_byte@plt>
and    $0x7f,%eax
mov    $0x40,%edi
mov    $0x3e,%esi
mov    %eax,%edx
call   1070 <write_byte@plt>
mov    $0x3f2,%edi
call   1060 <inp@plt>
mov    %eax,%ebx
xor    %esi,%esi
test   %ebp,%ebp
setne  %sil
shl    $0x4,%esi
add    $0x1c,%esi
or     %ebp,%esi
mov    $0x3f2,%edi
call   1040 <outp@plt>
mov    $0x40,%edi
mov    $0x40,%esi
mov    $0x25,%edx
call   1070 <write_byte@plt>
mov    $0x40,%edi
mov    $0x8b,%esi
call   1030 <read_byte@plt>
sar    $0x6,%eax
mov    $0x3f7,%edi
mov    %eax,%esi
call   1040 <outp@plt>
mov    $0x3f4,%edi
call   1060 <inp@plt>
and    $0xc0,%eax
cmp    $0x80,%eax
jne    11d0 <floppy_prepare_controller+0x80>
test   $0x4,%bl
jne    1209 <floppy_prepare_controller+0xb9>
xor    %eax,%eax
call   1050 <floppy_wait_for_interrupt@plt>
mov    $0x40,%edi
mov    $0x3e,%esi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <write_byte@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @floppy_prepare_controller(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i32 @read_byte(i32 64, i32 62)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = and i32 %7, 127
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @write_byte(i32 64, i32 62, i32 %9)
%11 = call i32 @inp(i32 1010)
%12 = and i32 %11, 4
store i32 %12, i32* %5
%13 = load i32, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 32, i32* %4
br label %17
16: ; preds = %1
store i32 16, i32* %4
br label %17
17: ; preds = %16, %15
%18 = load i32, i32* %4
%19 = or i32 %18, 12
store i32 %19, i32* %4
%20 = load i32, i32* %2
%21 = load i32, i32* %4
%22 = or i32 %21, %20
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = call i32 @outp(i32 1010, i32 %23)
%25 = call i32 @write_byte(i32 64, i32 64, i32 37)
%26 = call i32 @read_byte(i32 64, i32 139)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = ashr i32 %27, 6
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = call i32 @outp(i32 1015, i32 %29)
br label %31
31: ; preds = %33, %17
%32 = call i32 @inp(i32 1012)
store i32 %32, i32* %3
br label %33
33: ; preds = %31
%34 = load i32, i32* %3
%35 = and i32 %34, 192
%36 = icmp ne i32 %35, 128
br i1 %36, label %31, label %37
37: ; preds = %33
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 (...) @floppy_wait_for_interrupt()
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = call i32 @write_byte(i32 64, i32 62, i32 %42)
br label %44
44: ; preds = %40, %37
ret void
}

LLVM IR (-O2):
define void @floppy_prepare_controller(i32 %0) {
%2 = tail call i32 @read_byte(i32 64, i32 62)
%3 = and i32 %2, 127
%4 = tail call i32 @write_byte(i32 64, i32 62, i32 %3)
%5 = tail call i32 @inp(i32 1010)
%6 = icmp eq i32 %0, 0
%7 = select i1 %6, i32 28, i32 44
%8 = or i32 %7, %0
%9 = tail call i32 @outp(i32 1010, i32 %8)
%10 = tail call i32 @write_byte(i32 64, i32 64, i32 37)
%11 = tail call i32 @read_byte(i32 64, i32 139)
%12 = ashr i32 %11, 6
%13 = tail call i32 @outp(i32 1015, i32 %12)
br label %14
14: ; preds = %14, %1
%15 = tail call i32 @inp(i32 1012)
%16 = and i32 %15, 192
%17 = icmp eq i32 %16, 128
br i1 %17, label %18, label %14
18: ; preds = %14
%19 = and i32 %5, 4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 (...) @floppy_wait_for_interrupt()
%23 = tail call i32 @write_byte(i32 64, i32 62, i32 %22)
br label %24
24: ; preds = %21, %18
ret void
}