#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int open(void *, int, ...);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int do_test(void *logf) {
  int fd, i = 0;
  setsid();
  close(0);
  close(1);
  close(2);
  fd = open("/dev/null", O_RDONLY);
  if (fd != 0) {
    dup2(fd, 0);
    close(fd);
  }
  fd = open(logf, O_WRONLY | O_TRUNC | O_CREAT, 0600);
  dup2(fd, 1);
  dup2(fd, 2);
  if (fd != 1 && fd != 2)
    close(fd);
  while (1) {
    sleep(1);
    printf("%d\n", i++);
    fflush(stdout);
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1060 <setsid@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11db <do_test+0x6b>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmpl   $0x1,-0xc(%rbp)
je     123f <do_test+0xcf>
cmpl   $0x2,-0xc(%rbp)
je     123f <do_test+0xcf>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    1244 <do_test+0xd4>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x10(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    %eax,-0x10(%rbp)
lea    0xda7(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    1244 <do_test+0xd4>

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <setsid@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
je     11c9 <do_test+0x59>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2de8(%rip),%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e16(%rip),%rax
or     (%rax),%esi
xor    %ebx,%ebx
mov    %r14,%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %r14d,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
lea    -0x3(%r14),%eax
cmp    $0xfffffffd,%eax
ja     1222 <do_test+0xb2>
mov    %r14d,%edi
call   1050 <close@plt>
lea    0xde1(%rip),%r14
mov    0x2d98(%rip),%r15
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0x1(%rbx),%ebp
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    (%r15),%edi
call   1070 <fflush@plt>
mov    %ebp,%ebx
jmp    1230 <do_test+0xc0>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@stdout = global i32 0
define i32 @do_test(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
%5 = call i32 (...) @setsid()
%6 = call i32 @close(i32 0)
%7 = call i32 @close(i32 1)
%8 = call i32 @close(i32 2)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i32 @dup2(i32 %14, i32 0)
%16 = load i32, i32* %3
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %13, %1
%19 = load i8*, i8** %2
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %24, i32 384)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = load i32, i32* %3
%29 = call i32 @dup2(i32 %28, i32 2)
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %38
32: ; preds = %18
%33 = load i32, i32* %3
%34 = icmp ne i32 %33, 2
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %32, %18
br label %39
39: ; preds = %38, %39
%40 = call i32 @sleep(i32 1)
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
%43 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%44 = load i32, i32* @stdout
%45 = call i32 @fflush(i32 %44)
br label %39
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@stdout = global i32 0
define i32 @do_test(i8* nocapture %0) {
%2 = tail call i32 (...) @setsid()
%3 = tail call i32 @close(i32 0)
%4 = tail call i32 @close(i32 1)
%5 = tail call i32 @close(i32 2)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = tail call i32 @dup2(i32 %7, i32 0)
%11 = tail call i32 @close(i32 %7)
br label %12
12: ; preds = %9, %1
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %14, %13
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %17, i32 384)
%19 = tail call i32 @dup2(i32 %18, i32 1)
%20 = tail call i32 @dup2(i32 %18, i32 2)
%21 = add i32 %18, -3
%22 = icmp ult i32 %21, -2
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = tail call i32 @close(i32 %18)
br label %25
25: ; preds = %23, %12
br label %26
26: ; preds = %25, %26
%27 = phi i32 [ %29, %26 ], [ 0, %25 ]
%28 = tail call i32 @sleep(i32 1)
%29 = add nuw nsw i32 %27, 1
%30 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%31 = load i32, i32* @stdout
%32 = tail call i32 @fflush(i32 %31)
br label %26
}