#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_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*/ printf(char *);
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;
  int flag = 1;
  char buff[64] = "sleep 10 seconds!\n";
  pid_t pid;
  pid = fork();
  if (0 > pid) {
    printf("Create process fail!\n");
    exit(1);
  }
  if (0 < pid)
    exit(0);
  setsid();
  chdir("/");
  umask(0);
  for (i = 0; i < 65535; i++) {
    close(i);
  }
  while (1) {
    if (1 == flag) {
      if (-1 ==
          (fd = open("/arm/150707_daemon/sleep.log", O_RDWR | O_CREAT, 0777))) {
        printf("Create file fail!\n");
        exit(1);
      }
      flag = 0;
    }
    write(fd, buff, strlen(buff));
    sleep(10);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe1f(%rip),%rsi
mov    $0x40,%edx
call   10a0 <memcpy@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x58(%rbp)
xor    %eax,%eax
cmp    -0x58(%rbp),%rax
jle    1218 <main+0x58>
lea    0xe37(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
cmp    -0x58(%rbp),%rax
jge    122b <main+0x6b>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
lea    0xe1d(%rip),%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
call   1090 <umask@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0xffff,-0x8(%rbp)
jge    126f <main+0xaf>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    124c <main+0x8c>
jmp    1274 <main+0xb4>
mov    $0x1,%eax
cmp    -0x10(%rbp),%eax
jne    12d4 <main+0x114>
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    0x2d66(%rip),%rax
or     (%rax),%esi
lea    0xdbd(%rip),%rdi
mov    $0x1ff,%edx
call   10b0 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12cd <main+0x10d>
lea    0xdb7(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x64(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
jmp    1274 <main+0xb4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
movaps 0xe6d(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe51(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe35(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe19(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jns    1203 <main+0x53>
lea    0xe73(%rip),%rdi
jmp    12a6 <main+0xf6>
jne    12b5 <main+0x105>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <setsid@plt>
lea    0xe27(%rip),%rdi
call   1060 <chdir@plt>
xor    %edi,%edi
call   1090 <umask@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x1,%ebx
cmp    $0xffff,%ebx
jne    1230 <main+0x80>
mov    $0x1,%al
mov    0x2d95(%rip),%r15
mov    0x2da6(%rip),%r12
lea    0xde9(%rip),%r14
mov    %rsp,%rbx
jmp    1280 <main+0xd0>
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
test   $0x1,%al
je     1260 <main+0xb0>
mov    (%r12),%esi
or     (%r15),%esi
mov    %r14,%rdi
mov    $0x1ff,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1260 <main+0xb0>
lea    0xdb9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
@__const.main.buff = [64 x i8] c"sleep 10 seconds!\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [29 x i8] c"/arm/150707_daemon/sleep.log\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@str = [18 x i8] c"Create file fail!\00"
@str.4 = [21 x i8] c"Create process fail!\00"
define i32 @main() {
%1 = alloca [64 x i8]
%2 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.4, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %8
%13 = tail call i32 (...) @setsid()
%14 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @umask(i32 0)
br label %16
16: ; preds = %12, %16
%17 = phi i32 [ 0, %12 ], [ %19, %16 ]
%18 = tail call i32 @close(i32 %17)
%19 = add nuw nsw i32 %17, 1
%20 = icmp eq i32 %19, 65535
br i1 %20, label %21, label %16
21: ; preds = %16, %33
%22 = phi i32 [ %34, %33 ], [ undef, %16 ]
%23 = phi i1 [ false, %33 ], [ true, %16 ]
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_CREAT
%27 = or i32 %26, %25
%28 = call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 511)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %24, %21
%34 = phi i32 [ %22, %21 ], [ %28, %24 ]
%35 = call i32 @strlen(i8* %2)
%36 = call i32 @write(i32 %34, i8* %2, i32 %35)
%37 = call i32 @sleep(i32 10)
br label %21
}