#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ system(char *);
ttyin() {
  char buf[BUFSIZ];
  FILE *efopen();
  static FILE *tty = NULL;
  if (tty == NULL)
    tty = efopen("/dev/tty", "r");
  for (;;) {
    if (fgets(buf, BUFSIZ, tty) == NULL || buf[0] == 'q')
      exit(0);
    else if (buf[0] == '!') {
      system(buf + 1);
      printf("!\n");
    } else
      return buf[0];
  }
}


x86 assembly (clang, -O0):
<ttyin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x8(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x10(%rbp)
cmpq   $0x0,0x2ecc(%rip)
jne    11ae <ttyin+0x5e>
lea    0xe67(%rip),%rdi
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1050 <efopen@plt>
mov    %rax,0x2eaa(%rip)
jmp    11b3 <ttyin+0x63>
mov    -0x18(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11e6 <ttyin+0x96>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
jne    11ed <ttyin+0x9d>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    121b <ttyin+0xcb>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <system@plt>
lea    0xdfa(%rip),%rdi
call   1040 <printf@plt>
jmp    122e <ttyin+0xde>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    -0x8(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret
jmp    1233 <ttyin+0xe3>
jmp    11b3 <ttyin+0x63>

x86 assembly (clang, -O2):
<ttyin>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e7e(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2ed7(%rip),%rdx
test   %rdx,%rdx
jne    11a9 <ttyin+0x59>
lea    0xe73(%rip),%rdi
lea    0xe75(%rip),%rsi
xor    %eax,%eax
call   1050 <efopen@plt>
mov    %rax,%rdx
mov    %rax,0x2eb3(%rip)
mov    (%r12),%esi
mov    %rbx,%rdi
call   1060 <fgets@plt>
test   %rax,%rax
je     1213 <ttyin+0xc3>
lea    0x1(%rbx),%r14
lea    0xe4a(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%rbx),%eax
cmp    $0x21,%eax
jne    1202 <ttyin+0xb2>
mov    %r14,%rdi
call   1040 <system@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    (%r12),%esi
mov    0x2e65(%rip),%rdx
mov    %rbx,%rdi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11d0 <ttyin+0x80>
jmp    1213 <ttyin+0xc3>
cmp    $0x71,%al
je     1213 <ttyin+0xc3>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@ttyin.tty = internal global i32* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"!\0A\00"
define i32 @ttyin() {
%1 = alloca i8*
%2 = alloca i64
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = call i8* @llvm.stacksave()
store i8* %5, i8** %1
%6 = alloca i8, i64 %4
store i64 %4, i64* %2
%7 = load i32*, i32** @ttyin.tty
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32* (i8*, i8*, ...) bitcast (i32* (...)* @efopen to i32* (i8*, i8*, ...)*)(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** @ttyin.tty
br label %11
11: ; preds = %9, %0
br label %12
12: ; preds = %39, %11
%13 = load i32, i32* @BUFSIZ
%14 = load i32*, i32** @ttyin.tty
%15 = call i32* @fgets(i8* %6, i32 %13, i32* %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = getelementptr inbounds i8, i8* %6, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 113
br i1 %21, label %22, label %24
22: ; preds = %17, %12
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %17
%25 = getelementptr inbounds i8, i8* %6, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 33
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = getelementptr inbounds i8, i8* %6, i64 1
%31 = call i32 @system(i8* %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %38
33: ; preds = %24
%34 = getelementptr inbounds i8, i8* %6, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = load i8*, i8** %1
ret i32 %36
38: ; preds = %29
br label %39
39: ; preds = %38
br label %12
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@ttyin.tty = internal unnamed_addr global i32* null
@.str = [9 x i8] c"/dev/tty\00"
@.str.1 = [2 x i8] c"r\00"
@str = [2 x i8] c"!\00"
define i32 @ttyin() {
%1 = load i32, i32* @BUFSIZ
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = load i32*, i32** @ttyin.tty
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32* (i8*, i8*, ...) bitcast (i32* (...)* @efopen to i32* (i8*, i8*, ...)*)(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** @ttyin.tty
%8 = load i32, i32* @BUFSIZ
br label %9
9: ; preds = %6, %0
%10 = phi i32* [ %7, %6 ], [ %4, %0 ]
%11 = phi i32 [ %8, %6 ], [ %1, %0 ]
%12 = call i32* @fgets(i8* %3, i32 %11, i32* %10)
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %3, i64 1
br label %16
16: ; preds = %14, %20
%17 = load i8, i8* %3
switch i8 %17, label %27 [
i8 113, label %18
i8 33, label %20
]
18: ; preds = %20, %16, %9
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %16
%21 = call i32 @system(i8* %15)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @BUFSIZ
%24 = load i32*, i32** @ttyin.tty
%25 = call i32* @fgets(i8* %3, i32 %23, i32* %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %18, label %16
27: ; preds = %16
%28 = sext i8 %17 to i32
ret i32 %28
}