#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 getch();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ ungets(char *);
main() {
  int i, c;
  char s[1000];
  char t[1000];
  i = 0;
  while ((s[i++] = getch()) != '\n')
    ;
  s[i] = '\0';
  printf("Reading: %s", s);
  ungets(s);
  printf("ungets called\n");
  i = 0;
  while ((t[i++] = getch()) != '\n')
    ;
  printf("Reading again: %s", s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x7f0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    -0x8(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x8(%rbp)
movslq %ecx,%rcx
mov    %al,-0x400(%rbp,%rcx,1)
movsbl %al,%eax
cmp    $0xa,%eax
je     1176 <main+0x46>
jmp    1149 <main+0x19>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x400(%rbp,%rax,1)
lea    -0x400(%rbp),%rsi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x400(%rbp),%rdi
call   1030 <ungets@plt>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getch@plt>
mov    -0x8(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x8(%rbp)
movslq %ecx,%rcx
mov    %al,-0x7f0(%rbp,%rcx,1)
movsbl %al,%eax
cmp    $0xa,%eax
je     11e5 <main+0xb5>
jmp    11b8 <main+0x88>
lea    -0x400(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x7f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x3f0,%rsp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getch@plt>
mov    %al,(%rsp,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%al
jne    1150 <main+0x10>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe91(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <ungets@plt>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <getch@plt>
cmp    $0xa,%al
jne    1190 <main+0x50>
lea    0xe6a(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Reading: %s\00"
@.str.1 = [15 x i8] c"ungets called\0A\00"
@.str.2 = [18 x i8] c"Reading again: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1000 x i8]
%5 = alloca [1000 x i8]
store i32 0, i32* %1
store i32 0, i32* %2
br label %6
6: ; preds = %14, %0
%7 = call signext i8 (...) @getch()
%8 = load i32, i32* %2
%9 = add nsw i32 %8, 1
store i32 %9, i32* %2
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 %10
store i8 %7, i8* %11
%12 = sext i8 %7 to i32
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %15
14: ; preds = %6
br label %6
15: ; preds = %6
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%22 = call i32 @ungets(i8* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %24
24: ; preds = %32, %15
%25 = call signext i8 (...) @getch()
%26 = load i32, i32* %2
%27 = add nsw i32 %26, 1
store i32 %27, i32* %2
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 %28
store i8 %25, i8* %29
%30 = sext i8 %25 to i32
%31 = icmp ne i32 %30, 10
br i1 %31, label %32, label %33
32: ; preds = %24
br label %24
33: ; preds = %24
%34 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"Reading: %s\00"
@.str.2 = [18 x i8] c"Reading again: %s\00"
@str = [14 x i8] c"ungets called\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %3, %0
%4 = phi i64 [ %6, %3 ], [ 0, %0 ]
%5 = tail call signext i8 (...) @getch()
%6 = add nuw i64 %4, 1
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %4
store i8 %5, i8* %7
%8 = icmp eq i8 %5, 10
br i1 %8, label %9, label %3
9: ; preds = %3
%10 = and i64 %6, 4294967295
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %2)
%13 = call i32 @ungets(i8* %2)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %15, %9
%16 = call signext i8 (...) @getch()
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %15
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %2)
ret i32 0
}