#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_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, 0644)) < 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    122c <writepidfile+0xdc>
mov    -0x10(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    11c1 <writepidfile+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <writepidfile+0xdc>
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xe2c(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     11fa <writepidfile+0xaa>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <writepidfile+0xdc>
mov    -0x38(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1218 <writepidfile+0xc8>
jmp    1218 <writepidfile+0xc8>
jmp    121d <writepidfile+0xcd>
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     11b4 <writepidfile+0x74>
cmpb   $0x0,(%rdi)
je     11b4 <writepidfile+0x74>
mov    %esi,%ebp
mov    0x2e70(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11b4 <writepidfile+0x74>
mov    %eax,%r14d
lea    0xe7d(%rip),%rdx
mov    %rsp,%rdi
mov    $0x10,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
jle    11a7 <writepidfile+0x67>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebx,%ebx
jmp    11ac <writepidfile+0x6c>
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
@.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 %44
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 = call i32 @open(i8* %17, i32 %20, i32 420)
store i32 %21, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %3
br label %44
24: ; preds = %16
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%26 = load i32, i32* %5
%27 = call i32 @snprintf(i8* %25, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %44
33: ; preds = %24
%34 = load i32, i32* %8
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* %7
%37 = call i64 @write(i32 %34, i8* %35, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
br label %40
40: ; preds = %39, %33
br label %41
41: ; preds = %40
%42 = load i32, i32* %8
%43 = call i32 @close(i32 %42)
store i32 0, i32* %3
br label %44
44: ; preds = %41, %30, %23, %15
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = 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 %23, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %6
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* %0, i32 %12, i32 420)
%14 = icmp slt i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %9
%16 = call i32 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%17 = icmp slt i32 %16, 1
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = call i64 @write(i32 %13, i8* %4, i32 %16)
br label %20
20: ; preds = %15, %18
%21 = phi i32 [ 0, %18 ], [ -1, %15 ]
%22 = call i32 @close(i32 %13)
br label %23
23: ; preds = %20, %9, %2, %6
%24 = phi i32 [ -1, %6 ], [ -1, %2 ], [ -1, %9 ], [ %21, %20 ]
ret i32 %24
}