#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 /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ initTermios(int);
char read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ resetTermios();
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcflush(int /*<<< orphan*/, int /*<<< orphan*/);
int doit(int type) {
  char ch;
  initTermios(type);
  char b;
  if (!(type & 1))
    tcflush(fileno(stdin), TCIOFLUSH);
  ch = read(fileno(stdin), &b, 1);
  ch = b;
  if ((type & 1))
    tcflush(fileno(stdin), TCIOFLUSH);
  resetTermios();
  return ch;
}


x86 assembly (clang, -O0):
<doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <initTermios@plt>
mov    -0x4(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    1190 <doit+0x40>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x6(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %al,-0x5(%rbp)
mov    -0x6(%rbp),%al
mov    %al,-0x5(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11e4 <doit+0x94>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    $0x0,%al
call   1030 <resetTermios@plt>
movsbl -0x5(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
call   1070 <initTermios@plt>
test   $0x1,%bl
jne    1182 <doit+0x32>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <tcflush@plt>
mov    0x2e5f(%rip),%r14
mov    (%r14),%edi
call   1060 <fileno@plt>
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
movsbl 0xf(%rsp),%ebp
test   $0x1,%bl
je     11c4 <doit+0x74>
mov    (%r14),%edi
call   1060 <fileno@plt>
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <tcflush@plt>
xor    %eax,%eax
call   1030 <resetTermios@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@TCIOFLUSH = global i32 0
define i32 @doit(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @initTermios(i32 %5)
%7 = load i32, i32* %2
%8 = and i32 %7, 1
%9 = icmp ne i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i32, i32* @stdin
%12 = call i32 @fileno(i32 %11)
%13 = load i32, i32* @TCIOFLUSH
%14 = call i32 @tcflush(i32 %12, i32 %13)
br label %15
15: ; preds = %10, %1
%16 = load i32, i32* @stdin
%17 = call i32 @fileno(i32 %16)
%18 = call signext i8 @read(i32 %17, i8* %4, i32 1)
store i8 %18, i8* %3
%19 = load i8, i8* %4
store i8 %19, i8* %3
%20 = load i32, i32* %2
%21 = and i32 %20, 1
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %15
%24 = load i32, i32* @stdin
%25 = call i32 @fileno(i32 %24)
%26 = load i32, i32* @TCIOFLUSH
%27 = call i32 @tcflush(i32 %25, i32 %26)
br label %28
28: ; preds = %23, %15
%29 = call i32 (...) @resetTermios()
%30 = load i8, i8* %3
%31 = sext i8 %30 to i32
ret i32 %31
}

LLVM IR (-O2):
@stdin = global i32 0
@TCIOFLUSH = global i32 0
define i32 @doit(i32 %0) {
%2 = alloca i8
%3 = tail call i32 @initTermios(i32 %0)
%4 = and i32 %0, 1
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stdin
%8 = tail call i32 @fileno(i32 %7)
%9 = load i32, i32* @TCIOFLUSH
%10 = tail call i32 @tcflush(i32 %8, i32 %9)
br label %11
11: ; preds = %6, %1
%12 = load i32, i32* @stdin
%13 = tail call i32 @fileno(i32 %12)
%14 = call signext i8 @read(i32 %13, i8* %2, i32 1)
%15 = load i8, i8* %2
br i1 %5, label %21, label %16
16: ; preds = %11
%17 = load i32, i32* @stdin
%18 = tail call i32 @fileno(i32 %17)
%19 = load i32, i32* @TCIOFLUSH
%20 = tail call i32 @tcflush(i32 %18, i32 %19)
br label %21
21: ; preds = %16, %11
%22 = tail call i32 (...) @resetTermios()
%23 = sext i8 %15 to i32
ret i32 %23
}