#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 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);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <main+0x52>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <main+0xde>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
mov    $0x61,%edi
call   1070 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe86(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1203 <main+0x93>
mov    %rax,%rbx
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    $0x61,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
lea    0xe51(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
lea    0xe39(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
lea    0xe2c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1214 <main+0xa4>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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"
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 %34
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 @fclose(i32* %32)
store i32 0, i32* %3
br label %34
34: ; preds = %14, %12
%35 = load i32, i32* %3
ret i32 %35
}

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 = [22 x i8] c"open file a.c success\00"
@str.7 = [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.7, i64 0, i64 0))
br label %18
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 @fclose(i32* %3)
br label %18
18: ; preds = %7, %5
%19 = phi i32 [ -1, %5 ], [ 0, %7 ]
ret i32 %19
}