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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main() {
  FILE *fp;
  char buf[] = "hello\n";
  char readbuf[128] = {0};
  fp = fopen("./a.c", "w+");
  if (fp == NULL) {
    printf("open file a.c failure\n");
    return -1;
  }
  fwrite(buf, sizeof(char), sizeof(buf), fp);
  rewind(fp);
  fread(readbuf, sizeof(char), sizeof(readbuf), fp);
  printf("readbuf is %s", readbuf);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe78(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe72(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe6a(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
lea    0xe4e(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ee <main+0x7e>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <main+0xdd>
lea    -0x17(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80,%edx
call   1030 <fread@plt>
lea    -0xa0(%rbp),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
movl   $0xa6f6c,0xb(%rsp)
movl   $0x6c6c6568,0x8(%rsp)
6c
xorps  %xmm0,%xmm0
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xe47(%rip),%rdi
lea    0xe46(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     122a <main+0xba>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x7,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <rewind@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
lea    0xdfc(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    123b <main+0xcb>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [7 x i8] c"hello\0A\00"
@.str = [6 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 = [14 x i8] c"readbuf is %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [7 x i8]
%4 = alloca [128 x i8]
store i32 0, i32* %1
%5 = bitcast [7 x i8]* %3 to i8*
%6 = bitcast [128 x i8]* %4 to i8*
%7 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %25
12: ; preds = %0
%13 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i32 @fwrite(i8* %13, i32 1, i32 7, i32* %14)
%16 = load i32*, i32** %2
%17 = call i32 @rewind(i32* %16)
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %2
%20 = call i32 @fread(i8* %18, i32 1, i32 128, i32* %19)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
store i32 0, i32* %1
br label %25
25: ; preds = %12, %10
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@__const.main.buf = [7 x i8] c"hello\0A\00"
@.str = [6 x i8] c"./a.c\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.3 = [14 x i8] c"readbuf is %s\00"
@str = [22 x i8] c"open file a.c failure\00"
define i32 @main() {
%1 = alloca [7 x i8]
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %15
9: ; preds = %0
%10 = call i32 @fwrite(i8* %3, i32 1, i32 7, i32* %5)
%11 = tail call i32 @rewind(i32* %5)
%12 = call i32 @fread(i8* %4, i32 1, i32 128, i32* %5)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%14 = call i32 @fclose(i32* %5)
br label %15
15: ; preds = %9, %7
%16 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %16
}