#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 /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  int i = 0;
  int j = 0;
  char *pathname = (char *)malloc(20 * sizeof(char));
  const char *test = "test";
  pid = fork();
  if (pid < 0) {
    fprintf(stdout, "error in fork\n");
    exit(1);
  } else if (pid > 0) {
    exit(0);
  }
  setsid();
  chdir("/tmp");
  umask(0);
  for (i = 0; i < 65535; ++i) {
    close(i);
  }
  for (;;) {
    sleep(60);
    sprintf(pathname, "%s%d", test, j);
    mkdir(pathname, S_IRWXU);
    ++j;
    j = j % 25;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x14,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe1e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    121b <main+0x6b>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    122d <main+0x7d>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1232 <main+0x82>
mov    $0x0,%al
call   1060 <setsid@plt>
lea    0xdd4(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xffff,-0x14(%rbp)
jge    1276 <main+0xc6>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1253 <main+0xa3>
jmp    127b <main+0xcb>
mov    $0x3c,%edi
call   10c0 <sleep@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    0xd82(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x19,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
jmp    127b <main+0xcb>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x14,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1280 <main+0xd0>
jne    129f <main+0xef>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <setsid@plt>
lea    0xe28(%rip),%rdi
call   1040 <chdir@plt>
xor    %edi,%edi
call   1080 <umask@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    $0xffff,%ebx
jne    1200 <main+0x50>
xor    %ebx,%ebx
lea    0xdfe(%rip),%r15
lea    0xdde(%rip),%r12
mov    0x2dbf(%rip),%r13
nopl   0x0(%rax)
mov    $0x3c,%edi
call   10c0 <sleep@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %ebx,%ecx
call   10a0 <sprintf@plt>
mov    0x0(%r13),%esi
mov    %r14,%rdi
call   1030 <mkdir@plt>
mov    %ebx,%eax
add    $0x1,%eax
cltq
imul   $0x51eb851f,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%eax
lea    (%rax,%rax,4),%eax
neg    %eax
add    %eax,%ebx
add    $0x1,%ebx
jmp    1230 <main+0x80>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"test\00"
@stdout = global i32 0
@.str.1 = [15 x i8] c"error in fork\0A\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [5 x i8] c"%s%d\00"
@S_IRWXU = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %3
store i32 0, i32* %4
%7 = call i64 @malloc(i32 20)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %6
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = load i32, i32* @stdout
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i64, i64* %2
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %16
br label %22
22: ; preds = %21
%23 = call i32 (...) @setsid()
%24 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @umask(i32 0)
store i32 0, i32* %3
br label %26
26: ; preds = %32, %22
%27 = load i32, i32* %3
%28 = icmp slt i32 %27, 65535
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %26
35: ; preds = %26
br label %36
36: ; preds = %36, %35
%37 = call i32 @sleep(i32 60)
%38 = load i8*, i8** %5
%39 = load i8*, i8** %6
%40 = load i32, i32* %4
%41 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %39, i32 %40)
%42 = load i8*, i8** %5
%43 = load i32, i32* @S_IRWXU
%44 = call i32 @mkdir(i8* %42, i32 %43)
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
%47 = load i32, i32* %4
%48 = srem i32 %47, 25
store i32 %48, i32* %4
br label %36
}

LLVM IR (-O2):
@.str = [5 x i8] c"test\00"
@stdout = global i32 0
@.str.1 = [15 x i8] c"error in fork\0A\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [5 x i8] c"%s%d\00"
@S_IRWXU = global i32 0
define i32 @main() {
%1 = tail call i64 @malloc(i32 20)
%2 = inttoptr i64 %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stdout
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = icmp eq i64 %3, 0
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %9
%14 = tail call i32 (...) @setsid()
%15 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @umask(i32 0)
br label %17
17: ; preds = %13, %17
%18 = phi i32 [ 0, %13 ], [ %20, %17 ]
%19 = tail call i32 @close(i32 %18)
%20 = add nuw nsw i32 %18, 1
%21 = icmp eq i32 %20, 65535
br i1 %21, label %22, label %17
22: ; preds = %17, %22
%23 = phi i32 [ %29, %22 ], [ 0, %17 ]
%24 = tail call i32 @sleep(i32 60)
%25 = tail call i32 @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %23)
%26 = load i32, i32* @S_IRWXU
%27 = tail call i32 @mkdir(i8* %2, i32 %26)
%28 = add nsw i32 %23, 1
%29 = srem i32 %28, 25
br label %22
}