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

/* Variables and functions */
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ LOG_SYSLOG;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *args[]) {
  umask(0);
  pid_t pid = fork();
  if (pid < 0) {
    fprintf(stderr, "fork error:%s\n", strerror(errno));
    exit(1);
  } else if (pid > 0) {
    exit(1);
  }
  setsid();
  chdir("/");
  close(0);
  close(1);
  close(2);
  char *str = "hello world";
  openlog(args[0], LOG_PID, LOG_SYSLOG);
  while (1) {
    syslog(LOG_DEBUG, "%s", str);
    sleep(3);
  }
  closelog();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1219 <main+0x69>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    122e <main+0x7e>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1233 <main+0x83>
mov    $0x0,%al
call   1050 <setsid@plt>
lea    0xdce(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
lea    0xda9(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
call   1090 <openlog@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd7f(%rip),%rsi
call   1080 <syslog@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
jmp    128a <main+0xda>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
xor    %edi,%edi
call   1070 <umask@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     124a <main+0x9a>
jne    1272 <main+0xc2>
xor    %eax,%eax
call   1050 <setsid@plt>
lea    0xe30(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1040 <close@plt>
mov    (%rbx),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   1090 <openlog@plt>
mov    0x2da0(%rip),%r15
lea    0xdf6(%rip),%r14
lea    0xde3(%rip),%rbx
xchg   %ax,%ax
mov    (%r15),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <syslog@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
jmp    1230 <main+0x80>
mov    0x2d87(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd98(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"fork error:%s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [12 x i8] c"hello world\00"
@LOG_PID = global i32 0
@LOG_SYSLOG = global i32 0
@LOG_DEBUG = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @umask(i32 0)
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i64, i64* %6
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %18
br label %24
24: ; preds = %23
%25 = call i32 (...) @setsid()
%26 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @close(i32 0)
%28 = call i32 @close(i32 1)
%29 = call i32 @close(i32 2)
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %7
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = load i32, i32* @LOG_PID
%34 = load i32, i32* @LOG_SYSLOG
%35 = call i32 @openlog(i8* %32, i32 %33, i32 %34)
br label %36
36: ; preds = %24, %36
%37 = load i32, i32* @LOG_DEBUG
%38 = load i8*, i8** %7
%39 = call i32 @syslog(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = call i32 @sleep(i32 3)
br label %36
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"fork error:%s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [12 x i8] c"hello world\00"
@LOG_PID = global i32 0
@LOG_SYSLOG = global i32 0
@LOG_DEBUG = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @umask(i32 0)
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = icmp eq i64 %4, 0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %12
%17 = tail call i32 (...) @setsid()
%18 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @close(i32 0)
%20 = tail call i32 @close(i32 1)
%21 = tail call i32 @close(i32 2)
%22 = load i8*, i8** %1
%23 = load i32, i32* @LOG_PID
%24 = load i32, i32* @LOG_SYSLOG
%25 = tail call i32 @openlog(i8* %22, i32 %23, i32 %24)
br label %26
26: ; preds = %26, %16
%27 = load i32, i32* @LOG_DEBUG
%28 = tail call i32 @syslog(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @sleep(i32 3)
br label %26
}