#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 O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int i;
  int fd;
  char *content = "sundy print log\n";
  pid_t pid = fork();
  if (pid < 0) {
    exit(1);
  } else if (pid > 0) {
    exit(0);
  }
  setsid();
  for (i = 0; i < 101122; i++) {
    close(i);
  }
  chdir("/");
  umask(0);
  while (1) {
    fd = open("/tmp/mylog.log", O_CREAT | O_RDWR | O_APPEND, 06666);
    if (fd > 0) {
      write(fd, content, strlen(content));
      close(fd);
    }
    sleep(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11da <main+0x3a>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    11ec <main+0x4c>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    11f1 <main+0x51>
mov    $0x0,%al
call   1070 <setsid@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x18b02,-0x8(%rbp)
jge    1222 <main+0x82>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ff <main+0x5f>
lea    0xde8(%rip),%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
or     (%rax),%esi
mov    0x2d7a(%rip),%rax
or     (%rax),%esi
lea    0xdbc(%rip),%rdi
mov    $0xdb6,%edx
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    129b <main+0xfb>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
jmp    1235 <main+0x95>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1265 <main+0xc5>
jne    126f <main+0xcf>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <setsid@plt>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x1,%ebx
cmp    $0x18b02,%ebx
jne    11d0 <main+0x30>
lea    0xe28(%rip),%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    0x2dfc(%rip),%r15
mov    0x2ddd(%rip),%r12
mov    0x2dbe(%rip),%r13
lea    0xe02(%rip),%r14
lea    0xde8(%rip),%rbx
jmp    122a <main+0x8a>
nopw   0x0(%rax,%rax,1)
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    (%r12),%esi
or     (%r15),%esi
or     0x0(%r13),%esi
mov    %r14,%rdi
mov    $0xdb6,%edx
call   1090 <open@plt>
test   %eax,%eax
jle    1220 <main+0x80>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1220 <main+0x80>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"sundy print log\0A\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [15 x i8] c"/tmp/mylog.log\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = call i64 (...) @fork()
store i64 %6, i64* %5
%7 = load i64, i64* %5
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i64, i64* %5
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %11
br label %17
17: ; preds = %16
%18 = call i32 (...) @setsid()
store i32 0, i32* %2
br label %19
19: ; preds = %25, %17
%20 = load i32, i32* %2
%21 = icmp slt i32 %20, 101122
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %2
%27 = add nsw i32 %26, 1
store i32 %27, i32* %2
br label %19
28: ; preds = %19
%29 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @umask(i32 0)
br label %31
31: ; preds = %28, %48
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* @O_RDWR
%34 = or i32 %32, %33
%35 = load i32, i32* @O_APPEND
%36 = or i32 %34, %35
%37 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 3510)
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %31
%41 = load i32, i32* %3
%42 = load i8*, i8** %4
%43 = load i8*, i8** %4
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @write(i32 %41, i8* %42, i32 %44)
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %40, %31
%49 = call i32 @sleep(i32 3)
br label %31
}

LLVM IR (-O2):
@.str = [17 x i8] c"sundy print log\0A\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [15 x i8] c"/tmp/mylog.log\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @exit(i32 1)
unreachable
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %5
%10 = tail call i32 (...) @setsid()
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ 0, %9 ], [ %14, %11 ]
%13 = tail call i32 @close(i32 %12)
%14 = add nuw nsw i32 %12, 1
%15 = icmp eq i32 %14, 101122
br i1 %15, label %16, label %11
16: ; preds = %11
%17 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @umask(i32 0)
br label %19
19: ; preds = %31, %16
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_RDWR
%22 = or i32 %21, %20
%23 = load i32, i32* @O_APPEND
%24 = or i32 %22, %23
%25 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 3510)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%29 = tail call i32 @write(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = tail call i32 @close(i32 %25)
br label %31
31: ; preds = %27, %19
%32 = tail call i32 @sleep(i32 3)
br label %19
}