#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 */
struct termios {
  int *c_cc;
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
char getchar();
char screen_str(char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
char get_opt() {
  char str[4];
  int i;
  struct termios new_settings;
  struct termios old_settings;
  tcgetattr(0, &old_settings);
  new_settings = old_settings;
  new_settings.c_lflag &= (~ICANON);
  new_settings.c_cc[VTIME] = 0;
  new_settings.c_cc[VMIN] = 1;
  tcgetattr(0, &old_settings);
  tcsetattr(0, TCSANOW, &new_settings);
  for (i = 0; i < 3; i++) {
    str[i] = getchar();
    if ((str[i] != 'o') && (str[i] != 'q') && (str[i] != 'f')) {
      if (str[0] == 27) {
        continue;
      } else {
        i = -1;
      }
    } else {
      tcsetattr(0, TCSANOW, &old_settings);
      return str[i];
    }
  }
  tcsetattr(0, TCSANOW, &old_settings);
  return screen_str(str);
}


x86 assembly (clang, -O0):
<get_opt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    126b <get_opt+0x12b>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,%cl
movslq -0xc(%rbp),%rax
mov    %cl,-0x5(%rbp,%rax,1)
movslq -0xc(%rbp),%rax
movsbl -0x5(%rbp,%rax,1),%eax
cmp    $0x6f,%eax
je     1234 <get_opt+0xf4>
movslq -0xc(%rbp),%rax
movsbl -0x5(%rbp,%rax,1),%eax
cmp    $0x71,%eax
je     1234 <get_opt+0xf4>
movslq -0xc(%rbp),%rax
movsbl -0x5(%rbp,%rax,1),%eax
cmp    $0x66,%eax
je     1234 <get_opt+0xf4>
movsbl -0x5(%rbp),%eax
cmp    $0x1b,%eax
jne    1228 <get_opt+0xe8>
jmp    125d <get_opt+0x11d>
movl   $0xffffffff,-0xc(%rbp)
jmp    1258 <get_opt+0x118>
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
movslq -0xc(%rbp),%rax
mov    -0x5(%rbp,%rax,1),%al
mov    %al,-0x1(%rbp)
jmp    128b <get_opt+0x14b>
jmp    125d <get_opt+0x11d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c5 <get_opt+0x85>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1060 <tcsetattr@plt>
lea    -0x5(%rbp),%rdi
call   1040 <screen_str@plt>
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_opt>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
xor    %r15d,%r15d
lea    0x20(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
movups 0x20(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e39(%rip),%r14
mov    (%r14),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
mov    $0xa01,%r12d
xor    %ebp,%ebp
jmp    11e1 <get_opt+0xa1>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmpb   $0x1b,0xc(%rsp)
cmovne %r15d,%ebp
cmp    $0x3,%ebp
jge    1212 <get_opt+0xd2>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
movslq %ebp,%rax
mov    %bl,0xc(%rsp,%rax,1)
lea    -0x66(%rbx),%eax
cmp    $0xb,%al
ja     11d0 <get_opt+0x90>
movzbl %al,%eax
bt     %rax,%r12
jae    11d0 <get_opt+0x90>
mov    (%r14),%esi
lea    0x20(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
jmp    122d <get_opt+0xed>
mov    (%r14),%esi
lea    0x20(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
lea    0xc(%rsp),%rdi
call   1040 <screen_str@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@ICANON = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define signext i8 @get_opt() {
%1 = alloca i8
%2 = alloca [4 x i8]
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%7 = bitcast %struct.termios* %4 to i8*
%8 = bitcast %struct.termios* %5 to i8*
%9 = load i32, i32* @ICANON
%10 = xor i32 %9, -1
%11 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%12 = load i32, i32* %11
%13 = and i32 %12, %10
store i32 %13, i32* %11
%14 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%15 = load i32*, i32** %14
%16 = load i64, i64* @VTIME
%17 = getelementptr inbounds i32, i32* %15, i64 %16
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%19 = load i32*, i32** %18
%20 = load i64, i64* @VMIN
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32 1, i32* %21
%22 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 0, i32 %23, %struct.termios* %4)
store i32 0, i32* %3
br label %25
25: ; preds = %69, %0
%26 = load i32, i32* %3
%27 = icmp slt i32 %26, 3
br i1 %27, label %28, label %72
28: ; preds = %25
%29 = call signext i8 (...) @getchar()
%30 = load i32, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 %31
store i8 %29, i8* %32
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 111
br i1 %38, label %39, label %61
39: ; preds = %28
%40 = load i32, i32* %3
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 113
br i1 %45, label %46, label %61
46: ; preds = %39
%47 = load i32, i32* %3
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 102
br i1 %52, label %53, label %61
53: ; preds = %46
%54 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 27
br i1 %57, label %58, label %59
58: ; preds = %53
br label %69
59: ; preds = %53
store i32 -1, i32* %3
br label %60
60: ; preds = %59
br label %68
61: ; preds = %46, %39, %28
%62 = load i32, i32* @TCSANOW
%63 = call i32 @tcsetattr(i32 0, i32 %62, %struct.termios* %5)
%64 = load i32, i32* %3
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 %65
%67 = load i8, i8* %66
store i8 %67, i8* %1
br label %77
68: ; preds = %60
br label %69
69: ; preds = %68, %58
%70 = load i32, i32* %3
%71 = add nsw i32 %70, 1
store i32 %71, i32* %3
br label %25
72: ; preds = %25
%73 = load i32, i32* @TCSANOW
%74 = call i32 @tcsetattr(i32 0, i32 %73, %struct.termios* %5)
%75 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%76 = call signext i8 @screen_str(i8* %75)
store i8 %76, i8* %1
br label %77
77: ; preds = %72, %61
%78 = load i8, i8* %1
ret i8 %78
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@ICANON = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define signext i8 @get_opt() {
%1 = alloca [4 x i8]
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %3 to i8*
%7 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%8 = load i32, i32* @ICANON
%9 = xor i32 %8, -1
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%11 = load i32, i32* %10
%12 = and i32 %11, %9
store i32 %12, i32* %10
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%14 = load i32*, i32** %13
%15 = load i64, i64* @VTIME
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 0, i32* %16
%17 = load i64, i64* @VMIN
%18 = getelementptr inbounds i32, i32* %14, i64 %17
store i32 1, i32* %18
%19 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 0, i32 %20, %struct.termios* %2)
br label %22
22: ; preds = %0, %27
%23 = phi i32 [ 0, %0 ], [ %31, %27 ]
%24 = call signext i8 (...) @getchar()
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 %25
store i8 %24, i8* %26
switch i8 %24, label %27 [
i8 111, label %33
i8 113, label %33
i8 102, label %33
]
27: ; preds = %22
%28 = load i8, i8* %4
%29 = icmp eq i8 %28, 27
%30 = add nsw i32 %23, 1
%31 = select i1 %29, i32 %30, i32 0
%32 = icmp slt i32 %31, 3
br i1 %32, label %22, label %36
33: ; preds = %22, %22, %22
%34 = load i32, i32* @TCSANOW
%35 = call i32 @tcsetattr(i32 0, i32 %34, %struct.termios* %3)
br label %40
36: ; preds = %27
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 0, i32 %37, %struct.termios* %3)
%39 = call signext i8 @screen_str(i8* %4)
br label %40
40: ; preds = %36, %33
%41 = phi i8 [ %24, %33 ], [ %39, %36 ]
ret i8 %41
}