#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*/ LOG_ERR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fprintf(int /*<<< orphan*/ *, char *, long);
scalar_t__ getpid();
int /*<<< orphan*/ logitm(int /*<<< orphan*/, char *, char const *);
int write_pid_file(const char *path) {
  FILE *pid_file;
  if ((pid_file = fopen(path, "w")) == NULL) {
    logitm(LOG_ERR, "fopen(%s)", path);
    return (-1);
  }
  if (fprintf(pid_file, "%ld\n", (long)getpid()) == -1) {
    logitm(LOG_ERR, "fprintf(%s)", path);
    return (-1);
  }
  fclose(pid_file);
  return (0);
}


x86 assembly (clang, -O0):
<write_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119f <write_pid_file+0x4f>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe74(%rip),%rsi
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <write_pid_file+0xaf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rdx
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
jne    11ef <write_pid_file+0x9f>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe33(%rip),%rsi
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <write_pid_file+0xaf>
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):
<write_pid_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1195 <write_pid_file+0x45>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe91(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
je     11a7 <write_pid_file+0x57>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11c4 <write_pid_file+0x74>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
jmp    11b7 <write_pid_file+0x67>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
mov    %r14,%rdx
call   1050 <logitm@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@LOG_ERR = global i32 0
@.str.1 = [10 x i8] c"fopen(%s)\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [12 x i8] c"fprintf(%s)\00"
define i32 @write_pid_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @LOG_ERR
%10 = load i8*, i8** %3
%11 = call i32 @logitm(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %10)
store i32 -1, i32* %2
br label %24
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = call i64 (...) @getpid()
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @LOG_ERR
%19 = load i8*, i8** %3
%20 = call i32 @logitm(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %19)
store i32 -1, i32* %2
br label %24
21: ; preds = %12
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
store i32 0, i32* %2
br label %24
24: ; preds = %21, %17, %8
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@LOG_ERR = global i32 0
@.str.1 = [10 x i8] c"fopen(%s)\00"
@.str.2 = [5 x i8] c"%ld\0A\00"
@.str.3 = [12 x i8] c"fprintf(%s)\00"
define i32 @write_pid_file(i8* %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 %7
4: ; preds = %1
%5 = load i32, i32* @LOG_ERR
%6 = tail call i32 @logitm(i32 %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
7: ; preds = %1
%8 = tail call i64 (...) @getpid()
%9 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @LOG_ERR
%13 = tail call i32 @logitm(i32 %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %16
14: ; preds = %7
%15 = tail call i32 @fclose(i32* %2)
br label %16
16: ; preds = %14, %11, %4
%17 = phi i32 [ -1, %4 ], [ -1, %11 ], [ 0, %14 ]
ret i32 %17
}