#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*/ pidstring;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int, int);
int snprintf(char *, int, char *, int);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *);
scalar_t__ write(int, char *, int);
int writepidfile(const char *fname, int pid) {
  char pidstring[16];
  int pidstringlen;
  int pidfile;
  if (!fname || (strlen(fname) == 0))
    return -1;
  if ((pidfile = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0) {
    syslog(LOG_ERR, "Unable to open pidfile for writing %s: %m", fname);
    return -1;
  }
  pidstringlen = snprintf(pidstring, sizeof(pidstring), "%d\n", pid);
  if (pidstringlen <= 0) {
    syslog(LOG_ERR, "Unable to write to pidfile %s: snprintf(): FAILED", fname);
    close(pidfile);
    return -1;
  } else {
    if (write(pidfile, pidstring, pidstringlen) < 0)
      syslog(LOG_ERR, "Unable to write to pidfile %s: %m", fname);
  }
  close(pidfile);
  return 0;
}


x86 assembly (clang, -O0):
<writepidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118d <writepidfile+0x2d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1199 <writepidfile+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <writepidfile+0x12b>
mov    -0x10(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    11f3 <writepidfile+0x93>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1e(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <writepidfile+0x12b>
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xe24(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     1245 <writepidfile+0xe5>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe02(%rip),%rsi
call   1070 <syslog@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <writepidfile+0x12b>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1277 <writepidfile+0x117>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdee(%rip),%rsi
call   1070 <syslog@plt>
jmp    127c <writepidfile+0x11c>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepidfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1231 <writepidfile+0xe1>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1231 <writepidfile+0xe1>
mov    %esi,%r15d
mov    0x2e4a(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11f7 <writepidfile+0xa7>
mov    %eax,%r14d
lea    0xe7d(%rip),%rdx
mov    %rsp,%rdi
mov    $0x10,%esi
mov    %r15d,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
jle    1211 <writepidfile+0xc1>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11eb <writepidfile+0x9b>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdx
call   1060 <syslog@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %ebp,%ebp
jmp    1231 <writepidfile+0xe1>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    %rbx,%rdx
call   1060 <syslog@plt>
jmp    1231 <writepidfile+0xe1>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    %rbx,%rdx
call   1060 <syslog@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@LOG_ERR = global i32 0
@.str = [42 x i8] c"Unable to open pidfile for writing %s: %m\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [50 x i8] c"Unable to write to pidfile %s: snprintf(): FAILED\00"
@.str.3 = [34 x i8] c"Unable to write to pidfile %s: %m\00"
define i32 @writepidfile(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [16 x i8]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11, %2
store i32 -1, i32* %3
br label %55
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_EXCL
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %17, i32 %22, i32 438)
store i32 %23, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i32, i32* @LOG_ERR
%27 = load i8*, i8** %4
%28 = call i32 @syslog(i32 %26, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %55
29: ; preds = %16
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* %5
%32 = call i32 @snprintf(i8* %30, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = load i32, i32* @LOG_ERR
%37 = load i8*, i8** %4
%38 = call i32 @syslog(i32 %36, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %3
br label %55
41: ; preds = %29
%42 = load i32, i32* %8
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%44 = load i32, i32* %7
%45 = call i64 @write(i32 %42, i8* %43, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = load i32, i32* @LOG_ERR
%49 = load i8*, i8** %4
%50 = call i32 @syslog(i32 %48, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %47, %41
br label %52
52: ; preds = %51
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
store i32 0, i32* %3
br label %55
55: ; preds = %52, %35, %25, %15
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@LOG_ERR = global i32 0
@.str = [42 x i8] c"Unable to open pidfile for writing %s: %m\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [50 x i8] c"Unable to write to pidfile %s: snprintf(): FAILED\00"
@.str.3 = [34 x i8] c"Unable to write to pidfile %s: %m\00"
define i32 @writepidfile(i8* %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
br i1 %5, label %35, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %6
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %0, i32 %14, i32 438)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = load i32, i32* @LOG_ERR
%19 = tail call i32 @syslog(i32 %18, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %35
20: ; preds = %9
%21 = call i32 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @LOG_ERR
%25 = call i32 @syslog(i32 %24, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%26 = call i32 @close(i32 %15)
br label %35
27: ; preds = %20
%28 = call i64 @write(i32 %15, i8* %4, i32 %21)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @LOG_ERR
%32 = call i32 @syslog(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %33
33: ; preds = %27, %30
%34 = call i32 @close(i32 %15)
br label %35
35: ; preds = %2, %6, %33, %23, %17
%36 = phi i32 [ -1, %17 ], [ -1, %23 ], [ 0, %33 ], [ -1, %6 ], [ -1, %2 ]
ret i32 %36
}