#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 /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ lpt1_init(int);
int /*<<< orphan*/ lpt1_remove();
int /*<<< orphan*/ lpt2_remove();
int /*<<< orphan*/ serial_setup(int, int, int);
void cbm_io_write(uint16_t port, uint8_t val, void *p) {
  lpt1_remove();
  lpt2_remove();
  switch (val & 3) {
  case 1:
    lpt1_init(0x3bc);
    break;
  case 2:
    lpt1_init(0x378);
    break;
  case 3:
    lpt1_init(0x278);
    break;
  }
  switch (val & 0xc) {
  case 0x4:
    serial_setup(1, 0x2f8, 3);
    break;
  case 0x8:
    serial_setup(1, 0x3f8, 4);
    break;
  }
}


x86 assembly (clang, -O0):
<cbm_io_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
xor    %eax,%eax
mov    %al,-0x15(%rbp)
call   1060 <lpt1_remove@plt>
mov    -0x15(%rbp),%al
call   1050 <lpt2_remove@plt>
mov    -0x8(%rbp),%eax
and    $0x3,%eax
mov    %eax,-0x14(%rbp)
sub    $0x1,%eax
je     119d <cbm_io_write+0x5d>
jmp    117b <cbm_io_write+0x3b>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
je     11ac <cbm_io_write+0x6c>
jmp    118c <cbm_io_write+0x4c>
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
je     11bb <cbm_io_write+0x7b>
jmp    11c5 <cbm_io_write+0x85>
mov    $0x3bc,%edi
call   1040 <lpt1_init@plt>
jmp    11c5 <cbm_io_write+0x85>
mov    $0x378,%edi
call   1040 <lpt1_init@plt>
jmp    11c5 <cbm_io_write+0x85>
mov    $0x278,%edi
call   1040 <lpt1_init@plt>
mov    -0x8(%rbp),%eax
and    $0xc,%eax
mov    %eax,-0x1c(%rbp)
sub    $0x4,%eax
je     11ed <cbm_io_write+0xad>
jmp    11dc <cbm_io_write+0x9c>
mov    -0x1c(%rbp),%eax
sub    $0x8,%eax
je     1206 <cbm_io_write+0xc6>
jmp    121a <cbm_io_write+0xda>
mov    $0x1,%edi
mov    $0x2f8,%esi
mov    $0x3,%edx
call   1030 <serial_setup@plt>
jmp    121a <cbm_io_write+0xda>
mov    $0x1,%edi
mov    $0x3f8,%esi
mov    $0x4,%edx
call   1030 <serial_setup@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbm_io_write>:
push   %rbx
mov    %esi,%ebx
xor    %eax,%eax
call   1060 <lpt1_remove@plt>
xor    %eax,%eax
call   1050 <lpt2_remove@plt>
mov    %ebx,%eax
and    $0x3,%eax
add    $0xffffffff,%eax
cmp    $0x2,%eax
ja     116f <cbm_io_write+0x2f>
cltq
lea    0xe99(%rip),%rcx
mov    (%rcx,%rax,4),%edi
call   1040 <lpt1_init@plt>
and    $0xc,%ebx
cmp    $0x8,%ebx
je     1191 <cbm_io_write+0x51>
cmp    $0x4,%ebx
jne    11a6 <cbm_io_write+0x66>
mov    $0x1,%edi
mov    $0x2f8,%esi
mov    $0x3,%edx
pop    %rbx
jmp    1030 <serial_setup@plt>
mov    $0x1,%edi
mov    $0x3f8,%esi
mov    $0x4,%edx
pop    %rbx
jmp    1030 <serial_setup@plt>
pop    %rbx
ret

LLVM IR (-O0):
define void @cbm_io_write(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%7 = call i32 (...) @lpt1_remove()
%8 = call i32 (...) @lpt2_remove()
%9 = load i32, i32* %5
%10 = and i32 %9, 3
switch i32 %10, label %17 [
i32 1, label %11
i32 2, label %13
i32 3, label %15
]
11: ; preds = %3
%12 = call i32 @lpt1_init(i32 956)
br label %17
13: ; preds = %3
%14 = call i32 @lpt1_init(i32 888)
br label %17
15: ; preds = %3
%16 = call i32 @lpt1_init(i32 632)
br label %17
17: ; preds = %3, %15, %13, %11
%18 = load i32, i32* %5
%19 = and i32 %18, 12
switch i32 %19, label %24 [
i32 4, label %20
i32 8, label %22
]
20: ; preds = %17
%21 = call i32 @serial_setup(i32 1, i32 760, i32 3)
br label %24
22: ; preds = %17
%23 = call i32 @serial_setup(i32 1, i32 1016, i32 4)
br label %24
24: ; preds = %17, %22, %20
ret void
}

LLVM IR (-O2):
@switch.table.cbm_io_write = [3 x i32] [i32 956, i32 888, i32 632]
define void @cbm_io_write(i32 %0, i32 %1, i8* nocapture readnone %2) {
%4 = tail call i32 (...) @lpt1_remove()
%5 = tail call i32 (...) @lpt2_remove()
%6 = and i32 %1, 3
%7 = add nsw i32 %6, -1
%8 = icmp ult i32 %7, 3
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = sext i32 %7 to i64
%11 = getelementptr inbounds [3 x i32], [3 x i32]* @switch.table.cbm_io_write, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = tail call i32 @lpt1_init(i32 %12)
br label %14
14: ; preds = %3, %9
%15 = and i32 %1, 12
switch i32 %15, label %20 [
i32 4, label %16
i32 8, label %18
]
16: ; preds = %14
%17 = tail call i32 @serial_setup(i32 1, i32 760, i32 3)
br label %20
18: ; preds = %14
%19 = tail call i32 @serial_setup(i32 1, i32 1016, i32 4)
br label %20
20: ; preds = %14, %18, %16
ret void
}