#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 O_CREAT;
int O_NOFOLLOW;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ fatal(char *, char const *);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int open(char const *, int, int);
void write_pidfile(const char *filename, pid_t pid) {
  FILE *fp;
  int fd;
  fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0666);
  if (fd < 0)
    fp = NULL;
  else
    fp = fdopen(fd, "w");
  if (fp == NULL)
    fatal("unable to open pidfile '%s' for writing", filename);
  fprintf(fp, "%d\n", pid);
  if (fclose(fp))
    fatal("unable to close pidfile '%s'", filename);
}


x86 assembly (clang, -O0):
<write_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
mov    0x2e6b(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ab <write_pidfile+0x5b>
movq   $0x0,-0x18(%rbp)
jmp    11be <write_pidfile+0x6e>
mov    -0x1c(%rbp),%edi
lea    0xe4b(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <write_pidfile+0x89>
mov    -0x8(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe43(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     120f <write_pidfile+0xbf>
mov    -0x8(%rbp),%rsi
lea    0xe24(%rip),%rdi
call   1070 <fatal@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pidfile>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e9d(%rip),%rcx
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e7b(%rip),%rcx
or     (%rcx),%eax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e74(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11a4 <write_pidfile+0x54>
lea    0xe6b(%rip),%rsi
mov    %eax,%edi
call   1050 <fdopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b5 <write_pidfile+0x65>
lea    0xe57(%rip),%rdi
mov    %rbx,%rsi
call   1070 <fatal@plt>
xor    %ebp,%ebp
lea    0xe6e(%rip),%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
call   1040 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     11e7 <write_pidfile+0x97>
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <fatal@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [40 x i8] c"unable to open pidfile '%s' for writing\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [29 x i8] c"unable to close pidfile '%s'\00"
define void @write_pidfile(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @O_NOFOLLOW
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %7, i32 %14, i32 438)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32* null, i32** %5
br label %22
19: ; preds = %2
%20 = load i32, i32* %6
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %5
br label %22
22: ; preds = %19, %18
%23 = load i32*, i32** %5
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i8*, i8** %3
%27 = call i32 @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %22
%29 = load i32*, i32** %5
%30 = load i32, i32* %4
%31 = call i32 @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %5
%33 = call i64 @fclose(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i8*, i8** %3
%37 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %28
ret void
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
@.str = [2 x i8] c"w\00"
@.str.1 = [40 x i8] c"unable to open pidfile '%s' for writing\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [29 x i8] c"unable to close pidfile '%s'\00"
define void @write_pidfile(i8* %0, i32 %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @O_NOFOLLOW
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 438)
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = tail call i32* @fdopen(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2, %12
%16 = tail call i32 @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %15, %12
%18 = phi i32* [ null, %15 ], [ %13, %12 ]
%19 = tail call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%20 = tail call i64 @fclose(i32* %18)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = tail call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %24
24: ; preds = %22, %17
ret void
}