#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*/ putchar(int);
int /*<<< orphan*/ ungetch(char);
main() {
  int i, c;
  char s[] = "hello, world";
  ungetch(EOF);
  i = 0;
  while (s[i++] != '\0')
    ungetch(s[i]);
  while ((c = getch()) != EOF)
    putchar(c);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0x19(%rbp)
mov    0xeb8(%rip),%eax
mov    %eax,-0x11(%rbp)
mov    0xeb3(%rip),%al
mov    %al,-0xd(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
movsbl %al,%edi
call   1040 <ungetch@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
cltq
movsbl -0x19(%rbp,%rax,1),%eax
cmp    $0x0,%eax
je     11a2 <main+0x72>
movslq -0x8(%rbp),%rax
movsbl -0x19(%rbp,%rax,1),%edi
call   1040 <ungetch@plt>
jmp    1174 <main+0x44>
jmp    11a7 <main+0x77>
mov    $0x0,%al
call   1050 <getch@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
je     11cd <main+0x9d>
mov    -0xc(%rbp),%edi
call   1030 <putchar@plt>
jmp    11a7 <main+0x77>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2eb0(%rip),%rbx
movsbl (%rbx),%edi
call   1040 <ungetch@plt>
mov    $0x65,%edi
call   1040 <ungetch@plt>
mov    $0x6c,%edi
call   1040 <ungetch@plt>
mov    $0x6c,%edi
call   1040 <ungetch@plt>
mov    $0x6f,%edi
call   1040 <ungetch@plt>
mov    $0x2c,%edi
call   1040 <ungetch@plt>
mov    $0x20,%edi
call   1040 <ungetch@plt>
mov    $0x77,%edi
call   1040 <ungetch@plt>
mov    $0x6f,%edi
call   1040 <ungetch@plt>
mov    $0x72,%edi
call   1040 <ungetch@plt>
mov    $0x6c,%edi
call   1040 <ungetch@plt>
mov    $0x64,%edi
call   1040 <ungetch@plt>
xor    %edi,%edi
call   1040 <ungetch@plt>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1050 <getch@plt>
cmp    (%rbx),%eax
je     11d4 <main+0xa4>
mov    %eax,%edi
call   1030 <putchar@plt>
jmp    11c0 <main+0x90>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@__const.main.s = [13 x i8] c"hello, world\00"
@EOF = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [13 x i8]
store i32 0, i32* %1
%5 = bitcast [13 x i8]* %4 to i8*
%6 = load i32, i32* @EOF
%7 = trunc i32 %6 to i8
%8 = call i32 @ungetch(i8 signext %7)
store i32 0, i32* %2
br label %9
9: ; preds = %17, %0
%10 = load i32, i32* %2
%11 = add nsw i32 %10, 1
store i32 %11, i32* %2
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %9
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = call i32 @ungetch(i8 signext %21)
br label %9
23: ; preds = %9
br label %24
24: ; preds = %28, %23
%25 = call i32 (...) @getch()
store i32 %25, i32* %3
%26 = load i32, i32* @EOF
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* %3
%30 = call i32 @putchar(i32 %29)
br label %24
31: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@EOF = global i32 0
define i32 @main() {
%1 = load i32, i32* @EOF
%2 = trunc i32 %1 to i8
%3 = tail call i32 @ungetch(i8 signext %2)
%4 = tail call i32 @ungetch(i8 signext 101)
%5 = tail call i32 @ungetch(i8 signext 108)
%6 = tail call i32 @ungetch(i8 signext 108)
%7 = tail call i32 @ungetch(i8 signext 111)
%8 = tail call i32 @ungetch(i8 signext 44)
%9 = tail call i32 @ungetch(i8 signext 32)
%10 = tail call i32 @ungetch(i8 signext 119)
%11 = tail call i32 @ungetch(i8 signext 111)
%12 = tail call i32 @ungetch(i8 signext 114)
%13 = tail call i32 @ungetch(i8 signext 108)
%14 = tail call i32 @ungetch(i8 signext 100)
%15 = tail call i32 @ungetch(i8 signext 0)
%16 = tail call i32 (...) @getch()
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %25, label %19
19: ; preds = %0, %19
%20 = phi i32 [ %22, %19 ], [ %16, %0 ]
%21 = tail call i32 @putchar(i32 %20)
%22 = tail call i32 (...) @getch()
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %19
25: ; preds = %19, %0
ret i32 0
}