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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int dprintf(int, char *, int);
int ftruncate(int, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int write_pid(int fd, pid_t pid) {
  if (ftruncate(fd, (off_t)0) == -1)
    return -1;
  lseek(fd, (off_t)0, SEEK_SET);
  return dprintf(fd, "%d\n", (int)pid);
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    115e <write_pid+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1189 <write_pid+0x59>
mov    -0x8(%rbp),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xe7f(%rip),%rsi
call   1050 <dprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %esi,%esi
call   1030 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     116c <write_pid+0x3c>
mov    0x2e95(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
lea    0xea3(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <dprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @write_pid(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
%6 = load i32, i32* %4
%7 = call i32 @ftruncate(i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 -1, i32* %3
br label %18
10: ; preds = %2
%11 = load i32, i32* %4
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @lseek(i32 %11, i32 0, i32 %12)
%14 = load i32, i32* %4
%15 = load i64, i64* %5
%16 = trunc i64 %15 to i32
%17 = call i32 @dprintf(i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %3
br label %18
18: ; preds = %10, %9
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [4 x i8] c"%d\0A\00"
define i32 @write_pid(i32 %0, i64 %1) {
%3 = tail call i32 @ftruncate(i32 %0, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %0, i32 0, i32 %6)
%8 = trunc i64 %1 to i32
%9 = tail call i32 @dprintf(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %2, %5
%11 = phi i32 [ %9, %5 ], [ -1, %2 ]
ret i32 %11
}