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

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main() {
  pid_t pid;
  FILE *fp;
  int i;
  char *buf = "This is a example process\n";
  pid = fork();
  if (pid < 0) {
    printf("create child process fail\n");
    exit(1);
  }
  if (pid > 0) {
    exit(0);
  }
  setsid();
  for (i = 0; i < 1024; i++) {
    close(i);
  }
  chdir("/tmp");
  umask(0);
  signal(SIGCHLD, SIG_IGN);
  while (1) {
    fp = fopen("/tmp/test", "a");
    if (fp == NULL) {
      perror("open test file fail\n");
      exit(1);
    }
    fputs(buf, fp);
    fclose(fp);
    sleep(3);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe1a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1216 <main+0x46>
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    1228 <main+0x58>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x400,-0x1c(%rbp)
jge    1259 <main+0x89>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1236 <main+0x66>
lea    0xdd6(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   10a0 <umask@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
lea    0xdb1(%rip),%rdi
lea    0xdb4(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12bb <main+0xeb>
lea    0xd9b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
jmp    1283 <main+0xb3>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jns    11fa <main+0x2a>
lea    0xe56(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jne    12d0 <main+0x100>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <setsid@plt>
nopl   0x0(%rax)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    $0x400,%ebx
jne    1210 <main+0x40>
lea    0xdf2(%rip),%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
call   1090 <umask@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
lea    0xdcd(%rip),%rdi
lea    0xdd0(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12ba <main+0xea>
mov    %rax,%rbx
lea    0xd92(%rip),%r14
lea    0xdab(%rip),%r15
lea    0xdae(%rip),%r12
nopl   0x0(%rax)
mov    $0x1a,%esi
mov    $0x1,%edx
mov    %r14,%rdi
mov    %rbx,%rcx
call   10d0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1280 <main+0xb0>
lea    0xd6b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"This is a example process\0A\00"
@.str.1 = [27 x i8] c"create child process fail\0A\00"
@.str.2 = [5 x i8] c"/tmp\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.3 = [10 x i8] c"/tmp/test\00"
@.str.4 = [2 x i8] c"a\00"
@.str.5 = [21 x i8] c"open test file fail\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i64, i64* %2
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %12
%18 = call i32 (...) @setsid()
store i32 0, i32* %4
br label %19
19: ; preds = %25, %17
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 1024
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %19
28: ; preds = %19
%29 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @umask(i32 0)
%31 = load i32, i32* @SIGCHLD
%32 = load i32, i32* @SIG_IGN
%33 = call i32 @signal(i32 %31, i32 %32)
br label %34
34: ; preds = %28, %41
%35 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %35, i32** %3
%36 = load i32*, i32** %3
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = load i8*, i8** %5
%43 = load i32*, i32** %3
%44 = call i32 @fputs(i8* %42, i32* %43)
%45 = load i32*, i32** %3
%46 = call i32 @fclose(i32* %45)
%47 = call i32 @sleep(i32 3)
br label %34
}

LLVM IR (-O2):
@.str = [27 x i8] c"This is a example process\0A\00"
@.str.2 = [5 x i8] c"/tmp\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.3 = [10 x i8] c"/tmp/test\00"
@.str.4 = [2 x i8] c"a\00"
@.str.5 = [21 x i8] c"open test file fail\0A\00"
@str = [26 x i8] c"create child process fail\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %6
%11 = tail call i32 (...) @setsid()
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ 0, %10 ], [ %15, %12 ]
%14 = tail call i32 @close(i32 %13)
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, 1024
br i1 %16, label %17, label %12
17: ; preds = %12
%18 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @umask(i32 0)
%20 = load i32, i32* @SIGCHLD
%21 = load i32, i32* @SIG_IGN
%22 = tail call i32 @signal(i32 %20, i32 %21)
%23 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %28, %17
%26 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %17, %28
%29 = phi i32* [ %33, %28 ], [ %23, %17 ]
%30 = tail call i64 @fwrite(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 26, i64 1, i32* %29)
%31 = tail call i32 @fclose(i32* %29)
%32 = tail call i32 @sleep(i32 3)
%33 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %25, label %28
}