#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 R_OK;
int W_OK;
scalar_t__ access(char const *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpid();
int /*<<< orphan*/ stderr;
int make_pidfile(const char *path) {
  FILE *fp;
  if (access(path, R_OK | W_OK) == 0) {
    fprintf(stderr, "already exist process id file.\n");
    return 1;
  }
  fp = fopen(path, "w+");
  if (fp == NULL) {
    fprintf(stderr, "failed in open process id file.\n");
    return 1;
  }
  fprintf(fp, "%d\n", getpid());
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<make_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
or     (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11a5 <make_pidfile+0x55>
mov    0x2e50(%rip),%rax
movslq (%rax),%rdi
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121b <make_pidfile+0xcb>
mov    -0x10(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e8 <make_pidfile+0x98>
mov    0x2e0d(%rip),%rax
movslq (%rax),%rdi
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121b <make_pidfile+0xcb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_pidfile>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e63(%rip),%rax
mov    0x2e74(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <access@plt>
test   %rax,%rax
je     11bf <make_pidfile+0x5f>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d7 <make_pidfile+0x77>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9b(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11fc <make_pidfile+0x9c>
mov    0x2e12(%rip),%rax
movslq (%rax),%rcx
lea    0xe30(%rip),%rdi
mov    $0x1f,%esi
jmp    11ed <make_pidfile+0x8d>
mov    0x2dfa(%rip),%rax
movslq (%rax),%rcx
lea    0xe3b(%rip),%rdi
mov    $0x20,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@R_OK = global i32 0
@W_OK = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"already exist process id file.\0A\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [33 x i8] c"failed in open process id file.\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @make_pidfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @R_OK
%7 = load i32, i32* @W_OK
%8 = or i32 %6, %7
%9 = call i64 @access(i8* %5, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i32*
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %32
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %4
%19 = load i32*, i32** %4
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = sext i32 %22 to i64
%24 = inttoptr i64 %23 to i32*
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %32
26: ; preds = %16
%27 = load i32*, i32** %4
%28 = call i32 (...) @getpid()
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %2
br label %32
32: ; preds = %26, %21, %11
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@R_OK = global i32 0
@W_OK = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"already exist process id file.\0A\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [33 x i8] c"failed in open process id file.\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @make_pidfile(i8* nocapture %0) {
%2 = load i32, i32* @R_OK
%3 = load i32, i32* @W_OK
%4 = or i32 %3, %2
%5 = tail call i64 @access(i8* %0, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i32*
%11 = tail call i64 @fwrite(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 31, i64 1, i32* %10)
br label %24
12: ; preds = %1
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i32*
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 32, i64 1, i32* %18)
br label %24
20: ; preds = %12
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = tail call i32 @fclose(i32* %13)
br label %24
24: ; preds = %20, %15, %7
%25 = phi i32 [ 1, %7 ], [ 1, %15 ], [ 0, %20 ]
ret i32 %25
}