#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 inb(int);
int serial_getc(int tty_minor) {
  int com;
  switch (tty_minor) {
  case 1:
    com = 0x3F8;
    break;
  case 1 + 1:
    com = 0x2F8;
    break;
  case 1 + 2:
    com = 0x3E8;
    break;
  case 1 + 3:
    com = 0x2E8;
    break;
  }
  if (!(inb(com + 5) & 0x01))
    return -1;
  return inb(com);
}


x86 assembly (clang, -O0):
<serial_getc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x3,%eax
ja     116f <serial_getc+0x5f>
mov    -0x18(%rbp),%rax
lea    0xec5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x3f8,-0xc(%rbp)
jmp    116f <serial_getc+0x5f>
movl   $0x2f8,-0xc(%rbp)
jmp    116f <serial_getc+0x5f>
movl   $0x3e8,-0xc(%rbp)
jmp    116f <serial_getc+0x5f>
movl   $0x2e8,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x5,%edi
call   1030 <inb@plt>
and    $0x1,%eax
cmp    $0x0,%eax
jne    1192 <serial_getc+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <serial_getc+0x8d>
mov    -0xc(%rbp),%edi
call   1030 <inb@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_getc>:
push   %rbx
add    $0xfffffffe,%edi
mov    $0x3f8,%ebx
cmp    $0x2,%edi
ja     112b <serial_getc+0x1b>
movslq %edi,%rax
lea    0xed8(%rip),%rcx
mov    (%rcx,%rax,4),%ebx
mov    %ebx,%edi
or     $0x5,%edi
call   1030 <inb@plt>
test   $0x1,%al
jne    1140 <serial_getc+0x30>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    %ebx,%edi
pop    %rbx
jmp    1030 <inb@plt>

LLVM IR (-O0):
define i32 @serial_getc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
switch i32 %5, label %10 [
i32 1, label %6
i32 2, label %7
i32 3, label %8
i32 4, label %9
]
6: ; preds = %1
store i32 1016, i32* %4
br label %10
7: ; preds = %1
store i32 760, i32* %4
br label %10
8: ; preds = %1
store i32 1000, i32* %4
br label %10
9: ; preds = %1
store i32 744, i32* %4
br label %10
10: ; preds = %1, %9, %8, %7, %6
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 5
%13 = call i32 @inb(i32 %12)
%14 = and i32 %13, 1
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %10
store i32 -1, i32* %2
br label %20
17: ; preds = %10
%18 = load i32, i32* %4
%19 = call i32 @inb(i32 %18)
store i32 %19, i32* %2
br label %20
20: ; preds = %17, %16
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@switch.table.serial_getc = [3 x i32] [i32 760, i32 1000, i32 744]
define i32 @serial_getc(i32 %0) {
%2 = add i32 %0, -2
%3 = icmp ult i32 %2, 3
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds [3 x i32], [3 x i32]* @switch.table.serial_getc, i64 0, i64 %5
%7 = load i32, i32* %6
br label %8
8: ; preds = %4, %1
%9 = phi i32 [ 1016, %1 ], [ %7, %4 ]
%10 = or i32 %9, 5
%11 = tail call i32 @inb(i32 %10)
%12 = and i32 %11, 1
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @inb(i32 %9)
br label %16
16: ; preds = %8, %14
%17 = phi i32 [ %15, %14 ], [ -1, %8 ]
ret i32 %17
}