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

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ W_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int snprintf(char *, size_t, char *, char *, char const *, int);
int set_pidfile(const char *appname, int port, char *buf, size_t len) {
  int n = -1;
  assert(appname && buf && len);
  if (-1 == access("/var/run", W_OK))
    return -1;
  n = snprintf(buf, len, "%s/%s%d.pid", "/var/run", appname, port);
  if (n < 0)
    return EXIT_FAILURE;
  buf[len - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<set_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x2d(%rbp)
je     1179 <set_pidfile+0x49>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x2d(%rbp)
je     1179 <set_pidfile+0x49>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1050 <access@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11b6 <set_pidfile+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <set_pidfile+0xde>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
lea    0xe3c(%rip),%rdx
lea    0xe2c(%rip),%rcx
call   1030 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11f7 <set_pidfile+0xc7>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <set_pidfile+0xde>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pidfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%rbp
test   %rdi,%rdi
setne  %al
test   %rdx,%rdx
setne  %cl
and    %al,%cl
test   %r14,%r14
setne  %al
and    %cl,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1050 <access@plt>
cmp    $0xffffffff,%eax
je     11a8 <set_pidfile+0x78>
lea    0xe87(%rip),%rdx
lea    0xe77(%rip),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rbp,%r8
mov    %r15d,%r9d
call   1030 <snprintf@plt>
test   %eax,%eax
js     11af <set_pidfile+0x7f>
movb   $0x0,-0x1(%r14,%rbx,1)
xor    %eax,%eax
jmp    11b8 <set_pidfile+0x88>
mov    $0xffffffff,%eax
jmp    11b8 <set_pidfile+0x88>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/var/run\00"
@W_OK = global i32 0
@.str.1 = [12 x i8] c"%s/%s%d.pid\00"
@EXIT_FAILURE = global i32 0
define i32 @set_pidfile(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i32 -1, i32* %10
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %4
%14 = load i8*, i8** %8
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i64, i64* %9
%18 = icmp ne i64 %17, 0
br label %19
19: ; preds = %16, %13, %4
%20 = phi i1 [ false, %13 ], [ false, %4 ], [ %18, %16 ]
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* @W_OK
%24 = call i32 @access(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = icmp eq i32 -1, %24
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -1, i32* %5
br label %42
27: ; preds = %19
%28 = load i8*, i8** %8
%29 = load i64, i64* %9
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = call i32 @snprintf(i8* %28, i64 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %30, i32 %31)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = load i32, i32* @EXIT_FAILURE
store i32 %36, i32* %5
br label %42
37: ; preds = %27
%38 = load i8*, i8** %8
%39 = load i64, i64* %9
%40 = sub i64 %39, 1
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
store i32 0, i32* %5
br label %42
42: ; preds = %37, %35, %26
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@.str = [9 x i8] c"/var/run\00"
@W_OK = global i32 0
@.str.1 = [12 x i8] c"%s/%s%d.pid\00"
@EXIT_FAILURE = global i32 0
define i32 @set_pidfile(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %2, null
%7 = and i1 %5, %6
%8 = icmp ne i64 %3, 0
%9 = and i1 %7, %8
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = load i32, i32* @W_OK
%13 = tail call i32 @access(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %23, label %15
15: ; preds = %4
%16 = tail call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i32, i32* @EXIT_FAILURE
br label %23
20: ; preds = %15
%21 = add i64 %3, -1
%22 = getelementptr inbounds i8, i8* %2, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %4, %20, %18
%24 = phi i32 [ %19, %18 ], [ 0, %20 ], [ -1, %4 ]
ret i32 %24
}