#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 */
char EOF;
char getch();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ ungetch(char);
int /*<<< orphan*/ ungets(char *);
int main(void) {
  char s[100];
  size_t i;
  char c;
  printf("Reading 10 characters from input...\n");
  i = 0;
  c = getch();
  while ((c = getch()) != EOF && i < 10)
    s[i++] = c;
  s[i] = '\0';
  printf("\t... result: \"%s\"\n", s);
  printf("Pushing back 1 character to input...\n");
  ungetch(s[--i]);
  s[i] = '\0';
  printf("\t... result: \"%s\"\n", s);
  printf("Reading 1 character from input...\n");
  s[i++] = getch();
  s[i] = '\0';
  printf("\t... result: \"%s\"\n", s);
  printf("Pushing back whole string to input...\n");
  ungets(s);
  printf("\t... result: \"%s\"\n", s);
  printf("Reading 10 characters from input...\n");
  i = 0;
  while ((c = getch()) != EOF && i < 10)
    s[i++] = c;
  s[i] = '\0';
  printf("\t... result: \"%s\"\n", s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x78(%rbp)
mov    $0x0,%al
call   1060 <getch@plt>
mov    %al,-0x79(%rbp)
mov    $0x0,%al
call   1060 <getch@plt>
mov    %al,-0x79(%rbp)
movsbl %al,%ecx
mov    0x2e62(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x7a(%rbp)
je     11a1 <main+0x61>
cmpq   $0xa,-0x78(%rbp)
setb   %al
mov    %al,-0x7a(%rbp)
mov    -0x7a(%rbp),%al
test   $0x1,%al
jne    11b1 <main+0x71>
jmp    11cc <main+0x8c>
mov    -0x79(%rbp),%cl
mov    -0x78(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x78(%rbp)
mov    %cl,-0x70(%rbp,%rax,1)
jmp    1172 <main+0x32>
mov    -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x78(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x78(%rbp)
movsbl -0x71(%rbp,%rax,1),%edi
call   1050 <ungetch@plt>
mov    -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getch@plt>
mov    %al,%cl
mov    -0x78(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x78(%rbp)
mov    %cl,-0x70(%rbp,%rax,1)
mov    -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1030 <ungets@plt>
lea    -0x70(%rbp),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x78(%rbp)
mov    $0x0,%al
call   1060 <getch@plt>
mov    %al,-0x79(%rbp)
movsbl %al,%ecx
mov    0x2d27(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x7b(%rbp)
je     12dc <main+0x19c>
cmpq   $0xa,-0x78(%rbp)
setb   %al
mov    %al,-0x7b(%rbp)
mov    -0x7b(%rbp),%al
test   $0x1,%al
jne    12ec <main+0x1ac>
jmp    1307 <main+0x1c7>
mov    -0x79(%rbp),%cl
mov    -0x78(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x78(%rbp)
mov    %cl,-0x70(%rbp,%rax,1)
jmp    12ad <main+0x16d>
mov    -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xf1b(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <getch@plt>
xor    %eax,%eax
call   1070 <getch@plt>
mov    0x2e67(%rip),%r12
cmp    (%r12),%al
je     11ad <main+0x5d>
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    0x1(%rbx),%rbp
mov    %al,(%rsp,%rbx,1)
xor    %eax,%eax
call   1070 <getch@plt>
cmp    (%r12),%al
je     11ad <main+0x5d>
cmp    $0x9,%rbx
mov    %rbp,%rbx
jb     1190 <main+0x40>
movb   $0x0,(%rsp,%rbp,1)
lea    0xe48(%rip),%r14
xor    %r13d,%r13d
mov    %rsp,%r15
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
movsbl -0x1(%rsp,%rbp,1),%edi
call   1060 <ungetch@plt>
movb   $0x0,-0x1(%rsp,%rbp,1)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <getch@plt>
mov    %al,-0x1(%rsp,%rbp,1)
movb   $0x0,(%rsp,%rbp,1)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1040 <ungets@plt>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <getch@plt>
cmp    (%r12),%al
je     127d <main+0x12d>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    0x1(%rbx),%r13
mov    %al,(%rsp,%rbx,1)
xor    %eax,%eax
call   1070 <getch@plt>
cmp    (%r12),%al
je     127d <main+0x12d>
cmp    $0x9,%rbx
mov    %r13,%rbx
jb     1260 <main+0x110>
movb   $0x0,(%rsp,%r13,1)
lea    0xd77(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Reading 10 characters from input...\0A\00"
@EOF = global i8 0
@.str.1 = [19 x i8] c"\09... result: \22%s\22\0A\00"
@.str.2 = [38 x i8] c"Pushing back 1 character to input...\0A\00"
@.str.3 = [35 x i8] c"Reading 1 character from input...\0A\00"
@.str.4 = [39 x i8] c"Pushing back whole string to input...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i64
%4 = alloca i8
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
%6 = call signext i8 (...) @getch()
store i8 %6, i8* %4
br label %7
7: ; preds = %18, %0
%8 = call signext i8 (...) @getch()
store i8 %8, i8* %4
%9 = sext i8 %8 to i32
%10 = load i8, i8* @EOF
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %9, %11
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = load i64, i64* %3
%15 = icmp ult i64 %14, 10
br label %16
16: ; preds = %13, %7
%17 = phi i1 [ false, %7 ], [ %15, %13 ]
br i1 %17, label %18, label %23
18: ; preds = %16
%19 = load i8, i8* %4
%20 = load i64, i64* %3
%21 = add i64 %20, 1
store i64 %21, i64* %3
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %20
store i8 %19, i8* %22
br label %7
23: ; preds = %16
%24 = load i64, i64* %3
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%29 = load i64, i64* %3
%30 = add i64 %29, -1
store i64 %30, i64* %3
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = call i32 @ungetch(i8 signext %32)
%34 = load i64, i64* %3
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%39 = call signext i8 (...) @getch()
%40 = load i64, i64* %3
%41 = add i64 %40, 1
store i64 %41, i64* %3
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %40
store i8 %39, i8* %42
%43 = load i64, i64* %3
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %43
store i8 0, i8* %44
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%49 = call i32 @ungets(i8* %48)
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %3
br label %53
53: ; preds = %64, %23
%54 = call signext i8 (...) @getch()
store i8 %54, i8* %4
%55 = sext i8 %54 to i32
%56 = load i8, i8* @EOF
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %55, %57
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = load i64, i64* %3
%61 = icmp ult i64 %60, 10
br label %62
62: ; preds = %59, %53
%63 = phi i1 [ false, %53 ], [ %61, %59 ]
br i1 %63, label %64, label %69
64: ; preds = %62
%65 = load i8, i8* %4
%66 = load i64, i64* %3
%67 = add i64 %66, 1
store i64 %67, i64* %3
%68 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %66
store i8 %65, i8* %68
br label %53
69: ; preds = %62
%70 = load i64, i64* %3
%71 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %70
store i8 0, i8* %71
%72 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %72)
ret i32 0
}

LLVM IR (-O2):
@EOF = global i8 0
@.str.1 = [19 x i8] c"\09... result: \22%s\22\0A\00"
@str.5 = [37 x i8] c"Pushing back 1 character to input...\00"
@str.6 = [34 x i8] c"Reading 1 character from input...\00"
@str.7 = [38 x i8] c"Pushing back whole string to input...\00"
@str.8 = [36 x i8] c"Reading 10 characters from input...\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%4 = tail call signext i8 (...) @getch()
%5 = tail call signext i8 (...) @getch()
%6 = load i8, i8* @EOF
%7 = icmp eq i8 %5, %6
br i1 %7, label %18, label %8
8: ; preds = %0, %8
%9 = phi i8 [ %13, %8 ], [ %5, %0 ]
%10 = phi i64 [ %11, %8 ], [ 0, %0 ]
%11 = add nuw nsw i64 %10, 1
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %10
store i8 %9, i8* %12
%13 = tail call signext i8 (...) @getch()
%14 = load i8, i8* @EOF
%15 = icmp ne i8 %13, %14
%16 = icmp ult i64 %10, 9
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %8, label %18
18: ; preds = %8, %0
%19 = phi i64 [ 0, %0 ], [ %11, %8 ]
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.5, i64 0, i64 0))
%23 = add nsw i64 %19, -1
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = call i32 @ungetch(i8 signext %25)
store i8 0, i8* %24
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
%29 = call signext i8 (...) @getch()
store i8 %29, i8* %24
store i8 0, i8* %20
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0))
%32 = call i32 @ungets(i8* %2)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%35 = call signext i8 (...) @getch()
%36 = load i8, i8* @EOF
%37 = icmp eq i8 %35, %36
br i1 %37, label %48, label %38
38: ; preds = %18, %38
%39 = phi i8 [ %43, %38 ], [ %35, %18 ]
%40 = phi i64 [ %41, %38 ], [ 0, %18 ]
%41 = add nuw nsw i64 %40, 1
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %40
store i8 %39, i8* %42
%43 = call signext i8 (...) @getch()
%44 = load i8, i8* @EOF
%45 = icmp ne i8 %43, %44
%46 = icmp ult i64 %40, 9
%47 = select i1 %45, i1 %46, i1 false
br i1 %47, label %38, label %48
48: ; preds = %38, %18
%49 = phi i64 [ 0, %18 ], [ %41, %38 ]
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %49
store i8 0, i8* %50
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
ret i32 0
}