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

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int a_fork(int, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char const *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char const *);
int write(int, char *, int /*<<< orphan*/);
int a_daemon(const char *pidfile, int nochdir) {
  int fd;
  int err;
  pid_t pid;
  char buf[16];
  fd = open(pidfile, O_WRONLY | O_CREAT | O_EXCL, 0640);
  if (fd < 0)
    return -2;
  err = a_fork(nochdir, 0);
  if (err) {
    close(fd);
    return err;
  }
  pid = getpid();
  memset(buf, 0, sizeof buf);
  snprintf(buf, sizeof buf, "%i\n", pid);
  err = write(fd, buf, strlen(buf));
  close(fd);
  if (err < 0) {
    unlink(pidfile);
    return err;
  }
  return pid;
}


x86 assembly (clang, -O0):
<a_daemon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e1 <a_daemon+0x51>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1291 <a_daemon+0x101>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   10b0 <a_fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     120b <a_daemon+0x7b>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1291 <a_daemon+0x101>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%ecx
mov    $0x10,%esi
lea    0xdc8(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    128b <a_daemon+0xfb>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1291 <a_daemon+0x101>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a_daemon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e27(%rip),%rcx
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e20(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a0,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     11e4 <a_daemon+0x54>
mov    %eax,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   10b0 <a_fork@plt>
test   %eax,%eax
je     11eb <a_daemon+0x5b>
mov    %eax,%ebx
mov    %ebp,%edi
call   1090 <close@plt>
jmp    124a <a_daemon+0xba>
mov    $0xfffffffe,%ebx
jmp    124a <a_daemon+0xba>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
lea    0xdf3(%rip),%rdx
mov    %r15,%rdi
mov    $0x10,%esi
mov    %ebx,%ecx
call   1070 <snprintf@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1090 <close@plt>
test   %r15d,%r15d
jns    124a <a_daemon+0xba>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %r15d,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [4 x i8] c"%i\0A\00"
define i32 @a_daemon(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [16 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_EXCL
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 416)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -2, i32* %3
br label %51
20: ; preds = %2
%21 = load i32, i32* %5
%22 = call i32 @a_fork(i32 %21, i32 0)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %7
store i32 %28, i32* %3
br label %51
29: ; preds = %20
%30 = call i32 (...) @getpid()
store i32 %30, i32* %8
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i32 0, i32 16)
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%34 = load i32, i32* %8
%35 = call i32 @snprintf(i8* %33, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %6
%37 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = call i32 @write(i32 %36, i8* %37, i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %7
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %29
%46 = load i8*, i8** %4
%47 = call i32 @unlink(i8* %46)
%48 = load i32, i32* %7
store i32 %48, i32* %3
br label %51
49: ; preds = %29
%50 = load i32, i32* %8
store i32 %50, i32* %3
br label %51
51: ; preds = %49, %45, %25, %19
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [4 x i8] c"%i\0A\00"
define i32 @a_daemon(i8* nocapture %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_EXCL
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 416)
%11 = icmp slt i32 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %2
%13 = tail call i32 @a_fork(i32 %1, i32 0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @close(i32 %10)
br label %27
17: ; preds = %12
%18 = tail call i32 (...) @getpid()
%19 = call i32 @memset(i8* %4, i32 0, i32 16)
%20 = call i32 @snprintf(i8* %4, i32 16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
%21 = call i32 @strlen(i8* %4)
%22 = call i32 @write(i32 %10, i8* %4, i32 %21)
%23 = call i32 @close(i32 %10)
%24 = icmp slt i32 %22, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @unlink(i8* %0)
br label %27
27: ; preds = %17, %2, %25, %15
%28 = phi i32 [ %13, %15 ], [ %22, %25 ], [ -2, %2 ], [ %18, %17 ]
ret i32 %28
}