#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 pid_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int getpid();
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int writepid(const char *pidfile) {
  char buff[20];
  pid_t pid = getpid();
  int fd;
  fd = open(pidfile, O_CREAT | O_WRONLY, S_IRWXU);
  if (fd == -1)
    return -1;
  sprintf(buff, "%d", pid);
  if (write(fd, buff, strlen(buff)) == -1) {
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<writepid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11b3 <writepid+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <writepid+0xb7>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xe3f(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1208 <writepid+0xa8>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <writepid+0xb7>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
mov    0x2e7c(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11dc <writepid+0x7c>
mov    %eax,%ebx
lea    0xe5a(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r14d,%edx
call   1080 <sprintf@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
mov    %ebx,%edi
call   1060 <close@plt>
neg    %ebp
jmp    11e1 <writepid+0x81>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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