#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_iflag;
  int c_lflag;
  int c_oflag;
  int c_cflag;
};

/* Variables and functions */
int ECHO;
int ECHONL;
scalar_t__ errno;
scalar_t__ isinListX(char const *, char *, char *);
scalar_t__ tcgetattr(int, struct termios *);
int Gtty(int fd, const char *mode) {
  struct termios ta;
  errno = 0;
  if (tcgetattr(fd, &ta) != 0) {
    return -2;
  }
  if (isinListX(mode, "all", "c")) {
    return (ta.c_iflag << 16) | ta.c_lflag;
  }
  if (isinListX(mode, "iflags", "c"))
    return ta.c_iflag;
  if (isinListX(mode, "oflags", "c"))
    return ta.c_oflag;
  if (isinListX(mode, "lflags", "c"))
    return ta.c_lflag;
  if (isinListX(mode, "cflags", "c"))
    return ta.c_cflag;
  if (isinListX(mode, "echo", "c")) {
    return ta.c_lflag & (ECHO | ECHONL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<Gtty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ea2(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <tcgetattr@plt>
cmp    $0x0,%rax
je     115f <Gtty+0x3f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1288 <Gtty+0x168>
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
lea    0xe93(%rip),%rdx
call   1040 <isinListX@plt>
cmp    $0x0,%rax
je     1191 <Gtty+0x71>
mov    -0x20(%rbp),%eax
shl    $0x10,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <Gtty+0x168>
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
lea    0xe61(%rip),%rdx
call   1040 <isinListX@plt>
cmp    $0x0,%rax
je     11bd <Gtty+0x9d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <Gtty+0x168>
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
lea    0xe35(%rip),%rdx
call   1040 <isinListX@plt>
cmp    $0x0,%rax
je     11e9 <Gtty+0xc9>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <Gtty+0x168>
mov    -0x10(%rbp),%rdi
lea    0xe20(%rip),%rsi
lea    0xe09(%rip),%rdx
call   1040 <isinListX@plt>
cmp    $0x0,%rax
je     1215 <Gtty+0xf5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <Gtty+0x168>
mov    -0x10(%rbp),%rdi
lea    0xdfb(%rip),%rsi
lea    0xddd(%rip),%rdx
call   1040 <isinListX@plt>
cmp    $0x0,%rax
je     1241 <Gtty+0x121>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <Gtty+0x168>
mov    -0x10(%rbp),%rdi
lea    0xdd6(%rip),%rsi
lea    0xdb1(%rip),%rdx
call   1040 <isinListX@plt>
cmp    $0x0,%rax
je     1281 <Gtty+0x161>
mov    -0x1c(%rbp),%eax
mov    0x2d7c(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d5b(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1288 <Gtty+0x168>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Gtty>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x2ea9(%rip),%rax
movq   $0x0,(%rax)
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
mov    %rax,%rcx
mov    $0xfffffffe,%eax
test   %rcx,%rcx
jne    1234 <Gtty+0x114>
lea    0xeaa(%rip),%rsi
lea    0xea7(%rip),%rdx
mov    %rbx,%rdi
call   1040 <isinListX@plt>
test   %rax,%rax
je     1179 <Gtty+0x59>
mov    (%rsp),%eax
shl    $0x10,%eax
or     0x4(%rsp),%eax
jmp    1234 <Gtty+0x114>
lea    0xe86(%rip),%rsi
lea    0xe7d(%rip),%rdx
mov    %rbx,%rdi
call   1040 <isinListX@plt>
test   %rax,%rax
je     119c <Gtty+0x7c>
mov    (%rsp),%eax
jmp    1234 <Gtty+0x114>
lea    0xe6a(%rip),%rsi
lea    0xe5a(%rip),%rdx
mov    %rbx,%rdi
call   1040 <isinListX@plt>
test   %rax,%rax
je     11bd <Gtty+0x9d>
mov    0x8(%rsp),%eax
jmp    1234 <Gtty+0x114>
lea    0xe50(%rip),%rsi
lea    0xe39(%rip),%rdx
mov    %rbx,%rdi
call   1040 <isinListX@plt>
test   %rax,%rax
je     11de <Gtty+0xbe>
mov    0x4(%rsp),%eax
jmp    1234 <Gtty+0x114>
lea    0xe36(%rip),%rsi
lea    0xe18(%rip),%rdx
mov    %rbx,%rdi
call   1040 <isinListX@plt>
test   %rax,%rax
je     11ff <Gtty+0xdf>
mov    0xc(%rsp),%eax
jmp    1234 <Gtty+0x114>
lea    0xe1c(%rip),%rsi
lea    0xdf7(%rip),%rdx
mov    %rbx,%rdi
call   1040 <isinListX@plt>
test   %rax,%rax
je     1232 <Gtty+0x112>
mov    0x2dc7(%rip),%rcx
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
and    0x4(%rsp),%eax
jmp    1234 <Gtty+0x114>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@errno = global i64 0
@.str = [4 x i8] c"all\00"
@.str.1 = [2 x i8] c"c\00"
@.str.2 = [7 x i8] c"iflags\00"
@.str.3 = [7 x i8] c"oflags\00"
@.str.4 = [7 x i8] c"lflags\00"
@.str.5 = [7 x i8] c"cflags\00"
@.str.6 = [5 x i8] c"echo\00"
@ECHO = global i32 0
@ECHONL = global i32 0
define i32 @Gtty(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 0, i64* @errno
%7 = load i32, i32* %4
%8 = call i64 @tcgetattr(i32 %7, %struct.termios* %6)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -2, i32* %3
br label %62
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = call i64 @isinListX(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = shl i32 %17, 16
%19 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%20 = load i32, i32* %19
%21 = or i32 %18, %20
store i32 %21, i32* %3
br label %62
22: ; preds = %11
%23 = load i8*, i8** %5
%24 = call i64 @isinListX(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%28 = load i32, i32* %27
store i32 %28, i32* %3
br label %62
29: ; preds = %22
%30 = load i8*, i8** %5
%31 = call i64 @isinListX(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%35 = load i32, i32* %34
store i32 %35, i32* %3
br label %62
36: ; preds = %29
%37 = load i8*, i8** %5
%38 = call i64 @isinListX(i8* %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%42 = load i32, i32* %41
store i32 %42, i32* %3
br label %62
43: ; preds = %36
%44 = load i8*, i8** %5
%45 = call i64 @isinListX(i8* %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%49 = load i32, i32* %48
store i32 %49, i32* %3
br label %62
50: ; preds = %43
%51 = load i8*, i8** %5
%52 = call i64 @isinListX(i8* %51, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load i32, i32* @ECHO
%58 = load i32, i32* @ECHONL
%59 = or i32 %57, %58
%60 = and i32 %56, %59
store i32 %60, i32* %3
br label %62
61: ; preds = %50
store i32 0, i32* %3
br label %62
62: ; preds = %61, %54, %47, %40, %33, %26, %15, %10
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@errno = global i64 0
@.str = [4 x i8] c"all\00"
@.str.1 = [2 x i8] c"c\00"
@.str.2 = [7 x i8] c"iflags\00"
@.str.3 = [7 x i8] c"oflags\00"
@.str.4 = [7 x i8] c"lflags\00"
@.str.5 = [7 x i8] c"cflags\00"
@.str.6 = [5 x i8] c"echo\00"
@ECHO = global i32 0
@ECHONL = global i32 0
define i32 @Gtty(i32 %0, i8* %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %51
7: ; preds = %2
%8 = call i64 @isinListX(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = shl i32 %12, 16
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%15 = load i32, i32* %14
%16 = or i32 %13, %15
br label %51
17: ; preds = %7
%18 = call i64 @isinListX(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%22 = load i32, i32* %21
br label %51
23: ; preds = %17
%24 = call i64 @isinListX(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%28 = load i32, i32* %27
br label %51
29: ; preds = %23
%30 = call i64 @isinListX(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%34 = load i32, i32* %33
br label %51
35: ; preds = %29
%36 = call i64 @isinListX(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%40 = load i32, i32* %39
br label %51
41: ; preds = %35
%42 = call i64 @isinListX(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %41
%45 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%46 = load i32, i32* %45
%47 = load i32, i32* @ECHO
%48 = load i32, i32* @ECHONL
%49 = or i32 %48, %47
%50 = and i32 %49, %46
br label %51
51: ; preds = %41, %2, %44, %38, %32, %26, %20, %10
%52 = phi i32 [ %16, %10 ], [ %22, %20 ], [ %28, %26 ], [ %34, %32 ], [ %40, %38 ], [ %50, %44 ], [ -2, %2 ], [ 0, %41 ]
ret i32 %52
}