#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 /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
char *read_file(char *, size_t *);
int /*<<< orphan*/ sleep(int);
void fopen_write_file_test() {
  int i = 0, j = 0;
  FILE *fp1 = fopen("test.txt", "w");
  fprintf(fp1, "pid is %d\n", getpid());
  size_t len;
  char *buf;
  buf = read_file("test.txt", &len);
  printf("buf is %s(%s)\n", buf ? buf : "null", buf ? "not null" : "null");
  printf("len = %d\n", len);
  for (j = 0; j < 1000; j++) {
    fprintf(fp1, "fp1 wirte %d.. \n", i);
    i++;
  }
  fprintf(fp1, "fp1 write %d.. \n", i);
  i++;
  sleep(10);
  FILE *fp2 = fopen("test.txt", "w");
  fprintf(fp2, "fp2 write %d.. \n", i);
  i++;
  fprintf(fp1, "fp1 write %d.. \n", i);
  i++;
  fprintf(fp2, "fp2 write %d.. \n", i);
  i++;
  fclose(fp2);
  fclose(fp1);
}


x86 assembly (clang, -O0):
<fopen_write_file_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    0xe73(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe52(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe3b(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <read_file@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ea <fopen_write_file_test+0x7a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11fa <fopen_write_file_test+0x8a>
lea    0xe38(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11fa <fopen_write_file_test+0x8a>
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    0xe20(%rip),%rdx
lea    0xe15(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3e8,-0x8(%rbp)
jge    1276 <fopen_write_file_test+0x106>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xdde(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    123f <fopen_write_file_test+0xcf>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    $0xa,%edi
call   1090 <sleep@plt>
lea    0xd5d(%rip),%rdi
lea    0xd5f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xd99(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xd6c(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xd61(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen_write_file_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe82(%rip),%rbx
lea    0xe84(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <read_file@plt>
test   %rax,%rax
lea    0xe6a(%rip),%rcx
cmove  %rcx,%rax
lea    0xe5b(%rip),%rdx
cmove  %rcx,%rdx
lea    0xe41(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rsi
lea    0xe44(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe40(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <fprintf@plt>
add    $0x1,%ebp
cmp    $0x3e8,%ebp
jne    1200 <fopen_write_file_test+0x90>
lea    0xe2a(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x3e8,%edx
call   1060 <fprintf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
lea    0xdc0(%rip),%rdi
lea    0xdc2(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0xe04(%rip),%rbp
mov    %rax,%rdi
mov    %rbp,%rsi
mov    $0x3e9,%edx
call   1060 <fprintf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x3ea,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x3eb,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"pid is %d\0A\00"
@.str.3 = [15 x i8] c"buf is %s(%s)\0A\00"
@.str.4 = [5 x i8] c"null\00"
@.str.5 = [9 x i8] c"not null\00"
@.str.6 = [10 x i8] c"len = %d\0A\00"
@.str.7 = [17 x i8] c"fp1 wirte %d.. \0A\00"
@.str.8 = [17 x i8] c"fp1 write %d.. \0A\00"
@.str.9 = [17 x i8] c"fp2 write %d.. \0A\00"
define void @fopen_write_file_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
%7 = 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* %7, i32** %3
%8 = load i32*, i32** %3
%9 = call i32 (...) @getpid()
%10 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = call i8* @read_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i64* %4)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = load i8*, i8** %5
br label %17
16: ; preds = %0
br label %17
17: ; preds = %16, %14
%18 = phi i8* [ %15, %14 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %16 ]
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i64
%22 = select i1 %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %18, i8* %22)
%24 = load i64, i64* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i64 %24)
store i32 0, i32* %2
br label %26
26: ; preds = %35, %17
%27 = load i32, i32* %2
%28 = icmp slt i32 %27, 1000
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i32*, i32** %3
%31 = load i32, i32* %1
%32 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %1
%34 = add nsw i32 %33, 1
store i32 %34, i32* %1
br label %35
35: ; preds = %29
%36 = load i32, i32* %2
%37 = add nsw i32 %36, 1
store i32 %37, i32* %2
br label %26
38: ; preds = %26
%39 = load i32*, i32** %3
%40 = load i32, i32* %1
%41 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %1
%43 = add nsw i32 %42, 1
store i32 %43, i32* %1
%44 = call i32 @sleep(i32 10)
%45 = 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* %45, i32** %6
%46 = load i32*, i32** %6
%47 = load i32, i32* %1
%48 = call i32 @fprintf(i32* %46, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %1
%50 = add nsw i32 %49, 1
store i32 %50, i32* %1
%51 = load i32*, i32** %3
%52 = load i32, i32* %1
%53 = call i32 @fprintf(i32* %51, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %1
%55 = add nsw i32 %54, 1
store i32 %55, i32* %1
%56 = load i32*, i32** %6
%57 = load i32, i32* %1
%58 = call i32 @fprintf(i32* %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %1
%60 = add nsw i32 %59, 1
store i32 %60, i32* %1
%61 = load i32*, i32** %6
%62 = call i32 @fclose(i32* %61)
%63 = load i32*, i32** %3
%64 = call i32 @fclose(i32* %63)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"pid is %d\0A\00"
@.str.3 = [15 x i8] c"buf is %s(%s)\0A\00"
@.str.4 = [5 x i8] c"null\00"
@.str.5 = [9 x i8] c"not null\00"
@.str.6 = [10 x i8] c"len = %d\0A\00"
@.str.7 = [17 x i8] c"fp1 wirte %d.. \0A\00"
@.str.8 = [17 x i8] c"fp1 write %d.. \0A\00"
@.str.9 = [17 x i8] c"fp2 write %d.. \0A\00"
define void @fopen_write_file_test() {
%1 = alloca i64
%2 = 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))
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%5 = bitcast i64* %1 to i8*
%6 = call i8* @read_file(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i64* %1)
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %6
%9 = select i1 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %8, i8* %9)
%11 = load i64, i64* %1
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i64 %11)
br label %13
13: ; preds = %0, %13
%14 = phi i32 [ 0, %0 ], [ %16, %13 ]
%15 = call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %14)
%16 = add nuw nsw i32 %14, 1
%17 = icmp eq i32 %16, 1000
br i1 %17, label %18, label %13
18: ; preds = %13
%19 = call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 1000)
%20 = call i32 @sleep(i32 10)
%21 = 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))
%22 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 1001)
%23 = call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0), i32 1002)
%24 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0), i32 1003)
%25 = call i32 @fclose(i32* %21)
%26 = call i32 @fclose(i32* %2)
ret void
}