#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*/ abort_report(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
int pidfile(const char *path, const char *mode) {
  FILE *fp;
  int pid;
  fp = fopen(path, mode);
  pid = getpid();
  if (*mode == 'w') {
    (fp)       ? fprintf(fp, "%d", pid)
    : (1 == 1) ? abort_report("daemon: Cannot open pidfile for writing.", "")
               : abort_report("daemon: Cannot open pidfile for writing.");
  } else if (*mode == 'r') {
    (fp)       ? fscanf(fp, "%d", &pid)
    : (1 == 1) ? abort_report("daemon: No pidfile. Is the daemon running?", "")
               : abort_report("daemon: No pidfile. Is the daemon running?");
  } else
    (1 == 1) ? abort_report("daemon: Invalid mode supplied to pidfile().", "")
             : abort_report("daemon: Invalid mode supplied to pidfile().");
  fclose(fp);
  return pid;
}


x86 assembly (clang, -O0):
<pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    11d8 <pidfile+0x78>
cmpq   $0x0,-0x18(%rbp)
je     11be <pidfile+0x5e>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe4c(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11d3 <pidfile+0x73>
lea    0xe3e(%rip),%rdi
lea    0xeb6(%rip),%rsi
mov    $0x0,%al
call   1050 <abort_report@plt>
jmp    1240 <pidfile+0xe0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    1226 <pidfile+0xc6>
cmpq   $0x0,-0x18(%rbp)
je     120c <pidfile+0xac>
mov    -0x18(%rbp),%rdi
lea    0xe02(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
jmp    1221 <pidfile+0xc1>
lea    0xe19(%rip),%rdi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1050 <abort_report@plt>
jmp    123b <pidfile+0xdb>
lea    0xe2a(%rip),%rdi
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1050 <abort_report@plt>
jmp    1240 <pidfile+0xe0>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfile>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1080 <fopen@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%cl
cmp    $0x72,%cl
je     119e <pidfile+0x3e>
cmp    $0x77,%cl
jne    11b9 <pidfile+0x59>
test   %r14,%r14
je     11c2 <pidfile+0x62>
lea    0xe6e(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1070 <fprintf@plt>
jmp    11e0 <pidfile+0x80>
test   %r14,%r14
je     11cb <pidfile+0x6b>
lea    0xe56(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %r14,%rdi
call   1060 <fscanf@plt>
jmp    11e0 <pidfile+0x80>
lea    0xe97(%rip),%rdi
jmp    11d2 <pidfile+0x72>
lea    0xe3a(%rip),%rdi
jmp    11d2 <pidfile+0x72>
lea    0xe5a(%rip),%rdi
lea    0xea9(%rip),%rsi
xor    %eax,%eax
call   1050 <abort_report@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [41 x i8] c"daemon: Cannot open pidfile for writing.\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [43 x i8] c"daemon: No pidfile. Is the daemon running?\00"
@.str.4 = [44 x i8] c"daemon: Invalid mode supplied to pidfile().\00"
define i32 @pidfile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %7, i8* %8)
store i32* %9, i32** %5
%10 = call i32 (...) @getpid()
store i32 %10, i32* %6
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 119
br i1 %14, label %15, label %26
15: ; preds = %2
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32*, i32** %5
%20 = load i32, i32* %6
%21 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %24
22: ; preds = %15
%23 = call i32 (i8*, ...) @abort_report(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = phi i32 [ %21, %18 ], [ %23, %22 ]
br label %44
26: ; preds = %2
%27 = load i8*, i8** %4
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 114
br i1 %30, label %31, label %41
31: ; preds = %26
%32 = load i32*, i32** %5
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32*, i32** %5
%36 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %39
37: ; preds = %31
%38 = call i32 (i8*, ...) @abort_report(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = phi i32 [ %36, %34 ], [ %38, %37 ]
br label %43
41: ; preds = %26
%42 = call i32 (i8*, ...) @abort_report(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %39
br label %44
44: ; preds = %43, %24
%45 = load i32*, i32** %5
%46 = call i32 @fclose(i32* %45)
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [41 x i8] c"daemon: Cannot open pidfile for writing.\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [43 x i8] c"daemon: No pidfile. Is the daemon running?\00"
@.str.4 = [44 x i8] c"daemon: Invalid mode supplied to pidfile().\00"
define i32 @pidfile(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* %0, i8* %1)
%6 = tail call i32 (...) @getpid()
store i32 %6, i32* %3
%7 = load i8, i8* %1
switch i8 %7, label %20 [
i8 119, label %8
i8 114, label %14
]
8: ; preds = %2
%9 = icmp eq i32* %5, null
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %22
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @abort_report(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %22
14: ; preds = %2
%15 = icmp eq i32* %5, null
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
br label %22
18: ; preds = %14
%19 = tail call i32 (i8*, ...) @abort_report(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %22
20: ; preds = %2
%21 = tail call i32 (i8*, ...) @abort_report(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %18, %16, %10, %12
%23 = call i32 @fclose(i32* %5)
%24 = load i32, i32* %3
ret i32 %24
}