#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*/ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main() {
  int pid;
  pid_t sid = 0;
  FILE *fp = NULL;
  pid = fork();
  printf("%d\n", pid);
  if (pid < 0) {
    printf("Fork failed\n");
    exit(1);
  } else if (pid > 0) {
    printf("Parent process\n");
    exit(0);
  } else {
    umask(0);
    sid = setsid();
    chdir("/");
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
    fp = fopen("log.txt", "w+");
    while (1) {
      sleep(5);
      fprintf(fp, "I slept for 5 seconds...\n");
      fflush(fp);
      openlog("abhilog", LOG_PID, LOG_USER);
      sleep(5);
      syslog(LOG_INFO, "I slept for 5 seconds..!");
      closelog();
    }
    fclose(fp);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    123b <main+0x5b>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jle    125a <main+0x7a>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %eax,-0xc(%rbp)
lea    0xdaf(%rip),%rdi
call   1040 <chdir@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
lea    0xd7b(%rip),%rdi
lea    0xd7c(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    $0x5,%edi
call   10f0 <sleep@plt>
mov    -0x18(%rbp),%rdi
lea    0xd61(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <fflush@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    0x2cf5(%rip),%rax
mov    (%rax),%edx
lea    0xd54(%rip),%rdi
call   10d0 <openlog@plt>
mov    $0x5,%edi
call   10f0 <sleep@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
lea    0xd3d(%rip),%rsi
call   10b0 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
jmp    12b8 <main+0xd8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1110 <fork@plt>
mov    %eax,%ebx
lea    0xdf5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebx,%ebx
js     12f8 <main+0x108>
jne    130e <main+0x11e>
xor    %edi,%edi
call   1090 <umask@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
lea    0xdcd(%rip),%rdi
call   1050 <chdir@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1070 <close@plt>
lea    0xd99(%rip),%rdi
lea    0xd9a(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rbx
mov    0x2d4d(%rip),%r13
mov    0x2d56(%rip),%rbp
lea    0xd9a(%rip),%r15
mov    0x2d28(%rip),%r14
lea    0xd94(%rip),%r12
nop
mov    $0x5,%edi
call   1100 <sleep@plt>
mov    $0x19,%esi
mov    $0x1,%edx
lea    0xd56(%rip),%rdi
mov    %rbx,%rcx
call   10f0 <fwrite@plt>
mov    %rbx,%rdi
call   10a0 <fflush@plt>
mov    0x0(%r13),%esi
mov    0x0(%rbp),%edx
mov    %r15,%rdi
call   10d0 <openlog@plt>
mov    $0x5,%edi
call   1100 <sleep@plt>
mov    (%r14),%edi
mov    %r12,%rsi
call   10b0 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
jmp    12a0 <main+0xb0>
lea    0xd5c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd37(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [13 x i8] c"Fork failed\0A\00"
@.str.2 = [16 x i8] c"Parent process\0A\00"
@.str.3 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [8 x i8] c"log.txt\00"
@.str.5 = [3 x i8] c"w+\00"
@.str.6 = [26 x i8] c"I slept for 5 seconds...\0A\00"
@.str.7 = [8 x i8] c"abhilog\00"
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.8 = [25 x i8] c"I slept for 5 seconds..!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %3
store i32* null, i32** %4
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %2
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %13
%20 = call i32 @umask(i32 0)
%21 = call i32 (...) @setsid()
store i32 %21, i32* %3
%22 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @STDIN_FILENO
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @STDERR_FILENO
%28 = call i32 @close(i32 %27)
%29 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %29, i32** %4
br label %30
30: ; preds = %19, %30
%31 = call i32 @sleep(i32 5)
%32 = load i32*, i32** %4
%33 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%34 = load i32*, i32** %4
%35 = call i32 @fflush(i32* %34)
%36 = load i32, i32* @LOG_PID
%37 = load i32, i32* @LOG_USER
%38 = call i32 @openlog(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %36, i32 %37)
%39 = call i32 @sleep(i32 5)
%40 = load i32, i32* @LOG_INFO
%41 = call i32 @syslog(i32 %40, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%42 = call i32 (...) @closelog()
br label %30
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.3 = [2 x i8] c"/\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [8 x i8] c"log.txt\00"
@.str.5 = [3 x i8] c"w+\00"
@.str.6 = [26 x i8] c"I slept for 5 seconds...\0A\00"
@.str.7 = [8 x i8] c"abhilog\00"
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_INFO = global i32 0
@.str.8 = [25 x i8] c"I slept for 5 seconds..!\00"
@str = [15 x i8] c"Parent process\00"
@str.9 = [12 x i8] c"Fork failed\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = icmp eq i32 %1, 0
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %7
%13 = tail call i32 @umask(i32 0)
%14 = tail call i32 (...) @setsid()
%15 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32, i32* @STDIN_FILENO
%17 = tail call i32 @close(i32 %16)
%18 = load i32, i32* @STDOUT_FILENO
%19 = tail call i32 @close(i32 %18)
%20 = load i32, i32* @STDERR_FILENO
%21 = tail call i32 @close(i32 %20)
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %23, %12
%24 = tail call i32 @sleep(i32 5)
%25 = tail call i64 @fwrite(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i64 25, i64 1, i32* %22)
%26 = tail call i32 @fflush(i32* %22)
%27 = load i32, i32* @LOG_PID
%28 = load i32, i32* @LOG_USER
%29 = tail call i32 @openlog(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 %27, i32 %28)
%30 = tail call i32 @sleep(i32 5)
%31 = load i32, i32* @LOG_INFO
%32 = tail call i32 @syslog(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%33 = tail call i32 (...) @closelog()
br label %23
}