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

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int getpid();
int open(char const *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int pidfile_write(const char *filename) {
  char buff[8];
  int fd, exitno;
  exitno = 0;
  fd = open(filename, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
  if (fd == -1)
    return -1;
  snprintf(buff, sizeof(buff), "%d", getpid());
  if (write(fd, buff, strlen(buff)) == -1)
    exitno = -1;
  close(fd);
  return exitno;
}


x86 assembly (clang, -O0):
<pidfile_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    0x2e37(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b9 <pidfile_write+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <pidfile_write+0xc2>
lea    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%ecx
mov    $0x8,%esi
lea    0xe26(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1214 <pidfile_write+0xb4>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pidfile_write>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e64(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11db <pidfile_write+0x7b>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5f(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
mov    %ebx,%edi
call   1070 <close@plt>
jmp    11e0 <pidfile_write+0x80>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @pidfile_write(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [8 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IWUSR
%12 = load i32, i32* @S_IRUSR
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %7, i32 %10, i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %33
18: ; preds = %1
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%20 = call i32 (...) @getpid()
%21 = call i32 @snprintf(i8* %19, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %5
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 %22, i8* %23, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %18
store i32 -1, i32* %6
br label %29
29: ; preds = %28, %18
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %6
store i32 %32, i32* %2
br label %33
33: ; preds = %29, %17
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @pidfile_write(i8* nocapture %0) {
%2 = alloca [8 x i8]
%3 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IWUSR
%8 = load i32, i32* @S_IRUSR
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %6, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %20, label %12
12: ; preds = %1
%13 = tail call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %3, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @strlen(i8* %3)
%16 = call i32 @write(i32 %10, i8* %3, i32 %15)
%17 = icmp eq i32 %16, -1
%18 = sext i1 %17 to i32
%19 = call i32 @close(i32 %10)
br label %20
20: ; preds = %1, %12
%21 = phi i32 [ %18, %12 ], [ -1, %1 ]
ret i32 %21
}