#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 */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int feof(int /*<<< orphan*/ *);
int ferror(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  FILE *fp;
  int ret;
  int ret2;
  fp = fopen("a.c", "w+");
  if (fp == NULL) {
    printf("open file a.c failure\n");
    return -1;
  }
  printf("open file a.c success\n");
  fputc('a', fp);
  ret = fgetc(fp);
  printf("ret = %c\n", ret);
  rewind(fp);
  ret2 = fgetc(fp);
  printf("ret2 = %c\n", ret2);
  ret2 = fgetc(fp);
  printf("ret2 = %d\n", ret2);
  printf("feof is %d, ferror is %d\n", feof(fp), ferror(fp));
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
lea    0xe60(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <main+0x52>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <main+0x106>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rsi
mov    $0x61,%edi
call   1080 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe65(%rip),%rdi
lea    0xe62(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     124d <main+0xbd>
mov    %rax,%rbx
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   1090 <fputc@plt>
mov    %rbx,%rdi
call   1080 <fgetc@plt>
lea    0xe2c(%rip),%rdi
xor    %r14d,%r14d
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %rbx,%rdi
call   1080 <fgetc@plt>
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <fgetc@plt>
lea    0xe06(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <ferror@plt>
lea    0xdef(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    125f <main+0xcf>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"a.c\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [23 x i8] c"open file a.c failure\0A\00"
@.str.3 = [23 x i8] c"open file a.c success\0A\00"
@.str.4 = [10 x i8] c"ret = %c\0A\00"
@.str.5 = [11 x i8] c"ret2 = %c\0A\00"
@.str.6 = [11 x i8] c"ret2 = %d\0A\00"
@.str.7 = [26 x i8] c"feof is %d, ferror is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32*, i32** %6
%17 = call i32 @fputc(i8 signext 97, i32* %16)
%18 = load i32*, i32** %6
%19 = call i32 @fgetc(i32* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = load i32*, i32** %6
%23 = call i32 @rewind(i32* %22)
%24 = load i32*, i32** %6
%25 = call i32 @fgetc(i32* %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %6
%29 = call i32 @fgetc(i32* %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %6
%33 = call i32 @feof(i32* %32)
%34 = load i32*, i32** %6
%35 = call i32 @ferror(i32* %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %33, i32 %35)
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
store i32 0, i32* %3
br label %39
39: ; preds = %14, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [4 x i8] c"a.c\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.4 = [10 x i8] c"ret = %c\0A\00"
@.str.5 = [11 x i8] c"ret2 = %c\0A\00"
@.str.6 = [11 x i8] c"ret2 = %d\0A\00"
@.str.7 = [26 x i8] c"feof is %d, ferror is %d\0A\00"
@str = [22 x i8] c"open file a.c success\00"
@str.8 = [22 x i8] c"open file a.c failure\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.8, i64 0, i64 0))
br label %21
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @fputc(i8 signext 97, i32* %3)
%10 = tail call i32 @fgetc(i32* %3)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %10)
%12 = tail call i32 @rewind(i32* %3)
%13 = tail call i32 @fgetc(i32* %3)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %13)
%15 = tail call i32 @fgetc(i32* %3)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %15)
%17 = tail call i32 @feof(i32* %3)
%18 = tail call i32 @ferror(i32* %3)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %17, i32 %18)
%20 = tail call i32 @fclose(i32* %3)
br label %21
21: ; preds = %7, %5
%22 = phi i32 [ -1, %5 ], [ 0, %7 ]
ret i32 %22
}