#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 EINVAL;
int INLINE_SYSCALL(int (*)(int (*)(void *), void *, int, void *), int, int,
                   void *);
int /*<<< orphan*/ __set_errno(int);
int /*<<< orphan*/ _exit(int);
int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, ...) {
  register int (*_fn)(void *arg) = fn;
  register void *_arg = arg;
  int err;
  err = -EINVAL;
  if (!fn)
    goto syscall_error;
  if (!child_stack)
    goto syscall_error;
  err = INLINE_SYSCALL(clone, 2, flags, child_stack);
  if (err < 0)
    goto syscall_error;
  else if (err != 0)
    return err;
  _exit(_fn(_arg));
syscall_error:
  __set_errno(-err);
  return -1;
}


x86 assembly (clang, -O0):
<clone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
mov    0x2e80(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <clone+0x45>
jmp    11da <clone+0xaa>
cmpq   $0x0,-0x18(%rbp)
jne    1185 <clone+0x55>
jmp    11da <clone+0xaa>
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    0x2e3d(%rip),%rdi
mov    $0x2,%esi
call   1050 <INLINE_SYSCALL@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11af <clone+0x7f>
jmp    11da <clone+0xaa>
cmpl   $0x0,-0x3c(%rbp)
je     11c4 <clone+0x94>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11eb <clone+0xbb>
jmp    11c9 <clone+0x99>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rdi
call   *%rax
mov    %eax,%edi
call   1040 <_exit@plt>
xor    %edi,%edi
sub    -0x3c(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clone>:
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%rcx
mov    0x2e9f(%rip),%rsi
xor    %eax,%eax
sub    (%rsi),%eax
test   %rdi,%rdi
je     1175 <clone+0x45>
test   %rcx,%rcx
je     1175 <clone+0x45>
mov    %rdi,%rbx
mov    0x2e77(%rip),%rdi
mov    $0x2,%esi
call   1050 <INLINE_SYSCALL@plt>
test   %eax,%eax
js     1175 <clone+0x45>
jne    1183 <clone+0x53>
mov    %r14,%rdi
call   *%rbx
mov    %eax,%edi
call   1040 <_exit@plt>
neg    %eax
mov    %eax,%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EINVAL = global i32 0
define i32 @clone(i32 (i8*)* %0, i8* %1, i32 %2, i8* %3, ...) {
%5 = alloca i32
%6 = alloca i32 (i8*)*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32 (i8*)*
%11 = alloca i8*
%12 = alloca i32
store i32 (i8*)* %0, i32 (i8*)** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i32 (i8*)*, i32 (i8*)** %6
store i32 (i8*)* %13, i32 (i8*)** %10
%14 = load i8*, i8** %9
store i8* %14, i8** %11
%15 = load i32, i32* @EINVAL
%16 = sub nsw i32 0, %15
store i32 %16, i32* %12
%17 = load i32 (i8*)*, i32 (i8*)** %6
%18 = icmp ne i32 (i8*)* %17, null
br i1 %18, label %20, label %19
19: ; preds = %4
br label %42
20: ; preds = %4
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %20
br label %42
24: ; preds = %20
%25 = load i32, i32* %8
%26 = load i8*, i8** %7
%27 = call i32 @INLINE_SYSCALL(i32 (i32 (i8*)*, i8*, i32, i8*)* bitcast (i32 (i32 (i8*)*, i8*, i32, i8*, ...)* @clone to i32 (i32 (i8*)*, i8*, i32, i8*)*), i32 2, i32 %25, i8* %26)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %42
31: ; preds = %24
%32 = load i32, i32* %12
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* %12
store i32 %35, i32* %5
br label %46
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i32 (i8*)*, i32 (i8*)** %10
%39 = load i8*, i8** %11
%40 = call i32 %38(i8* %39)
%41 = call i32 @_exit(i32 %40)
unreachable
42: ; preds = %30, %23, %19
%43 = load i32, i32* %12
%44 = sub nsw i32 0, %43
%45 = call i32 @__set_errno(i32 %44)
store i32 -1, i32* %5
br label %46
46: ; preds = %42, %34
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@EINVAL = global i32 0
define i32 @clone(i32 (i8*)* %0, i8* %1, i32 %2, i8* %3, ...) {
%5 = load i32, i32* @EINVAL
%6 = sub nsw i32 0, %5
%7 = icmp ne i32 (i8*)* %0, null
%8 = icmp ne i8* %1, null
%9 = and i1 %7, %8
br i1 %9, label %10, label %18
10: ; preds = %4
%11 = tail call i32 @INLINE_SYSCALL(i32 (i32 (i8*)*, i8*, i32, i8*)* bitcast (i32 (i32 (i8*)*, i8*, i32, i8*, ...)* @clone to i32 (i32 (i8*)*, i8*, i32, i8*)*), i32 2, i32 %2, i8* %1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = icmp eq i32 %11, 0
br i1 %14, label %15, label %22
15: ; preds = %13
%16 = tail call i32 %0(i8* %3)
%17 = tail call i32 @_exit(i32 %16)
unreachable
18: ; preds = %10, %4
%19 = phi i32 [ %11, %10 ], [ %6, %4 ]
%20 = sub nsw i32 0, %19
%21 = tail call i32 @__set_errno(i32 %20)
br label %22
22: ; preds = %13, %18
%23 = phi i32 [ -1, %18 ], [ %11, %13 ]
ret i32 %23
}