#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 */
struct lsapi_packet_header {
  int dummy;
};

/* Variables and functions */
int getpid();
int /*<<< orphan*/ lsapi_buildPacketHeader(struct lsapi_packet_header *, int,
                                           int);
int /*<<< orphan*/ memmove(char *, char *, int);
int write(int, char *, int);
int lsapi_notify_pid(int fd) {
  char achBuf[16];
  lsapi_buildPacketHeader((struct lsapi_packet_header *)achBuf, 6, 8 + 8);
  memmove(&achBuf[8], "\0PID", 4);
  *((int *)&achBuf[12]) = getpid();
  if (write(fd, achBuf, 16) < 16)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<lsapi_notify_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x6,%esi
mov    $0x10,%edx
call   1050 <lsapi_buildPacketHeader@plt>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
lea    0xe93(%rip),%rsi
mov    $0x4,%edx
call   1060 <memmove@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x10,%eax
jge    11a7 <lsapi_notify_pid+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <lsapi_notify_pid+0x6e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsapi_notify_pid>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x6,%esi
mov    $0x10,%edx
call   1050 <lsapi_buildPacketHeader@plt>
lea    0x8(%rsp),%rdi
lea    0xe96(%rip),%rsi
mov    $0x4,%edx
call   1060 <memmove@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1030 <write@plt>
xor    %ecx,%ecx
cmp    $0x10,%eax
setl   %cl
neg    %ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.lsapi_packet_header = type { i32 }
@.str = [5 x i8] c"\00PID\00"
define i32 @lsapi_notify_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 = bitcast i8* %5 to %struct.lsapi_packet_header*
%7 = call i32 @lsapi_buildPacketHeader(%struct.lsapi_packet_header* %6, i32 6, i32 16)
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 8
%9 = call i32 @memmove(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%10 = call i32 (...) @getpid()
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 12
%12 = bitcast i8* %11 to i32*
store i32 %10, i32* %12
%13 = load i32, i32* %3
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%15 = call i32 @write(i32 %13, i8* %14, i32 16)
%16 = icmp slt i32 %15, 16
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %19
18: ; preds = %1
store i32 0, i32* %2
br label %19
19: ; preds = %18, %17
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
%struct.lsapi_packet_header = type { i32 }
@.str = [5 x i8] c"\00PID\00"
define i32 @lsapi_notify_pid(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = bitcast [16 x i8]* %2 to %struct.lsapi_packet_header*
%5 = call i32 @lsapi_buildPacketHeader(%struct.lsapi_packet_header* %4, i32 6, i32 16)
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 8
%7 = call i32 @memmove(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%8 = call i32 (...) @getpid()
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 12
%10 = bitcast i8* %9 to i32*
store i32 %8, i32* %10
%11 = call i32 @write(i32 %0, i8* %3, i32 16)
%12 = icmp slt i32 %11, 16
%13 = sext i1 %12 to i32
ret i32 %13
}