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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
int ungetc(char, int /*<<< orphan*/ *);
void test() {
  FILE *file;
  int err;
  char buffer[256];
  file = fopen("file.txt", "r");
  assert(file);
  rewind(file);
  err = ungetc(EOF, file);
  assert(err == EOF);
  err = ungetc('a', file);
  assert(err == (int)'a');
  rewind(file);
  ungetc('b', file);
  ungetc('a', file);
  err = fgetc(file);
  assert(err == (int)'a');
  int r = fread(buffer, sizeof(char), sizeof(buffer), file);
  assert(r == 3);
  buffer[3] = 0;
  assert(!strcmp(buffer, "bcd"));
  ungetc('a', file);
  rewind(file);
  err = fgetc(file);
  assert(err == (int)'c');
  ungetc('a', file);
  fseek(file, 0, SEEK_SET);
  err = fgetc(file);
  assert(err == (int)'c');
  fseek(file, 0, SEEK_END);
  err = fgetc(file);
  assert(err == EOF);
  err = feof(file);
  assert(err);
  ungetc('e', file);
  err = feof(file);
  assert(!err);
  fclose(file);
  puts("success");
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
lea    0xe3e(%rip),%rdi
lea    0xe40(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1060 <rewind@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rsi
movsbl %al,%edi
call   10b0 <ungetc@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rsi
mov    $0x61,%edi
call   10b0 <ungetc@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x61,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x8(%rbp),%rsi
mov    $0x62,%edi
call   10b0 <ungetc@plt>
mov    -0x8(%rbp),%rsi
mov    $0x61,%edi
call   10b0 <ungetc@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x61,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1040 <fread@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x3,-0x114(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
movb   $0x0,-0x10d(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xd47(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rsi
mov    $0x61,%edi
call   10b0 <ungetc@plt>
mov    -0x8(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x63,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rsi
mov    $0x61,%edi
call   10b0 <ungetc@plt>
mov    -0x8(%rbp),%rdi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x63,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    0x2c8f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2c68(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <feof@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rsi
mov    $0x65,%edi
call   10b0 <ungetc@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <feof@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xc37(%rip),%rdi
call   1030 <puts@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %r14
push   %rbx
sub    $0x108,%rsp
lea    0xe4f(%rip),%rdi
lea    0xe51(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbx
mov    %ebx,%edi
call   1080 <assert@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    0x2e0a(%rip),%r14
movsbl (%r14),%edi
mov    %rbx,%rsi
call   10a0 <ungetc@plt>
xor    %edi,%edi
cmp    (%r14),%eax
sete   %dil
call   1080 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   10a0 <ungetc@plt>
xor    %edi,%edi
cmp    $0x61,%eax
sete   %dil
call   1080 <assert@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    $0x62,%edi
mov    %rbx,%rsi
call   10a0 <ungetc@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   10a0 <ungetc@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
xor    %edi,%edi
cmp    $0x61,%eax
sete   %dil
call   1080 <assert@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
xor    %edi,%edi
cmp    $0x3,%eax
sete   %dil
call   1080 <assert@plt>
movb   $0x0,0x3(%rsp)
xor    %edi,%edi
cmpl   $0x646362,(%rsp)
sete   %dil
call   1080 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   10a0 <ungetc@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
xor    %edi,%edi
cmp    $0x63,%eax
sete   %dil
call   1080 <assert@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   10a0 <ungetc@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
xor    %edi,%edi
cmp    $0x63,%eax
sete   %dil
call   1080 <assert@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
xor    %edi,%edi
cmp    (%r14),%eax
sete   %dil
call   1080 <assert@plt>
mov    %rbx,%rdi
call   1090 <feof@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    $0x65,%edi
mov    %rbx,%rsi
call   10a0 <ungetc@plt>
mov    %rbx,%rdi
call   1090 <feof@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <assert@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xcc4(%rip),%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.2 = [4 x i8] c"bcd\00"
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.3 = [8 x i8] c"success\00"
define void @test() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %1
%6 = load i32*, i32** %1
%7 = ptrtoint i32* %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i32*, i32** %1
%10 = call i32 @rewind(i32* %9)
%11 = load i32, i32* @EOF
%12 = trunc i32 %11 to i8
%13 = load i32*, i32** %1
%14 = call i32 @ungetc(i8 signext %12, i32* %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32*, i32** %1
%21 = call i32 @ungetc(i8 signext 97, i32* %20)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 97
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32*, i32** %1
%27 = call i32 @rewind(i32* %26)
%28 = load i32*, i32** %1
%29 = call i32 @ungetc(i8 signext 98, i32* %28)
%30 = load i32*, i32** %1
%31 = call i32 @ungetc(i8 signext 97, i32* %30)
%32 = load i32*, i32** %1
%33 = call i32 @fgetc(i32* %32)
store i32 %33, i32* %2
%34 = load i32, i32* %2
%35 = icmp eq i32 %34, 97
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%39 = load i32*, i32** %1
%40 = call i32 @fread(i8* %38, i32 1, i32 256, i32* %39)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp eq i32 %41, 3
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 3
store i8 0, i8* %45
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%47 = call i32 @strcmp(i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp ne i32 %47, 0
%49 = xor i1 %48, true
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i32*, i32** %1
%53 = call i32 @ungetc(i8 signext 97, i32* %52)
%54 = load i32*, i32** %1
%55 = call i32 @rewind(i32* %54)
%56 = load i32*, i32** %1
%57 = call i32 @fgetc(i32* %56)
store i32 %57, i32* %2
%58 = load i32, i32* %2
%59 = icmp eq i32 %58, 99
%60 = zext i1 %59 to i32
%61 = call i32 @assert(i32 %60)
%62 = load i32*, i32** %1
%63 = call i32 @ungetc(i8 signext 97, i32* %62)
%64 = load i32*, i32** %1
%65 = load i32, i32* @SEEK_SET
%66 = call i32 @fseek(i32* %64, i32 0, i32 %65)
%67 = load i32*, i32** %1
%68 = call i32 @fgetc(i32* %67)
store i32 %68, i32* %2
%69 = load i32, i32* %2
%70 = icmp eq i32 %69, 99
%71 = zext i1 %70 to i32
%72 = call i32 @assert(i32 %71)
%73 = load i32*, i32** %1
%74 = load i32, i32* @SEEK_END
%75 = call i32 @fseek(i32* %73, i32 0, i32 %74)
%76 = load i32*, i32** %1
%77 = call i32 @fgetc(i32* %76)
store i32 %77, i32* %2
%78 = load i32, i32* %2
%79 = load i32, i32* @EOF
%80 = icmp eq i32 %78, %79
%81 = zext i1 %80 to i32
%82 = call i32 @assert(i32 %81)
%83 = load i32*, i32** %1
%84 = call i32 @feof(i32* %83)
store i32 %84, i32* %2
%85 = load i32, i32* %2
%86 = call i32 @assert(i32 %85)
%87 = load i32*, i32** %1
%88 = call i32 @ungetc(i8 signext 101, i32* %87)
%89 = load i32*, i32** %1
%90 = call i32 @feof(i32* %89)
store i32 %90, i32* %2
%91 = load i32, i32* %2
%92 = icmp ne i32 %91, 0
%93 = xor i1 %92, true
%94 = zext i1 %93 to i32
%95 = call i32 @assert(i32 %94)
%96 = load i32*, i32** %1
%97 = call i32 @fclose(i32* %96)
%98 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.3 = [8 x i8] c"success\00"
define void @test() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = ptrtoint i32* %3 to i64
%5 = trunc i64 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i32 @rewind(i32* %3)
%8 = load i32, i32* @EOF
%9 = trunc i32 %8 to i8
%10 = tail call i32 @ungetc(i8 signext %9, i32* %3)
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i32 @ungetc(i8 signext 97, i32* %3)
%16 = icmp eq i32 %15, 97
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = tail call i32 @rewind(i32* %3)
%20 = tail call i32 @ungetc(i8 signext 98, i32* %3)
%21 = tail call i32 @ungetc(i8 signext 97, i32* %3)
%22 = tail call i32 @fgetc(i32* %3)
%23 = icmp eq i32 %22, 97
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = call i32 @fread(i8* %2, i32 1, i32 256, i32* %3)
%27 = icmp eq i32 %26, 3
%28 = zext i1 %27 to i32
%29 = tail call i32 @assert(i32 %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 3
store i8 0, i8* %30
%31 = bitcast [256 x i8]* %1 to i32*
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 6579042
%34 = zext i1 %33 to i32
%35 = tail call i32 @assert(i32 %34)
%36 = tail call i32 @ungetc(i8 signext 97, i32* %3)
%37 = tail call i32 @rewind(i32* %3)
%38 = tail call i32 @fgetc(i32* %3)
%39 = icmp eq i32 %38, 99
%40 = zext i1 %39 to i32
%41 = tail call i32 @assert(i32 %40)
%42 = tail call i32 @ungetc(i8 signext 97, i32* %3)
%43 = load i32, i32* @SEEK_SET
%44 = tail call i32 @fseek(i32* %3, i32 0, i32 %43)
%45 = tail call i32 @fgetc(i32* %3)
%46 = icmp eq i32 %45, 99
%47 = zext i1 %46 to i32
%48 = tail call i32 @assert(i32 %47)
%49 = load i32, i32* @SEEK_END
%50 = tail call i32 @fseek(i32* %3, i32 0, i32 %49)
%51 = tail call i32 @fgetc(i32* %3)
%52 = load i32, i32* @EOF
%53 = icmp eq i32 %51, %52
%54 = zext i1 %53 to i32
%55 = tail call i32 @assert(i32 %54)
%56 = tail call i32 @feof(i32* %3)
%57 = tail call i32 @assert(i32 %56)
%58 = tail call i32 @ungetc(i8 signext 101, i32* %3)
%59 = tail call i32 @feof(i32* %3)
%60 = icmp eq i32 %59, 0
%61 = zext i1 %60 to i32
%62 = tail call i32 @assert(i32 %61)
%63 = tail call i32 @fclose(i32* %3)
%64 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
ret void
}