#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *fp;
  char buf[] = "hello\n";
  char readbuf[128] = {0};
  fp = fopen(argv[1], "w+");
  if (fp == NULL) {
    printf("open file %s failure\n", argv[1]);
    return -1;
  }
  printf("open file a.c success\n");
  fputs(buf, fp);
  fseek(fp, 0, SEEK_SET);
  fgets(readbuf, 128, fp);
  printf("readbuf: %s\n", readbuf);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe71(%rip),%eax
mov    %eax,-0x1f(%rbp)
mov    0xe6b(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe63(%rip),%al
mov    %al,-0x19(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fe <main+0x8e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <main+0xf7>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1f(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1070 <fgets@plt>
lea    -0xa0(%rbp),%rsi
lea    0xde7(%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    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%rbp
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)
mov    0x8(%rsi),%rdi
lea    0xe40(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1232 <main+0xc2>
mov    %rax,%rbx
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
lea    0xe02(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1249 <main+0xd9>
mov    0x8(%rbp),%rsi
lea    0xdcd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@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 = [3 x i8] c"w+\00"
@.str.1 = [22 x i8] c"open file %s failure\0A\00"
@.str.2 = [23 x i8] c"open file a.c success\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"readbuf: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [7 x i8]
%8 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [7 x i8]* %7 to i8*
%10 = bitcast [128 x i8]* %8 to i8*
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %37
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds [7 x i8], [7 x i8]* %7, i64 0, i64 0
%25 = load i32*, i32** %6
%26 = call i32 @fputs(i8* %24, i32* %25)
%27 = load i32*, i32** %6
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%31 = load i32*, i32** %6
%32 = call i32 @fgets(i8* %30, i32 128, i32* %31)
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
store i32 0, i32* %3
br label %37
37: ; preds = %22, %17
%38 = load i32, i32* %3
ret i32 %38
}

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