#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int SIGCHLD;
int /*<<< orphan*/ clone(int (*)(void *), void *, int, void *,
                         int /*<<< orphan*/ *);
void *malloc(int);
pid_t do_clone(int (*fn)(void *), void *arg, int flags) {
  void *stack;
  stack = malloc((8 * 1024 * 1024));
  if (!stack)
    return -ENOMEM;
  return clone(fn, stack + (8 * 1024 * 1024), flags | SIGCHLD, arg, NULL);
}


x86 assembly (clang, -O0):
<do_clone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x800000,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    115f <do_clone+0x3f>
xor    %eax,%eax
mov    0x2e83(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    118b <do_clone+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x800000,%rsi
mov    -0x1c(%rbp),%edx
mov    0x2e70(%rip),%rax
or     (%rax),%edx
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <clone@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_clone>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x800000,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1161 <do_clone+0x41>
add    $0x800000,%rax
mov    0x2ea0(%rip),%rcx
or     (%rcx),%ebp
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
mov    %r14,%rcx
xor    %r8d,%r8d
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <clone@plt>
mov    0x2e70(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@SIGCHLD = global i32 0
define i32 @do_clone(i32 (i8*)* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32 (i8*)*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i32 (i8*)* %0, i32 (i8*)** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = call i8* @malloc(i32 8388608)
store i8* %9, i8** %8
%10 = load i8*, i8** %8
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i32, i32* @ENOMEM
%14 = sub nsw i32 0, %13
store i32 %14, i32* %4
br label %24
15: ; preds = %3
%16 = load i32 (i8*)*, i32 (i8*)** %5
%17 = load i8*, i8** %8
%18 = getelementptr i8, i8* %17, i64 8388608
%19 = load i32, i32* %7
%20 = load i32, i32* @SIGCHLD
%21 = or i32 %19, %20
%22 = load i8*, i8** %6
%23 = call i32 @clone(i32 (i8*)* %16, i8* %18, i32 %21, i8* %22, i32* null)
store i32 %23, i32* %4
br label %24
24: ; preds = %15, %12
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@SIGCHLD = global i32 0
define i32 @do_clone(i32 (i8*)* %0, i8* %1, i32 %2) {
%4 = tail call dereferenceable_or_null(8388608) i8* @malloc(i32 8388608)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @ENOMEM
%8 = sub nsw i32 0, %7
br label %14
9: ; preds = %3
%10 = getelementptr i8, i8* %4, i64 8388608
%11 = load i32, i32* @SIGCHLD
%12 = or i32 %11, %2
%13 = tail call i32 @clone(i32 (i8*)* %0, i8* %10, i32 %12, i8* %1, i32* null)
br label %14
14: ; preds = %9, %6
%15 = phi i32 [ %13, %9 ], [ %8, %6 ]
ret i32 %15
}