#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 rdps();
int /*<<< orphan*/ uart_charav(int);
int /*<<< orphan*/ wtint(int /*<<< orphan*/);
int uart_getchar(int offset) {
  if ((rdps() & 7) == 0) {
    while (!uart_charav(offset)) {
      wtint(0);
    }
  } else {
    while (!uart_charav(offset))
      continue;
  }
  return inb(0x2f8 + offset);
}


x86 assembly (clang, -O0):
<uart_getchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1050 <rdps@plt>
and    $0x7,%eax
cmp    $0x0,%eax
jne    1191 <uart_getchar+0x51>
jmp    1163 <uart_getchar+0x23>
mov    -0x4(%rbp),%edi
call   1030 <uart_charav@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1180 <uart_getchar+0x40>
jmp    118c <uart_getchar+0x4c>
xor    %edi,%edi
call   1060 <wtint@plt>
jmp    1163 <uart_getchar+0x23>
jmp    11bd <uart_getchar+0x7d>
jmp    1196 <uart_getchar+0x56>
mov    -0x4(%rbp),%edi
call   1030 <uart_charav@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b3 <uart_getchar+0x73>
jmp    11b8 <uart_getchar+0x78>
jmp    1196 <uart_getchar+0x56>
jmp    11bd <uart_getchar+0x7d>
mov    -0x4(%rbp),%edi
add    $0x2f8,%edi
call   1040 <inb@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uart_getchar>:
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
call   1050 <rdps@plt>
test   $0x7,%al
je     1167 <uart_getchar+0x27>
xchg   %ax,%ax
mov    %ebx,%edi
call   1030 <uart_charav@plt>
test   %eax,%eax
je     1150 <uart_getchar+0x10>
jmp    1172 <uart_getchar+0x32>
nopl   (%rax)
xor    %edi,%edi
call   1060 <wtint@plt>
mov    %ebx,%edi
call   1030 <uart_charav@plt>
test   %eax,%eax
je     1160 <uart_getchar+0x20>
add    $0x2f8,%ebx
mov    %ebx,%edi
pop    %rbx
jmp    1040 <inb@plt>

LLVM IR (-O0):
define i32 @uart_getchar(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = call i32 (...) @rdps()
%4 = and i32 %3, 7
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %15
6: ; preds = %1
br label %7
7: ; preds = %12, %6
%8 = load i32, i32* %2
%9 = call i32 @uart_charav(i32 %8)
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = call i32 @wtint(i32 0)
br label %7
14: ; preds = %7
br label %23
15: ; preds = %1
br label %16
16: ; preds = %21, %15
%17 = load i32, i32* %2
%18 = call i32 @uart_charav(i32 %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %22
21: ; preds = %16
br label %16
22: ; preds = %16
br label %23
23: ; preds = %22, %14
%24 = load i32, i32* %2
%25 = add nsw i32 760, %24
%26 = call i32 @inb(i32 %25)
ret i32 %26
}

LLVM IR (-O2):
define i32 @uart_getchar(i32 %0) {
%2 = tail call i32 (...) @rdps()
%3 = and i32 %2, 7
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %1
%6 = tail call i32 @uart_charav(i32 %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %5, %8
%9 = tail call i32 @wtint(i32 0)
%10 = tail call i32 @uart_charav(i32 %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %8, label %15
12: ; preds = %1, %12
%13 = tail call i32 @uart_charav(i32 %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %12, label %15
15: ; preds = %12, %8, %5
%16 = add nsw i32 %0, 760
%17 = tail call i32 @inb(i32 %16)
ret i32 %17
}