#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 int uint8_t;
typedef int uint16_t;
typedef scalar_t__ ssize_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ my_func(char *, int, int, int, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
scalar_t__ read(int, unsigned char *, int);
unsigned char *realloc(unsigned char *, scalar_t__);
int /*<<< orphan*/ stdout;
int strlen(unsigned char *);
int /*<<< orphan*/ strncat(unsigned char *, unsigned char *, scalar_t__);
int /*<<< orphan*/ wait(int *);
void *jail(void *arg) {
  uint8_t id = *((uint8_t *)arg);
  pid_t fpid;
  int pfd[2];
  if (0) {
    fprintf(stdout, "Thread %d\n", id);
    fflush(stdout);
  };
  pipe(pfd);
  fpid = fork();
  if (0) {
    fprintf(stdout, "Fork PID: %d\n", fpid);
    fflush(stdout);
  };
  if (fpid >= 0) {
    if (0 == fpid) {
      close(pfd[0]);
      int moutfd = dup(STDOUT_FILENO);
      dup2(pfd[1], STDOUT_FILENO);
      my_func("Hello this is jail #%d written on %d instead of %d-%d-%d\n",
              pfd[1], id, moutfd, STDOUT_FILENO, fileno(stdout));
      dup2(moutfd, STDOUT_FILENO);
      close(moutfd);
      close(pfd[1]);
      exit(EXIT_SUCCESS);
    } else {
      close(pfd[1]);
      ssize_t nbytes;
      uint16_t len = 8192;
      unsigned char *result = (unsigned char *)malloc(sizeof(*result) * 2);
      *result = '\0';
      unsigned char *buffer = (unsigned char *)malloc(sizeof(*buffer) * len);
      if (0) {
        fprintf(stdout, "Parent Process (%d)\n", id);
        fflush(stdout);
      };
      while (nbytes = read(pfd[0], buffer, len)) {
        result =
            realloc(result, (sizeof(*result) * (strlen(result) + 1)) + nbytes);
        strncat(result, buffer, nbytes);
      }
      int state;
      wait(&state);
      return (void *)result;
    }
  } else {
    perror("Unable to fork");
  }
  return NULL;
}


x86 assembly (clang, -O0):
<jail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
call   1090 <pipe@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     137a <jail+0x18a>
xor    %eax,%eax
cmp    -0x18(%rbp),%eax
jne    12ce <jail+0xde>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    -0x5c(%rbp),%esi
mov    -0x58(%rbp),%edx
mov    -0x54(%rbp),%ecx
mov    -0x50(%rbp),%r8d
mov    %eax,%r9d
lea    0xd66(%rip),%rdi
call   1050 <my_func@plt>
mov    -0x24(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x2000,-0x34(%rbp)
mov    $0x2,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
movslq -0x34(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   10a0 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1364 <jail+0x174>
mov    -0x40(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x68(%rbp),%rdi
add    $0x1,%eax
movslq %eax,%rsi
shl    $0x0,%rsi
add    -0x30(%rbp),%rsi
call   10d0 <realloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <strncat@plt>
jmp    1305 <jail+0x115>
lea    -0x4c(%rbp),%rdi
call   1100 <wait@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    138e <jail+0x19e>
lea    0xcb9(%rip),%rdi
call   10e0 <perror@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jail>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    (%rdi),%ebx
lea    0x4(%rsp),%rdi
call   1090 <pipe@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
js     12be <jail+0xce>
je     12cf <jail+0xdf>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    $0x2,%edi
call   10c0 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    $0x2000,%edi
call   10c0 <malloc@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edi
mov    %rax,%rsi
mov    $0x2000,%edx
call   10a0 <read@plt>
test   %rax,%rax
je     12a4 <jail+0xb4>
mov    %rax,%rbx
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rax,%rbx,1),%rsi
add    $0x1,%rsi
mov    %r15,%rdi
call   10d0 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <strncat@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x2000,%edx
call   10a0 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1260 <jail+0x70>
lea    0xc(%rsp),%rdi
call   1100 <wait@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd75(%rip),%rdi
call   10e0 <perror@plt>
xor    %r15d,%r15d
jmp    12ae <jail+0xbe>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
mov    0x2d11(%rip),%r12
mov    (%r12),%edi
call   1060 <dup@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
mov    (%r12),%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%r14d
mov    (%r12),%r15d
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
lea    0xceb(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %r15d,%r8d
mov    %eax,%r9d
call   1050 <my_func@plt>
mov    (%r12),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [58 x i8] c"Hello this is jail #%d written on %d instead of %d-%d-%d\0A\00"
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [15 x i8] c"Unable to fork\00"
define i8* @jail(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %3
%13 = load i8*, i8** %3
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
store i32 %15, i32* %4
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = call i32 (...) @fork()
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %89
21: ; preds = %1
%22 = load i32, i32* %5
%23 = icmp eq i32 0, %22
br i1 %23, label %24, label %52
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @dup(i32 %28)
store i32 %29, i32* %7
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%35 = load i32, i32* %34
%36 = load i32, i32* %4
%37 = load i32, i32* %7
%38 = load i32, i32* @STDOUT_FILENO
%39 = load i32, i32* @stdout
%40 = call i32 @fileno(i32 %39)
%41 = call i32 @my_func(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36, i32 %37, i32 %38, i32 %40)
%42 = load i32, i32* %7
%43 = load i32, i32* @STDOUT_FILENO
%44 = call i32 @dup2(i32 %42, i32 %43)
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* @EXIT_SUCCESS
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %21
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
store i32 8192, i32* %9
%56 = call i64 @malloc(i32 2)
%57 = inttoptr i64 %56 to i8*
store i8* %57, i8** %10
%58 = load i8*, i8** %10
store i8 0, i8* %58
%59 = load i32, i32* %9
%60 = sext i32 %59 to i64
%61 = mul i64 1, %60
%62 = trunc i64 %61 to i32
%63 = call i64 @malloc(i32 %62)
%64 = inttoptr i64 %63 to i8*
store i8* %64, i8** %11
br label %65
65: ; preds = %72, %52
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%67 = load i32, i32* %66
%68 = load i8*, i8** %11
%69 = load i32, i32* %9
%70 = call i64 @read(i32 %67, i8* %68, i32 %69)
store i64 %70, i64* %8
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %86
72: ; preds = %65
%73 = load i8*, i8** %10
%74 = load i8*, i8** %10
%75 = call i32 @strlen(i8* %74)
%76 = add nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = mul i64 1, %77
%79 = load i64, i64* %8
%80 = add i64 %78, %79
%81 = call i8* @realloc(i8* %73, i64 %80)
store i8* %81, i8** %10
%82 = load i8*, i8** %10
%83 = load i8*, i8** %11
%84 = load i64, i64* %8
%85 = call i32 @strncat(i8* %82, i8* %83, i64 %84)
br label %65
86: ; preds = %65
%87 = call i32 @wait(i32* %12)
%88 = load i8*, i8** %10
store i8* %88, i8** %2
br label %92
89: ; preds = %1
%90 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %91
91: ; preds = %89
store i8* null, i8** %2
br label %92
92: ; preds = %91, %86
%93 = load i8*, i8** %2
ret i8* %93
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [58 x i8] c"Hello this is jail #%d written on %d instead of %d-%d-%d\0A\00"
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [15 x i8] c"Unable to fork\00"
define i8* @jail(i8* nocapture %0) {
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = bitcast [2 x i32]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %61
11: ; preds = %1
%12 = icmp eq i32 %9, 0
br i1 %12, label %13, label %34
13: ; preds = %11
%14 = load i32, i32* %7
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @dup(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = load i32, i32* %18
%23 = load i32, i32* @STDOUT_FILENO
%24 = load i32, i32* @stdout
%25 = call i32 @fileno(i32 %24)
%26 = call i32 @my_func(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %5, i32 %17, i32 %23, i32 %25)
%27 = load i32, i32* @STDOUT_FILENO
%28 = call i32 @dup2(i32 %17, i32 %27)
%29 = call i32 @close(i32 %17)
%30 = load i32, i32* %18
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %11
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = call i64 @malloc(i32 2)
%39 = inttoptr i64 %38 to i8*
store i8 0, i8* %39
%40 = call i64 @malloc(i32 8192)
%41 = inttoptr i64 %40 to i8*
%42 = load i32, i32* %7
%43 = call i64 @read(i32 %42, i8* %41, i32 8192)
%44 = icmp eq i64 %43, 0
br i1 %44, label %57, label %45
45: ; preds = %34, %45
%46 = phi i64 [ %55, %45 ], [ %43, %34 ]
%47 = phi i8* [ %52, %45 ], [ %39, %34 ]
%48 = call i32 @strlen(i8* %47)
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = add i64 %46, %50
%52 = call i8* @realloc(i8* %47, i64 %51)
%53 = call i32 @strncat(i8* %52, i8* %41, i64 %46)
%54 = load i32, i32* %7
%55 = call i64 @read(i32 %54, i8* %41, i32 8192)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %45
57: ; preds = %45, %34
%58 = phi i8* [ %39, %34 ], [ %52, %45 ]
%59 = bitcast i32* %3 to i8*
%60 = call i32 @wait(i32* %3)
br label %63
61: ; preds = %1
%62 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %63
63: ; preds = %61, %57
%64 = phi i8* [ %58, %57 ], [ null, %61 ]
ret i8* %64
}