#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 */
struct stat {
  int dummy;
};
typedef int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int write_pidfile(char *pid_file, pid_t pid) {
  struct stat buf;
  int stat_res = stat(pid_file, &buf);
  if (stat_res == 0) {
    syslog(LOG_ERR, "pid file already exists! (%s)", pid_file);
    return 1;
  }
  FILE *file = fopen(pid_file, "w");
  if (!file) {
    syslog(LOG_ERR, "Failed to open pid file! (%s)", pid_file);
    return 1;
  }
  fprintf(file, "%d", pid);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<write_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    119e <write_pidfile+0x4e>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe73(%rip),%rsi
call   1060 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    1205 <write_pidfile+0xb5>
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e2 <write_pidfile+0x92>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1060 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    1205 <write_pidfile+0xb5>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe4e(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pidfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
je     11a1 <write_pidfile+0x51>
lea    0xeab(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b3 <write_pidfile+0x63>
mov    %rax,%rbp
lea    0xeb4(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   1040 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11d0 <write_pidfile+0x80>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
jmp    11c3 <write_pidfile+0x73>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdx
call   1060 <syslog@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@LOG_ERR = global i32 0
@.str = [30 x i8] c"pid file already exists! (%s)\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [30 x i8] c"Failed to open pid file! (%s)\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @write_pidfile(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i32 @stat(i8* %9, %struct.stat* %6)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @LOG_ERR
%15 = load i8*, i8** %4
%16 = call i32 @syslog(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %32
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = load i32, i32* @LOG_ERR
%24 = load i8*, i8** %4
%25 = call i32 @syslog(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 1, i32* %3
br label %32
26: ; preds = %17
%27 = load i32*, i32** %8
%28 = load i32, i32* %5
%29 = call i32 @fprintf(i32* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %8
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %3
br label %32
32: ; preds = %26, %22, %13
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@LOG_ERR = global i32 0
@.str = [30 x i8] c"pid file already exists! (%s)\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [30 x i8] c"Failed to open pid file! (%s)\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @write_pidfile(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @LOG_ERR
%9 = tail call i32 @syslog(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %19
10: ; preds = %2
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @LOG_ERR
%15 = tail call i32 @syslog(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %19
16: ; preds = %10
%17 = tail call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%18 = tail call i32 @fclose(i32* %11)
br label %19
19: ; preds = %13, %16, %7
%20 = phi i32 [ 1, %7 ], [ 0, %16 ], [ 1, %13 ]
ret i32 %20
}