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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(char *, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *);
int getpid();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int save_pid(const char *path) {
  pid_t pid = getpid();
  FILE *fp = fopen(path, "w+");
  if (!fp) {
    printf("open file fail\n");
    return 0;
  }
  char str[10];
  sprintf(str, "%d", pid);
  fwrite(str, strlen(str), 1, fp);
  fclose(fp);
  fp = NULL;
  return 1;
}


x86 assembly (clang, -O0):
<save_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bd <save_pid+0x4d>
lea    0xe57(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    120d <save_pid+0x9d>
lea    -0x2a(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe48(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x2a(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x2a(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e0 <save_pid+0x70>
mov    %rax,%rbx
lea    0xe5c(%rip),%rsi
lea    0xe(%rsp),%r15
mov    %r15,%rdi
mov    %r14d,%edx
call   1080 <sprintf@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    $0x1,%ebp
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11ec <save_pid+0x7c>
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [16 x i8] c"open file fail\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @save_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [10 x i8]
store i8* %0, i8** %3
%7 = call i32 (...) @getpid()
store i32 %7, i32* %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %25
14: ; preds = %1
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
%21 = load i32*, i32** %5
%22 = call i32 @fwrite(i8* %18, i32 %20, i32 1, i32* %21)
%23 = load i32*, i32** %5
%24 = call i32 @fclose(i32* %23)
store i32* null, i32** %5
store i32 1, i32* %2
br label %25
25: ; preds = %14, %12
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.2 = [3 x i8] c"%d\00"
@str = [15 x i8] c"open file fail\00"
define i32 @save_pid(i8* nocapture %0) {
%2 = alloca [10 x i8]
%3 = tail call i32 (...) @getpid()
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %14
8: ; preds = %1
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%10 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%11 = call i32 @strlen(i8* %9)
%12 = call i32 @fwrite(i8* %9, i32 %11, i32 1, i32* %4)
%13 = call i32 @fclose(i32* %4)
br label %14
14: ; preds = %8, %6
%15 = phi i32 [ 1, %8 ], [ 0, %6 ]
ret i32 %15
}