#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 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);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x78(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %al,-0x79(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    %al,-0x79(%rbp)
movsbl %al,%ecx
mov    0x2e72(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x7a(%rbp)
je     1191 <main+0x61>
cmpq   $0xa,-0x78(%rbp)
setb   %al
mov    %al,-0x7a(%rbp)
mov    -0x7a(%rbp),%al
test   $0x1,%al
jne    11a1 <main+0x71>
jmp    11bc <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    1162 <main+0x32>
mov    -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x78(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x78(%rbp)
movsbl -0x71(%rbp,%rax,1),%edi
call   1040 <ungetch@plt>
mov    -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rsi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <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    0xdce(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xec1(%rip),%rdi
call   1030 <puts@plt>
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <getch@plt>
xor    %eax,%eax
call   1060 <getch@plt>
mov    0x2e79(%rip),%r14
cmp    (%r14),%al
je     119c <main+0x5c>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    0x1(%rbx),%r12
mov    %al,(%rsp,%rbx,1)
xor    %eax,%eax
call   1060 <getch@plt>
cmp    (%r14),%al
je     119c <main+0x5c>
cmp    $0x9,%rbx
mov    %r12,%rbx
jb     1180 <main+0x40>
movb   $0x0,(%rsp,%r12,1)
lea    0xe58(%rip),%r14
mov    %rsp,%r15
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
movsbl -0x1(%rsp,%r12,1),%edi
call   1050 <ungetch@plt>
movb   $0x0,-0x1(%rsp,%r12,1)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <getch@plt>
mov    %al,-0x1(%rsp,%r12,1)
movb   $0x0,(%rsp,%r12,1)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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"
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)
ret i32 0
}

LLVM IR (-O2):
@EOF = global i8 0
@.str.1 = [19 x i8] c"\09... result: \22%s\22\0A\00"
@str = [36 x i8] c"Reading 10 characters from input...\00"
@str.4 = [37 x i8] c"Pushing back 1 character to input...\00"
@str.5 = [34 x i8] c"Reading 1 character 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, 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.4, 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.5, 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)
ret i32 0
}