#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int, int);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int writepid(void) {
  int fd;
  int wlen;
  char buf[9] = {0};
  if ((fd = open("/var/run/luci-bwc.pid", O_WRONLY | O_CREAT | O_TRUNC, 0600)) >
      -1) {
    wlen = snprintf(buf, sizeof(buf), "%i", getpid());
    write(fd, buf, wlen);
    close(fd);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<writepid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x15(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1060 <memset@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
lea    0xe66(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jle    11fc <writepid+0x9c>
lea    -0x15(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%ecx
mov    $0x9,%esi
lea    0xe45(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1203 <writepid+0xa3>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0x8(%rsp)
mov    0x2e64(%rip),%rax
mov    0x2e8d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movq   $0x0,(%rsp)
mov    0x2e72(%rip),%rax
or     (%rax),%esi
lea    0xe79(%rip),%rdi
mov    $0x180,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11ce <writepid+0x7e>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe6f(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x9,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11d3 <writepid+0x83>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/var/run/luci-bwc.pid\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [3 x i8] c"%i\00"
define i32 @writepid() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [9 x i8]
%5 = bitcast [9 x i8]* %4 to i8*
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %10, i32 384)
store i32 %11, i32* %2
%12 = icmp sgt i32 %11, -1
br i1 %12, label %13, label %23
13: ; preds = %0
%14 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%15 = call i32 (...) @getpid()
%16 = call i32 @snprintf(i8* %14, i32 9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %2
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* %3
%20 = call i32 @write(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
store i32 0, i32* %1
br label %24
23: ; preds = %0
store i32 -1, i32* %1
br label %24
24: ; preds = %23, %13
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [22 x i8] c"/var/run/luci-bwc.pid\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [3 x i8] c"%i\00"
define i32 @writepid() {
%1 = alloca [9 x i8]
%2 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %7, i32 384)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = tail call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %2, i32 9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = call i32 @write(i32 %8, i8* %2, i32 %12)
%14 = call i32 @close(i32 %8)
br label %15
15: ; preds = %0, %10
%16 = phi i32 [ 0, %10 ], [ -1, %0 ]
ret i32 %16
}