#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 getchar();
char *llvm_gc_allocate(int);
char *__tiger__getchar(void) {
  char *s;
  char c = getchar();
  if (c == EOF) {
    s = llvm_gc_allocate(1);
    s[0] = '\0';
    return s;
  }
  s = llvm_gc_allocate(2);
  s[0] = c;
  s[1] = '\0';
  return s;
}


x86 assembly (clang, -O0):
<__tiger__getchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
mov    0x2eab(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    116a <__tiger__getchar+0x4a>
mov    $0x1,%edi
call   1040 <llvm_gc_allocate@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1191 <__tiger__getchar+0x71>
mov    $0x2,%edi
call   1040 <llvm_gc_allocate@plt>
mov    %rax,-0x10(%rbp)
mov    -0x11(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__tiger__getchar>:
push   %rbx
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
mov    0x2eb7(%rip),%rax
cmp    (%rax),%bl
jne    1144 <__tiger__getchar+0x24>
mov    $0x1,%edi
call   1040 <llvm_gc_allocate@plt>
mov    %rax,%rcx
jmp    1157 <__tiger__getchar+0x37>
mov    $0x2,%edi
call   1040 <llvm_gc_allocate@plt>
mov    %bl,(%rax)
mov    %rax,%rcx
add    $0x1,%rcx
movb   $0x0,(%rcx)
pop    %rbx
ret

LLVM IR (-O0):
@EOF = global i8 0
define i8* @__tiger__getchar() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8
%4 = call signext i8 (...) @getchar()
store i8 %4, i8* %3
%5 = load i8, i8* %3
%6 = sext i8 %5 to i32
%7 = load i8, i8* @EOF
%8 = sext i8 %7 to i32
%9 = icmp eq i32 %6, %8
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = call i8* @llvm_gc_allocate(i32 1)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
%14 = load i8*, i8** %2
store i8* %14, i8** %1
br label %23
15: ; preds = %0
%16 = call i8* @llvm_gc_allocate(i32 2)
store i8* %16, i8** %2
%17 = load i8, i8* %3
%18 = load i8*, i8** %2
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 %17, i8* %19
%20 = load i8*, i8** %2
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8 0, i8* %21
%22 = load i8*, i8** %2
store i8* %22, i8** %1
br label %23
23: ; preds = %15, %10
%24 = load i8*, i8** %1
ret i8* %24
}

LLVM IR (-O2):
@EOF = global i8 0
define i8* @__tiger__getchar() {
%1 = tail call signext i8 (...) @getchar()
%2 = load i8, i8* @EOF
%3 = icmp eq i8 %1, %2
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i8* @llvm_gc_allocate(i32 1)
br label %9
6: ; preds = %0
%7 = tail call i8* @llvm_gc_allocate(i32 2)
store i8 %1, i8* %7
%8 = getelementptr inbounds i8, i8* %7, i64 1
br label %9
9: ; preds = %6, %4
%10 = phi i8* [ %8, %6 ], [ %5, %4 ]
%11 = phi i8* [ %7, %6 ], [ %5, %4 ]
store i8 0, i8* %10
ret i8* %11
}