#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 /*<<< orphan*/ SEEK_SET;
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
scalar_t__ write(int, char *, int);
int write_pid_file(int fd, int pid) {
  char buf[33];
  int rv = 0;
  int count;
  count = snprintf(buf, sizeof buf, "%d", pid);
  if (count < 0)
    return -1;
  if (lseek(fd, 0, SEEK_SET) < 0)
    rv = -1;
  if (ftruncate(fd, 0) < 0)
    rv = -1;
  if (write(fd, buf, count) < 0)
    rv = -1;
  return rv;
}


x86 assembly (clang, -O0):
<write_pid_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x21,%esi
lea    0xe98(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1186 <write_pid_file+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <write_pid_file+0xab>
mov    -0x8(%rbp),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    11aa <write_pid_file+0x6a>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
jge    11c5 <write_pid_file+0x85>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11e5 <write_pid_file+0xa5>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ecx
mov    %edi,%r14d
lea    0xeaa(%rip),%rdx
mov    %rsp,%rdi
mov    $0x21,%esi
call   1040 <snprintf@plt>
test   %eax,%eax
js     11aa <write_pid_file+0x6a>
mov    %eax,%ebp
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    %r14d,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
mov    %rax,%r15
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %ebp,%edx
call   1030 <write@plt>
or     %r15,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    11af <write_pid_file+0x6f>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@SEEK_SET = global i32 0
define i32 @write_pid_file(i32 %0, i32 %1) {
%3 = alloca [33 x i8]
%4 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = load i32, i32* @SEEK_SET
%9 = call i64 @lseek(i32 %0, i32 0, i32 %8)
%10 = ashr i64 %9, 63
%11 = trunc i64 %10 to i32
%12 = call i64 @ftruncate(i32 %0, i32 0)
%13 = icmp slt i64 %12, 0
%14 = call i64 @write(i32 %0, i8* %4, i32 %5)
%15 = icmp slt i64 %14, 0
%16 = select i1 %15, i1 true, i1 %13
%17 = select i1 %16, i32 -1, i32 %11
br label %18
18: ; preds = %2, %7
%19 = phi i32 [ %17, %7 ], [ -1, %2 ]
ret i32 %19
}