#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 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 *);
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) {
    {};
    return -1;
  }
  pidstringlen = snprintf(pidstring, sizeof(pidstring), "%d\n", pid);
  if (pidstringlen <= 0) {
    {};
    close(pidfile);
    return -1;
  } else {
    if (write(pidfile, pidstring, pidstringlen) < 0) {
    };
  }
  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     117d <writepidfile+0x2d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1189 <writepidfile+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <writepidfile+0xe5>
mov    -0x10(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    11ca <writepidfile+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <writepidfile+0xe5>
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xe23(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     1203 <writepidfile+0xb3>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <writepidfile+0xe5>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1221 <writepidfile+0xd1>
jmp    1221 <writepidfile+0xd1>
jmp    1226 <writepidfile+0xd6>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     11bd <writepidfile+0x7d>
cmpb   $0x0,(%rdi)
je     11bd <writepidfile+0x7d>
mov    %esi,%ebp
mov    0x2e68(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11bd <writepidfile+0x7d>
mov    %eax,%r14d
lea    0xe74(%rip),%rdx
mov    %rsp,%rdi
mov    $0x10,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
jle    11b0 <writepidfile+0x70>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
jmp    11b5 <writepidfile+0x75>
mov    $0xffffffff,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [4 x i8] c"%d\0A\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 %46
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 %26
25: ; preds = %16
store i32 -1, i32* %3
br label %46
26: ; preds = %16
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%28 = load i32, i32* %5
%29 = call i32 @snprintf(i8* %27, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %3
br label %46
35: ; preds = %26
%36 = load i32, i32* %8
%37 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%38 = load i32, i32* %7
%39 = call i64 @write(i32 %36, i8* %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
br label %42
42: ; preds = %41, %35
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = call i32 @close(i32 %44)
store i32 0, i32* %3
br label %46
46: ; preds = %43, %32, %25, %15
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [4 x i8] c"%d\0A\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 %25, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %25, 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 %25, label %17
17: ; preds = %9
%18 = call i32 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%19 = icmp slt i32 %18, 1
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i64 @write(i32 %15, i8* %4, i32 %18)
br label %22
22: ; preds = %17, %20
%23 = phi i32 [ 0, %20 ], [ -1, %17 ]
%24 = call i32 @close(i32 %15)
br label %25
25: ; preds = %22, %9, %2, %6
%26 = phi i32 [ -1, %6 ], [ -1, %2 ], [ -1, %9 ], [ %23, %22 ]
ret i32 %26
}