#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();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ ungetch(int);
int /*<<< orphan*/ ungets(char *);
int main(void) {
  char s[100];
  size_t i;
  int c;
  printf("Reading 10 characters from input...\n");
  i = 0;
  c = getch();
  while ((c = getch()) != EOF && i < 10)
    s[i++] = (char)c;
  s[i] = '\0';
  printf("\t... result: \"%s\"\n", s);
  printf("Pushing back 1 character to input...\n");
  ungetch((int)s[--i]);
  s[i] = '\0';
  printf("\t... result: \"%s\"\n", s);
  printf("Reading 1 character from input...\n");
  s[i++] = (char)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++] = (char)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    %eax,-0x7c(%rbp)
mov    $0x0,%al
call   1060 <getch@plt>
mov    %eax,%ecx
mov    %ecx,-0x7c(%rbp)
xor    %eax,%eax
mov    0x2e61(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x7d(%rbp)
je     119d <main+0x5d>
cmpq   $0xa,-0x78(%rbp)
setb   %al
mov    %al,-0x7d(%rbp)
mov    -0x7d(%rbp),%al
test   $0x1,%al
jne    11ad <main+0x6d>
jmp    11ca <main+0x8a>
mov    -0x7c(%rbp),%eax
mov    %al,%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    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4c(%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    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe30(%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    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1030 <ungets@plt>
lea    -0x70(%rbp),%rsi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x78(%rbp)
mov    $0x0,%al
call   1060 <getch@plt>
mov    %eax,%ecx
mov    %ecx,-0x7c(%rbp)
xor    %eax,%eax
mov    0x2d28(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x7e(%rbp)
je     12d6 <main+0x196>
cmpq   $0xa,-0x78(%rbp)
setb   %al
mov    %al,-0x7e(%rbp)
mov    -0x7e(%rbp),%al
test   $0x1,%al
jne    12e6 <main+0x1a6>
jmp    1303 <main+0x1c3>
mov    -0x7c(%rbp),%eax
mov    %al,%cl
mov    -0x78(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x78(%rbp)
mov    %cl,-0x70(%rbp,%rax,1)
jmp    12ab <main+0x16b>
mov    -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xd0e(%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),%eax
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),%eax
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),%eax
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),%eax
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 i32 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 i32
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 i32 (...) @getch()
store i32 %6, i32* %4
br label %7
7: ; preds = %16, %0
%8 = call i32 (...) @getch()
store i32 %8, i32* %4
%9 = load i32, i32* @EOF
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i64, i64* %3
%13 = icmp ult i64 %12, 10
br label %14
14: ; preds = %11, %7
%15 = phi i1 [ false, %7 ], [ %13, %11 ]
br i1 %15, label %16, label %22
16: ; preds = %14
%17 = load i32, i32* %4
%18 = trunc i32 %17 to i8
%19 = load i64, i64* %3
%20 = add i64 %19, 1
store i64 %20, i64* %3
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %19
store i8 %18, i8* %21
br label %7
22: ; preds = %14
%23 = load i64, i64* %3
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%28 = load i64, i64* %3
%29 = add i64 %28, -1
store i64 %29, i64* %3
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = call i32 @ungetch(i32 %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 i32 (...) @getch()
%40 = trunc i32 %39 to i8
%41 = load i64, i64* %3
%42 = add i64 %41, 1
store i64 %42, i64* %3
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %41
store i8 %40, i8* %43
%44 = load i64, i64* %3
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %44
store i8 0, i8* %45
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%50 = call i32 @ungets(i8* %49)
%51 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %51)
%53 = 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 %54
54: ; preds = %63, %22
%55 = call i32 (...) @getch()
store i32 %55, i32* %4
%56 = load i32, i32* @EOF
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = load i64, i64* %3
%60 = icmp ult i64 %59, 10
br label %61
61: ; preds = %58, %54
%62 = phi i1 [ false, %54 ], [ %60, %58 ]
br i1 %62, label %63, label %69
63: ; preds = %61
%64 = load i32, i32* %4
%65 = trunc i32 %64 to i8
%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 %54
69: ; preds = %61
%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 i32 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 i32 (...) @getch()
%5 = tail call i32 (...) @getch()
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %19, label %8
8: ; preds = %0, %8
%9 = phi i32 [ %14, %8 ], [ %5, %0 ]
%10 = phi i64 [ %12, %8 ], [ 0, %0 ]
%11 = trunc i32 %9 to i8
%12 = add nuw nsw i64 %10, 1
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %10
store i8 %11, i8* %13
%14 = tail call i32 (...) @getch()
%15 = load i32, i32* @EOF
%16 = icmp ne i32 %14, %15
%17 = icmp ult i64 %10, 9
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %8, label %19
19: ; preds = %8, %0
%20 = phi i64 [ 0, %0 ], [ %12, %8 ]
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.5, i64 0, i64 0))
%24 = add nsw i64 %20, -1
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = call i32 @ungetch(i32 %27)
store i8 0, i8* %25
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
%31 = call i32 (...) @getch()
%32 = trunc i32 %31 to i8
store i8 %32, i8* %25
store i8 0, i8* %21
%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 ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0))
%35 = call i32 @ungets(i8* %2)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%38 = call i32 (...) @getch()
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %38, %39
br i1 %40, label %52, label %41
41: ; preds = %19, %41
%42 = phi i32 [ %47, %41 ], [ %38, %19 ]
%43 = phi i64 [ %45, %41 ], [ 0, %19 ]
%44 = trunc i32 %42 to i8
%45 = add nuw nsw i64 %43, 1
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %43
store i8 %44, i8* %46
%47 = call i32 (...) @getch()
%48 = load i32, i32* @EOF
%49 = icmp ne i32 %47, %48
%50 = icmp ult i64 %43, 9
%51 = select i1 %49, i1 %50, i1 false
br i1 %51, label %41, label %52
52: ; preds = %41, %19
%53 = phi i64 [ 0, %19 ], [ %45, %41 ]
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %53
store i8 0, i8* %54
%55 = 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
}