#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 *);
size_t fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int rand();
int /*<<< orphan*/ stderr;
int writeFile(char *filePath) {
  FILE *fp = fopen(filePath, "w");
  if (NULL == fp) {
    perror("open file fail");
    return -1;
  }
  size_t num = fprintf(fp, "write something:%d", rand());
  if (num < 0) {
    fprintf(stderr, "write data to file fail");
    return -1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<writeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1194 <writeFile+0x44>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <writeFile+0xa7>
mov    -0x18(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1070 <rand@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11e7 <writeFile+0x97>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <writeFile+0xa7>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFile>:
push   %rbp
push   %rbx
push   %rax
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1193 <writeFile+0x43>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <rand@plt>
lea    0xe9a(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    118a <writeFile+0x3a>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [15 x i8] c"open file fail\00"
@.str.2 = [19 x i8] c"write something:%d\00"
@stderr = global i32 0
@.str.3 = [24 x i8] c"write data to file fail\00"
define i32 @writeFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* null, %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %25
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = ptrtoint i32* %13 to i32
%15 = call i32 (...) @rand()
%16 = call i64 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %15)
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = icmp ult i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = call i64 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %25
22: ; preds = %12
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
store i32 0, i32* %2
br label %25
25: ; preds = %22, %19, %10
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [15 x i8] c"open file fail\00"
@.str.2 = [19 x i8] c"write something:%d\00"
@stderr = global i32 0
define i32 @writeFile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %12
6: ; preds = %1
%7 = ptrtoint i32* %2 to i64
%8 = trunc i64 %7 to i32
%9 = tail call i32 (...) @rand()
%10 = tail call i64 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = tail call i32 @fclose(i32* %2)
br label %12
12: ; preds = %6, %4
%13 = phi i32 [ -1, %4 ], [ 0, %6 ]
ret i32 %13
}