#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ spid;
typedef int pid_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int ftruncate(int, int /*<<< orphan*/);
scalar_t__ pwrite(int, char *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strlen(char *);
int writepid(int fd, pid_t pid) {
  char spid[16];
  ssize_t len;
  if (ftruncate(fd, (off_t)0) == -1)
    return -1;
  snprintf(spid, sizeof(spid), "%u\n", pid);
  len = pwrite(fd, spid, strlen(spid), (off_t)0);
  if (len != (ssize_t)strlen(spid))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<writepid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    116d <writepid+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <writepid+0xa5>
lea    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x10,%esi
lea    0xe80(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <pwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     11de <writepid+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <writepid+0xa5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11a2 <writepid+0x62>
lea    0xea1(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x10,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <pwrite@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rbx
setne  %al
neg    %eax
jmp    11a7 <writepid+0x67>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%u\0A\00"
define i32 @writepid(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [16 x i8]
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = call i32 @ftruncate(i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %27
12: ; preds = %2
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%14 = load i32, i32* %5
%15 = call i32 @snprintf(i8* %13, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %4
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%19 = call i64 @strlen(i8* %18)
%20 = call i64 @pwrite(i32 %16, i8* %17, i64 %19, i32 0)
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
%24 = icmp ne i64 %21, %23
br i1 %24, label %25, label %26
25: ; preds = %12
store i32 -1, i32* %3
br label %27
26: ; preds = %12
store i32 0, i32* %3
br label %27
27: ; preds = %26, %25, %11
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [4 x i8] c"%u\0A\00"
define i32 @writepid(i32 %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @ftruncate(i32 %0, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %14, label %7
7: ; preds = %2
%8 = call i32 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%9 = call i64 @strlen(i8* %4)
%10 = call i64 @pwrite(i32 %0, i8* %4, i64 %9, i32 0)
%11 = call i64 @strlen(i8* %4)
%12 = icmp ne i64 %10, %11
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %7, %2
%15 = phi i32 [ -1, %2 ], [ %13, %7 ]
ret i32 %15
}