#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 inp(int);
int /*<<< orphan*/ int_disable();
int /*<<< orphan*/ int_enable();
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(0x40, 0x40, 37);
  val8 = read_byte(0x0040, 0x008b);
  val8 >>= 6;
  outp(0x03f7, val8);
  do {
    val8 = inp(0x3f4);
  } while ((val8 & 0xc0) != 0x80);
  if (prev_reset == 0) {
    int_enable();
    do {
      val8 = read_byte(0x0040, 0x003e);
    } while ((val8 & 0x80) == 0);
    val8 &= 0x7f;
    int_disable();
    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   1080 <write_byte@plt>
mov    $0x3f2,%edi
call   1070 <inp@plt>
and    $0x4,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11be <floppy_prepare_controller+0x5e>
movl   $0x20,-0xc(%rbp)
jmp    11c5 <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   1050 <outp@plt>
mov    $0x40,%esi
mov    $0x25,%edx
mov    %esi,%edi
call   1080 <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   1050 <outp@plt>
mov    $0x3f4,%edi
call   1070 <inp@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0xc0,%eax
cmp    $0x80,%eax
jne    121d <floppy_prepare_controller+0xbd>
cmpl   $0x0,-0x10(%rbp)
jne    1293 <floppy_prepare_controller+0x133>
mov    $0x0,%al
call   1040 <int_enable@plt>
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     124e <floppy_prepare_controller+0xee>
mov    -0x8(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1060 <int_disable@plt>
mov    -0x8(%rbp),%edx
mov    $0x40,%edi
mov    $0x3e,%esi
call   1080 <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   1080 <write_byte@plt>
mov    $0x3f2,%edi
call   1070 <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   1050 <outp@plt>
mov    $0x40,%edi
mov    $0x40,%esi
mov    $0x25,%edx
call   1080 <write_byte@plt>
mov    $0x40,%edi
mov    $0x8b,%esi
call   1030 <read_byte@plt>
sar    $0x6,%eax
mov    $0x3f7,%edi
mov    %eax,%esi
call   1050 <outp@plt>
mov    $0x3f4,%edi
call   1070 <inp@plt>
and    $0xc0,%eax
cmp    $0x80,%eax
jne    11e0 <floppy_prepare_controller+0x80>
test   $0x4,%bl
jne    1247 <floppy_prepare_controller+0xe7>
xor    %eax,%eax
call   1040 <int_enable@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x40,%edi
mov    $0x3e,%esi
call   1030 <read_byte@plt>
mov    %eax,%ebx
test   $0x80,%bl
je     1210 <floppy_prepare_controller+0xb0>
and    $0x7f,%ebx
xor    %eax,%eax
call   1060 <int_disable@plt>
mov    $0x40,%edi
mov    $0x3e,%esi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <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 %54
40: ; preds = %37
%41 = call i32 (...) @int_enable()
br label %42
42: ; preds = %44, %40
%43 = call i32 @read_byte(i32 64, i32 62)
store i32 %43, i32* %3
br label %44
44: ; preds = %42
%45 = load i32, i32* %3
%46 = and i32 %45, 128
%47 = icmp eq i32 %46, 0
br i1 %47, label %42, label %48
48: ; preds = %44
%49 = load i32, i32* %3
%50 = and i32 %49, 127
store i32 %50, i32* %3
%51 = call i32 (...) @int_disable()
%52 = load i32, i32* %3
%53 = call i32 @write_byte(i32 64, i32 62, i32 %52)
br label %54
54: ; preds = %48, %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 %31
21: ; preds = %18
%22 = tail call i32 (...) @int_enable()
br label %23
23: ; preds = %23, %21
%24 = tail call i32 @read_byte(i32 64, i32 62)
%25 = and i32 %24, 128
%26 = icmp eq i32 %25, 0
br i1 %26, label %23, label %27
27: ; preds = %23
%28 = and i32 %24, 127
%29 = tail call i32 (...) @int_disable()
%30 = tail call i32 @write_byte(i32 64, i32 62, i32 %28)
br label %31
31: ; preds = %27, %18
ret void
}