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

/* Variables and functions */
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int fsync(int);
scalar_t__ getpid();
scalar_t__ lock_file(int, int /*<<< orphan*/, int /*<<< orphan*/,
                     int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int write_pid(int fd) {
  char buf[64];
  snprintf(buf, 64, "%ld\n", (long int)getpid());
  if (lock_file(fd, F_WRLCK, SEEK_SET, 0, 0)) {
    return -1;
  }
  if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
    return -1;
  }
  return fsync(fd);
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rcx
mov    $0x40,%esi
lea    0xe73(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1060 <lock_file@plt>
cmp    $0x0,%rax
je     11c8 <write_pid+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <write_pid+0xc2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
cmp    %rcx,%rax
je     1217 <write_pid+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1222 <write_pid+0xc2>
mov    -0x8(%rbp),%edi
call   1080 <fsync@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe86(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
mov    %rax,%rcx
call   1070 <snprintf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <lock_file@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11e3 <write_pid+0x83>
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %rax,%r15
jne    11e3 <write_pid+0x83>
mov    %ebx,%edi
call   1080 <fsync@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%ld\0A\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
define i32 @write_pid(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = tail call i64 (...) @getpid()
%5 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %4)
%6 = load i32, i32* @F_WRLCK
%7 = load i32, i32* @SEEK_SET
%8 = call i64 @lock_file(i32 %0, i32 %6, i32 %7, i32 0, i32 0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = call i64 @strlen(i8* %3)
%12 = call i64 @write(i32 %0, i8* %3, i64 %11)
%13 = call i64 @strlen(i8* %3)
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @fsync(i32 %0)
br label %17
17: ; preds = %10, %1, %15
%18 = phi i32 [ %16, %15 ], [ -1, %1 ], [ -1, %10 ]
ret i32 %18
}