#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 EOF;
int getch();
char getchar();
int /*<<< orphan*/ print_usage();
int /*<<< orphan*/ printf(char *, ...);
int ungetch(int);
main() {
  char c;
  int c_get;
  char *prompt = ">";
  print_usage();
  printf("%s", prompt);
  while (c = getchar()) {
    switch (c) {
    case 'g':
      c_get = getch();
      printf("returned by getch: %i %c\n", c_get, c_get);
      break;
    case 'u':
      c_get = getchar();
      if (ungetch(c_get) != -5) {
        printf("pushed back: %i %c\n", c_get, c_get);
        ++c_get;
      } else {
        printf("error -- buffer overflow -- character has not been pushed "
               "back: %i %c\n",
               c_get, c_get);
      }
      break;
    case 's':
      printf("BUF_SIZE: %i\n", 2);
      break;
    case 'o':
      printf("BUF_OVERFLOW: %i\n", -5);
      break;
    case 'e':
      ungetch('\n');
      printf("'\\n' has been pushed back: %i\n", '\n');
      break;
    case 'E':
      ungetch(EOF);
      printf("EOF has been pushed back: %i\n", EOF);
      break;
    case 'H':
      print_usage();
      break;
    case 'Q':
      return 0;
    default:
      print_usage();
      break;
    }
    while (getchar() != '\n') {
    }
    printf("%s", prompt);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xf5e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <print_usage@plt>
mov    -0x18(%rbp),%rsi
lea    0xf4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x5(%rbp)
cmp    $0x0,%al
je     130a <main+0x1ba>
movsbl -0x5(%rbp),%eax
add    $0xffffffbb,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x30,%eax
ja     12cf <main+0x17f>
mov    -0x20(%rbp),%rax
lea    0xe4a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x0,%al
call   1060 <getch@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xef3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d6 <main+0x186>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <ungetch@plt>
cmp    $0xfffffffb,%eax
je     1222 <main+0xd2>
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xf09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1236 <main+0xe6>
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d6 <main+0x186>
lea    0xee8(%rip),%rdi
mov    $0x2,%esi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d6 <main+0x186>
lea    0xede(%rip),%rdi
mov    $0xfffffffb,%esi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d6 <main+0x186>
mov    $0xa,%edi
call   1050 <ungetch@plt>
lea    0xece(%rip),%rdi
mov    $0xa,%esi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d6 <main+0x186>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1050 <ungetch@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d6 <main+0x186>
mov    $0x0,%al
call   1070 <print_usage@plt>
jmp    12d6 <main+0x186>
movl   $0x0,-0x4(%rbp)
jmp    130a <main+0x1ba>
mov    $0x0,%al
call   1070 <print_usage@plt>
jmp    12db <main+0x18b>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     12f3 <main+0x1a3>
jmp    12db <main+0x18b>
mov    -0x18(%rbp),%rsi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1183 <main+0x33>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <print_usage@plt>
mov    $0x3e,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
test   %al,%al
je     1284 <main+0x124>
lea    0xe6e(%rip),%r14
mov    0x2e4f(%rip),%r15
lea    0xf24(%rip),%r12
lea    0xf8c(%rip),%r13
lea    0xf77(%rip),%rbx
xchg   %ax,%ax
movsbl %al,%eax
add    $0xffffffbb,%eax
cmp    $0x30,%eax
ja     11c4 <main+0x64>
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
xor    %eax,%eax
call   1080 <print_usage@plt>
jmp    1260 <main+0x100>
mov    (%r15),%edi
call   1060 <ungetch@plt>
mov    (%r15),%esi
lea    0xf82(%rip),%rdi
jmp    121e <main+0xbe>
mov    $0xa,%edi
call   1060 <ungetch@plt>
lea    0xf50(%rip),%rdi
mov    $0xa,%esi
jmp    121e <main+0xbe>
xor    %eax,%eax
call   1070 <getch@plt>
mov    %r12,%rdi
mov    %eax,%esi
mov    %eax,%edx
jmp    1251 <main+0xf1>
mov    %r13,%rdi
mov    $0xfffffffb,%esi
jmp    121e <main+0xbe>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1260 <main+0x100>
xor    %eax,%eax
call   1050 <getchar@plt>
movsbl %al,%ebp
mov    %ebp,%edi
call   1060 <ungetch@plt>
cmp    $0xfffffffb,%eax
jne    1246 <main+0xe6>
lea    0xe9a(%rip),%rdi
jmp    124d <main+0xed>
lea    0xec4(%rip),%rdi
mov    %ebp,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%al
jne    1260 <main+0x100>
mov    $0x3e,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
test   %al,%al
jne    11b0 <main+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c">\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [26 x i8] c"returned by getch: %i %c\0A\00"
@.str.3 = [20 x i8] c"pushed back: %i %c\0A\00"
@.str.4 = [71 x i8] c"error -- buffer overflow -- character has not been pushed back: %i %c\0A\00"
@.str.5 = [14 x i8] c"BUF_SIZE: %i\0A\00"
@.str.6 = [18 x i8] c"BUF_OVERFLOW: %i\0A\00"
@.str.7 = [31 x i8] c"'\\n' has been pushed back: %i\0A\00"
@EOF = global i32 0
@.str.8 = [30 x i8] c"EOF has been pushed back: %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = call i32 (...) @print_usage()
%6 = load i8*, i8** %4
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %59, %0
%9 = call signext i8 (...) @getchar()
store i8 %9, i8* %2
%10 = icmp ne i8 %9, 0
br i1 %10, label %11, label %62
11: ; preds = %8
%12 = load i8, i8* %2
%13 = sext i8 %12 to i32
switch i32 %13, label %51 [
i32 103, label %14
i32 117, label %19
i32 115, label %36
i32 111, label %38
i32 101, label %40
i32 69, label %43
i32 72, label %48
i32 81, label %50
]
14: ; preds = %11
%15 = call i32 (...) @getch()
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = load i32, i32* %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 %17)
br label %53
19: ; preds = %11
%20 = call signext i8 (...) @getchar()
%21 = sext i8 %20 to i32
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = call i32 @ungetch(i32 %22)
%24 = icmp ne i32 %23, -5
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i32, i32* %3
%27 = load i32, i32* %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %35
31: ; preds = %19
%32 = load i32, i32* %3
%33 = load i32, i32* %3
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %33)
br label %35
35: ; preds = %31, %25
br label %53
36: ; preds = %11
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 2)
br label %53
38: ; preds = %11
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 -5)
br label %53
40: ; preds = %11
%41 = call i32 @ungetch(i32 10)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 10)
br label %53
43: ; preds = %11
%44 = load i32, i32* @EOF
%45 = call i32 @ungetch(i32 %44)
%46 = load i32, i32* @EOF
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %46)
br label %53
48: ; preds = %11
%49 = call i32 (...) @print_usage()
br label %53
50: ; preds = %11
store i32 0, i32* %1
br label %62
51: ; preds = %11
%52 = call i32 (...) @print_usage()
br label %53
53: ; preds = %51, %48, %43, %40, %38, %36, %35, %14
br label %54
54: ; preds = %58, %53
%55 = call signext i8 (...) @getchar()
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %56, 10
br i1 %57, label %58, label %59
58: ; preds = %54
br label %54
59: ; preds = %54
%60 = load i8*, i8** %4
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %60)
br label %8
62: ; preds = %50, %8
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
@.str.2 = [26 x i8] c"returned by getch: %i %c\0A\00"
@.str.3 = [20 x i8] c"pushed back: %i %c\0A\00"
@.str.4 = [71 x i8] c"error -- buffer overflow -- character has not been pushed back: %i %c\0A\00"
@.str.5 = [14 x i8] c"BUF_SIZE: %i\0A\00"
@.str.6 = [18 x i8] c"BUF_OVERFLOW: %i\0A\00"
@.str.7 = [31 x i8] c"'\\n' has been pushed back: %i\0A\00"
@EOF = global i32 0
@.str.8 = [30 x i8] c"EOF has been pushed back: %i\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @print_usage()
%2 = tail call i32 @putchar(i32 62)
%3 = tail call signext i8 (...) @getchar()
%4 = icmp eq i8 %3, 0
br i1 %4, label %44, label %5
5: ; preds = %0, %40
%6 = phi i8 [ %42, %40 ], [ %3, %0 ]
%7 = sext i8 %6 to i32
switch i32 %7, label %34 [
i32 103, label %8
i32 117, label %11
i32 115, label %20
i32 111, label %22
i32 101, label %24
i32 69, label %27
i32 72, label %32
i32 81, label %44
]
8: ; preds = %5
%9 = tail call i32 (...) @getch()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 %9)
br label %36
11: ; preds = %5
%12 = tail call signext i8 (...) @getchar()
%13 = sext i8 %12 to i32
%14 = tail call i32 @ungetch(i32 %13)
%15 = icmp eq i32 %14, -5
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %13, i32 %13)
br label %36
18: ; preds = %11
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([71 x i8], [71 x i8]* @.str.4, i64 0, i64 0), i32 %13, i32 %13)
br label %36
20: ; preds = %5
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 2)
br label %36
22: ; preds = %5
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 -5)
br label %36
24: ; preds = %5
%25 = tail call i32 @ungetch(i32 10)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 10)
br label %36
27: ; preds = %5
%28 = load i32, i32* @EOF
%29 = tail call i32 @ungetch(i32 %28)
%30 = load i32, i32* @EOF
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %30)
br label %36
32: ; preds = %5
%33 = tail call i32 (...) @print_usage()
br label %36
34: ; preds = %5
%35 = tail call i32 (...) @print_usage()
br label %36
36: ; preds = %16, %18, %34, %32, %27, %24, %22, %20, %8
br label %37
37: ; preds = %36, %37
%38 = tail call signext i8 (...) @getchar()
%39 = icmp eq i8 %38, 10
br i1 %39, label %40, label %37
40: ; preds = %37
%41 = tail call i32 @putchar(i32 62)
%42 = tail call signext i8 (...) @getchar()
%43 = icmp eq i8 %42, 0
br i1 %43, label %44, label %5
44: ; preds = %5, %40, %0
ret i32 0
}