#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 getchar();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ term_character();
int /*<<< orphan*/ term_exit();
int /*<<< orphan*/ term_init();
int term_testchar();
main() {
  int c;
  int count = 0;
  term_init();
  term_character();
  while ((c = term_testchar()) == -2)
    count++;
  printf("got %c after %d\n", c, count);
  while ((c = getchar()) != 'x') {
    printf("got '%c' = %x\n", c, c);
  }
  term_exit();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1080 <term_init@plt>
mov    $0x0,%al
call   1050 <term_character@plt>
mov    $0x0,%al
call   1030 <term_testchar@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xfffffffe,%eax
jne    11a5 <main+0x45>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1184 <main+0x24>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x78,%eax
je     11e1 <main+0x81>
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
jmp    11b7 <main+0x57>
mov    $0x0,%al
call   1070 <term_exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1080 <term_init@plt>
xor    %eax,%eax
call   1050 <term_character@plt>
mov    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <term_testchar@plt>
add    $0x1,%ebx
cmp    $0xfffffffe,%eax
je     1180 <main+0x20>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0x78,%eax
je     11d8 <main+0x78>
lea    0xe5f(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %eax,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0x78,%eax
jne    11c0 <main+0x60>
xor    %eax,%eax
call   1070 <term_exit@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"got %c after %d\0A\00"
@.str.1 = [15 x i8] c"got '%c' = %x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%4 = call i32 (...) @term_init()
%5 = call i32 (...) @term_character()
br label %6
6: ; preds = %9, %0
%7 = call i32 (...) @term_testchar()
store i32 %7, i32* %2
%8 = icmp eq i32 %7, -2
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* %3
%11 = add nsw i32 %10, 1
store i32 %11, i32* %3
br label %6
12: ; preds = %6
%13 = load i32, i32* %2
%14 = load i32, i32* %3
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
br label %16
16: ; preds = %19, %12
%17 = call i32 (...) @getchar()
store i32 %17, i32* %2
%18 = icmp ne i32 %17, 120
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* %2
%21 = load i32, i32* %2
%22 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
br label %16
23: ; preds = %16
%24 = call i32 (...) @term_exit()
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [17 x i8] c"got %c after %d\0A\00"
@.str.1 = [15 x i8] c"got '%c' = %x\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @term_init()
%2 = tail call i32 (...) @term_character()
br label %3
3: ; preds = %3, %0
%4 = phi i32 [ 0, %0 ], [ %7, %3 ]
%5 = tail call i32 (...) @term_testchar()
%6 = icmp eq i32 %5, -2
%7 = add nuw nsw i32 %4, 1
br i1 %6, label %3, label %8
8: ; preds = %3
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %4)
%10 = tail call i32 (...) @getchar()
%11 = icmp eq i32 %10, 120
br i1 %11, label %17, label %12
12: ; preds = %8, %12
%13 = phi i32 [ %15, %12 ], [ %10, %8 ]
%14 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %13)
%15 = tail call i32 (...) @getchar()
%16 = icmp eq i32 %15, 120
br i1 %16, label %17, label %12
17: ; preds = %12, %8
%18 = tail call i32 (...) @term_exit()
ret i32 0
}