#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 */
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ write(int, char *, int);
int main() {
  struct timespec ts;
  if (fork())
    return 0;
  setsid();
  int m = 0;
  for (m = 0; m < 100; m++) {
    close(m);
  };
  ts.tv_sec = 10;
  ts.tv_nsec = 0;
  while (1) {
    int fd = open("/tmp/key", O_RDWR, 0);
    write(fd, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", 40);
    close(fd);
    ;
    nanosleep(&ts, NULL);
    if (fork() > 0)
      return 0;
    setsid();
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
je     118c <main+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    123c <main+0xdc>
mov    $0x0,%al
call   1060 <setsid@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    11c1 <main+0x61>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a1 <main+0x41>
movl   $0xa,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
lea    0xe15(%rip),%rsi
mov    $0x28,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jle    1230 <main+0xd0>
movl   $0x0,-0x4(%rbp)
jmp    123c <main+0xdc>
mov    $0x0,%al
call   1060 <setsid@plt>
jmp    11d0 <main+0x70>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1189 <main+0x29>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1060 <setsid@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    $0x64,%ebx
jne    1190 <main+0x30>
movl   $0xa,(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e32(%rip),%r12
lea    0xe43(%rip),%r14
lea    0xe45(%rip),%r15
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r12),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x28,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jg     117a <main+0x1a>
xor    %eax,%eax
call   1060 <setsid@plt>
jmp    11d0 <main+0x70>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@.str = [9 x i8] c"/tmp/key\00"
@O_RDWR = global i32 0
@.str.1 = [41 x i8] c"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @fork()
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 0, i32* %1
br label %35
8: ; preds = %0
%9 = call i32 (...) @setsid()
store i32 0, i32* %3
store i32 0, i32* %3
br label %10
10: ; preds = %16, %8
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 100
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* %3
%15 = call i32 @close(i32 %14)
br label %16
16: ; preds = %13
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %10
19: ; preds = %10
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i32 10, i32* %20
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i64 0, i64* %21
br label %22
22: ; preds = %19, %33
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %23, i32 0)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 40)
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
%30 = call i64 (...) @fork()
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %22
store i32 0, i32* %1
br label %35
33: ; preds = %22
%34 = call i32 (...) @setsid()
br label %22
35: ; preds = %32, %7
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@.str = [9 x i8] c"/tmp/key\00"
@O_RDWR = global i32 0
@.str.1 = [41 x i8] c"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %25
5: ; preds = %0
%6 = tail call i32 (...) @setsid()
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ 0, %5 ], [ %10, %7 ]
%9 = tail call i32 @close(i32 %8)
%10 = add nuw nsw i32 %8, 1
%11 = icmp eq i32 %10, 100
br i1 %11, label %12, label %7
12: ; preds = %7
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 10, i32* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i64 0, i64* %14
br label %15
15: ; preds = %23, %12
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %16, i32 0)
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 40)
%19 = call i32 @close(i32 %17)
%20 = call i32 @nanosleep(%struct.timespec* %1, i32* null)
%21 = call i64 (...) @fork()
%22 = icmp sgt i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = call i32 (...) @setsid()
br label %15
25: ; preds = %15, %0
ret i32 0
}