#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ ungetc(char, int /*<<< orphan*/ *);
int main() {
  char c;
  FILE *stream;
  stream = fopen("ungetc.txt", "r");
  while (1) {
    c = fgetc(stream);
    if (c == EOF) {
      perror("fgetc");
      break;
    } else if (c == '#') {
      ungetc('%', stream);
      ungetc('*', stream);
      ungetc('^', stream);
    } else {
      printf("%c", c);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
mov    0x2e5b(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11a9 <main+0x59>
lea    0xe6e(%rip),%rdi
call   1070 <perror@plt>
jmp    11ff <main+0xaf>
movsbl -0x5(%rbp),%eax
cmp    $0x23,%eax
jne    11e5 <main+0x95>
mov    -0x10(%rbp),%rsi
mov    $0x25,%edi
call   1050 <ungetc@plt>
mov    -0x10(%rbp),%rsi
mov    $0x2a,%edi
call   1050 <ungetc@plt>
mov    -0x10(%rbp),%rsi
mov    $0x5e,%edi
call   1050 <ungetc@plt>
jmp    11f5 <main+0xa5>
lea    0xe27(%rip),%rdi
movsbl -0x5(%rbp),%esi
call   1030 <printf@plt>
jmp    11fa <main+0xaa>
jmp    1176 <main+0x26>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xea4(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    0x2e6e(%rip),%r15
cmp    (%r15),%al
jne    1193 <main+0x43>
lea    0xe87(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe79(%rip),%r14
jmp    11b8 <main+0x68>
nopl   0x0(%rax)
movsbl %al,%esi
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%al
je     117f <main+0x2f>
cmp    $0x23,%al
jne    11a0 <main+0x50>
mov    $0x25,%edi
mov    %rbx,%rsi
call   1050 <ungetc@plt>
mov    $0x2a,%edi
mov    %rbx,%rsi
call   1050 <ungetc@plt>
mov    $0x5e,%edi
mov    %rbx,%rsi
call   1050 <ungetc@plt>
jmp    11ab <main+0x5b>

LLVM IR (-O0):
@.str = [11 x i8] c"ungetc.txt\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.2 = [6 x i8] c"fgetc\00"
@.str.3 = [3 x i8] c"%c\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
br label %5
5: ; preds = %0, %30
%6 = load i32*, i32** %3
%7 = call signext i8 @fgetc(i32* %6)
store i8 %7, i8* %2
%8 = load i8, i8* %2
%9 = sext i8 %8 to i32
%10 = load i8, i8* @EOF
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %9, %11
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %31
15: ; preds = %5
%16 = load i8, i8* %2
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 35
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i32*, i32** %3
%21 = call i32 @ungetc(i8 signext 37, i32* %20)
%22 = load i32*, i32** %3
%23 = call i32 @ungetc(i8 signext 42, i32* %22)
%24 = load i32*, i32** %3
%25 = call i32 @ungetc(i8 signext 94, i32* %24)
br label %29
26: ; preds = %15
%27 = load i8, i8* %2
%28 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8 signext %27)
br label %29
29: ; preds = %26, %19
br label %30
30: ; preds = %29
br label %5
31: ; preds = %13
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"ungetc.txt\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.2 = [6 x i8] c"fgetc\00"
@.str.3 = [3 x i8] c"%c\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call signext i8 @fgetc(i32* %1)
%3 = load i8, i8* @EOF
%4 = icmp eq i8 %2, %3
br i1 %4, label %5, label %7
5: ; preds = %16, %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
7: ; preds = %0, %16
%8 = phi i8 [ %17, %16 ], [ %2, %0 ]
%9 = icmp eq i8 %8, 35
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = tail call i32 @ungetc(i8 signext 37, i32* %1)
%12 = tail call i32 @ungetc(i8 signext 42, i32* %1)
%13 = tail call i32 @ungetc(i8 signext 94, i32* %1)
br label %16
14: ; preds = %7
%15 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8 signext %8)
br label %16
16: ; preds = %10, %14
%17 = tail call signext i8 @fgetc(i32* %1)
%18 = load i8, i8* @EOF
%19 = icmp eq i8 %17, %18
br i1 %19, label %5, label %7
}