#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int strlen(char const *);
int tbx_fork_redirect(char *, char *);
int tbx_fork(const char *prefix_std) {
  int fmax = 8192;
  char fname_stdout[fmax], fname_stderr[fmax];
  int n;
  if (fork() != 0) {
    exit(0);
  };
  n = strlen(prefix_std) + 10;
  if (n > fmax)
    return (4);
  fname_stdout[fmax - 1] = '\0';
  snprintf(fname_stdout, fmax - 1, "%s.stdout", prefix_std);
  fname_stderr[fmax - 1] = '\0';
  snprintf(fname_stderr, fmax - 1, "%s.stderr", prefix_std);
  return (tbx_fork_redirect(fname_stdout, fname_stderr));
}


x86 assembly (clang, -O0):
<tbx_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x2000,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
je     11c0 <tbx_fork+0x70>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0xa,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11ee <tbx_fork+0x9e>
movl   $0x4,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1251 <tbx_fork+0x101>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rcx
lea    0xdf1(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rcx
lea    0xdd5(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1050 <tbx_fork_redirect@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tbx_fork>:
push   %r15
push   %r14
push   %rbx
sub    $0x4000,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11e3 <tbx_fork+0x93>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    $0x4,%eax
cmp    $0x1ff6,%ecx
jg     11d6 <tbx_fork+0x86>
movb   $0x0,0x3fff(%rsp)
lea    0xe6f(%rip),%rdx
lea    0x2000(%rsp),%r14
mov    %r14,%rdi
mov    $0x1fff,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
movb   $0x0,0x1fff(%rsp)
lea    0xe52(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1fff,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <tbx_fork_redirect@plt>
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"%s.stdout\00"
@.str.1 = [10 x i8] c"%s.stderr\00"
define i32 @tbx_fork(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 8192, i32* %4
%10 = load i32, i32* %4
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
%14 = load i32, i32* %4
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
store i64 %15, i64* %7
%17 = call i64 (...) @fork()
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %1
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, 10
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = load i32, i32* %4
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 4, i32* %2
store i32 1, i32* %9
br label %47
29: ; preds = %21
%30 = load i32, i32* %4
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %13, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* %4
%35 = sub nsw i32 %34, 1
%36 = load i8*, i8** %3
%37 = call i32 @snprintf(i8* %13, i32 %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %4
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %16, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, 1
%44 = load i8*, i8** %3
%45 = call i32 @snprintf(i8* %16, i32 %43, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = call i32 @tbx_fork_redirect(i8* %13, i8* %16)
store i32 %46, i32* %2
store i32 1, i32* %9
br label %47
47: ; preds = %29, %28
%48 = load i8*, i8** %5
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s.stdout\00"
@.str.1 = [10 x i8] c"%s.stderr\00"
define i32 @tbx_fork(i8* %0) {
%2 = alloca [8192 x i8]
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%6 = tail call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %1
%11 = tail call i32 @strlen(i8* %0)
%12 = icmp sgt i32 %11, 8182
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 8191
store i8 0, i8* %14
%15 = call i32 @snprintf(i8* %5, i32 8191, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 8191
store i8 0, i8* %16
%17 = call i32 @snprintf(i8* %4, i32 8191, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = call i32 @tbx_fork_redirect(i8* %5, i8* %4)
br label %19
19: ; preds = %10, %13
%20 = phi i32 [ %18, %13 ], [ 4, %10 ]
ret i32 %20
}