#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*/ close(int);
scalar_t__ getpid();
int /*<<< orphan*/ lprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int lock_write_pid(int fd) {
  char apid[16];
  sprintf(apid, "%10d\n", (int)getpid());
  if (write(fd, apid, strlen(apid)) != strlen(apid)) {
    lprintf(0, "cannot write PID to (temp) lock file");
    close(fd);
    return (-1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<lock_write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xe79(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     11f1 <lock_write_pid+0x91>
xor    %edi,%edi
lea    0xe2e(%rip),%rsi
call   1080 <lprintf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <lock_write_pid+0xa0>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe82(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    %rax,%rbx
je     11c5 <lock_write_pid+0x65>
lea    0xe4e(%rip),%rsi
xor    %edi,%edi
call   1080 <lprintf@plt>
mov    $0xffffffff,%r14d
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%10d\0A\00"
@.str.1 = [37 x i8] c"cannot write PID to (temp) lock file\00"
define i32 @lock_write_pid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
store i32 %0, i32* %3
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = call i64 (...) @getpid()
%7 = trunc i64 %6 to i32
%8 = call i32 @sprintf(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %3
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%12 = call i64 @strlen(i8* %11)
%13 = call i64 @write(i32 %9, i8* %10, i64 %12)
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%15 = call i64 @strlen(i8* %14)
%16 = icmp ne i64 %13, %15
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = call i32 @lprintf(i32 0, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %3
%20 = call i32 @close(i32 %19)
store i32 -1, i32* %2
br label %24
21: ; preds = %1
%22 = load i32, i32* %3
%23 = call i32 @close(i32 %22)
store i32 0, i32* %2
br label %24
24: ; preds = %21, %17
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [6 x i8] c"%10d\0A\00"
@.str.1 = [37 x i8] c"cannot write PID to (temp) lock file\00"
define i32 @lock_write_pid(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = tail call i64 (...) @getpid()
%5 = trunc i64 %4 to i32
%6 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i64 @strlen(i8* %3)
%8 = call i64 @write(i32 %0, i8* %3, i64 %7)
%9 = call i64 @strlen(i8* %3)
%10 = icmp eq i64 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = tail call i32 @lprintf(i32 0, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %1, %11
%14 = phi i32 [ -1, %11 ], [ 0, %1 ]
%15 = tail call i32 @close(i32 %0)
ret i32 %14
}