#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 MAP_ANON;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
size_t codesize(size_t);
char *inject_funcall(char *, void *);
char *inject_param(char *, size_t, void *);
char *mmap(int /*<<< orphan*/, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int /*<<< orphan*/ restore_stack(char *, size_t);
char *setup_stack(char *, size_t);
void *stub1();
void *apply(void *func, void **args, size_t argc) {
  size_t len = codesize(argc);
  char *buf;
  void *result;
  char *codemem = mmap(0, len, PROT_READ | PROT_WRITE | PROT_EXEC,
                       MAP_PRIVATE | MAP_ANON, -1, 0);
  if (!codemem) {
    return NULL;
  }
  buf = setup_stack(codemem, argc);
  for (size_t pos = 0; pos < argc; ++pos) {
    buf = inject_param(buf, pos, args[pos]);
  }
  buf = inject_funcall(buf, func);
  restore_stack(buf, argc);
  result = ((void *(*)())codemem)();
  munmap(codemem, len);
  return result;
}


x86 assembly (clang, -O0):
<apply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <codesize@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
mov    0x2e2b(%rip),%rax
or     (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
mov    0x2e08(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1060 <mmap@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11ef <apply+0x7f>
movq   $0x0,-0x8(%rbp)
jmp    1280 <apply+0x110>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <setup_stack@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1244 <apply+0xd4>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
call   1050 <inject_param@plt>
mov    %rax,-0x30(%rbp)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1208 <apply+0x98>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <inject_funcall@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <restore_stack@plt>
mov    $0x0,%al
call   *-0x40(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <munmap@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apply>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rdx,%rdi
call   1090 <codesize@plt>
mov    %rax,%r14
mov    0x2e62(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e38(%rip),%rax
or     (%rax),%edx
mov    0x2e0f(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
test   %rax,%rax
je     1236 <apply+0xc6>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <setup_stack@plt>
test   %rbx,%rbx
je     1208 <apply+0x98>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    (%r12,%rbp,8),%rdx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <inject_param@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11f0 <apply+0x80>
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <inject_funcall@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <restore_stack@plt>
xor    %eax,%eax
call   *%r13
mov    %rax,%rbx
mov    %r13,%rdi
mov    %r14,%rsi
call   1080 <munmap@plt>
mov    %rbx,%rax
jmp    1238 <apply+0xc8>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
define i8* @apply(i8* %0, i8** %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
%13 = load i64, i64* %7
%14 = call i64 @codesize(i64 %13)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @PROT_EXEC
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_PRIVATE
%22 = load i32, i32* @MAP_ANON
%23 = or i32 %21, %22
%24 = call i8* @mmap(i32 0, i64 %15, i32 %20, i32 %23, i32 -1, i32 0)
store i8* %24, i8** %11
%25 = load i8*, i8** %11
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %3
store i8* null, i8** %4
br label %61
28: ; preds = %3
%29 = load i8*, i8** %11
%30 = load i64, i64* %7
%31 = call i8* @setup_stack(i8* %29, i64 %30)
store i8* %31, i8** %9
store i64 0, i64* %12
br label %32
32: ; preds = %44, %28
%33 = load i64, i64* %12
%34 = load i64, i64* %7
%35 = icmp ult i64 %33, %34
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = load i8*, i8** %9
%38 = load i64, i64* %12
%39 = load i8**, i8*** %6
%40 = load i64, i64* %12
%41 = getelementptr inbounds i8*, i8** %39, i64 %40
%42 = load i8*, i8** %41
%43 = call i8* @inject_param(i8* %37, i64 %38, i8* %42)
store i8* %43, i8** %9
br label %44
44: ; preds = %36
%45 = load i64, i64* %12
%46 = add i64 %45, 1
store i64 %46, i64* %12
br label %32
47: ; preds = %32
%48 = load i8*, i8** %9
%49 = load i8*, i8** %5
%50 = call i8* @inject_funcall(i8* %48, i8* %49)
store i8* %50, i8** %9
%51 = load i8*, i8** %9
%52 = load i64, i64* %7
%53 = call i32 @restore_stack(i8* %51, i64 %52)
%54 = load i8*, i8** %11
%55 = bitcast i8* %54 to i8* (...)*
%56 = call i8* (...) %55()
store i8* %56, i8** %10
%57 = load i8*, i8** %11
%58 = load i64, i64* %8
%59 = call i32 @munmap(i8* %57, i64 %58)
%60 = load i8*, i8** %10
store i8* %60, i8** %4
br label %61
61: ; preds = %47, %27
%62 = load i8*, i8** %4
ret i8* %62
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
define i8* @apply(i8* %0, i8** nocapture %1, i64 %2) {
%4 = tail call i64 @codesize(i64 %2)
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @PROT_EXEC
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_PRIVATE
%11 = load i32, i32* @MAP_ANON
%12 = or i32 %11, %10
%13 = tail call i8* @mmap(i32 0, i64 %4, i32 %9, i32 %12, i32 -1, i32 0)
%14 = icmp eq i8* %13, null
br i1 %14, label %33, label %15
15: ; preds = %3
%16 = tail call i8* @setup_stack(i8* %13, i64 %2)
%17 = icmp eq i64 %2, 0
br i1 %17, label %18, label %25
18: ; preds = %25, %15
%19 = phi i8* [ %16, %15 ], [ %30, %25 ]
%20 = tail call i8* @inject_funcall(i8* %19, i8* %0)
%21 = tail call i32 @restore_stack(i8* %20, i64 %2)
%22 = bitcast i8* %13 to i8* (...)*
%23 = tail call i8* (...) %22()
%24 = tail call i32 @munmap(i8* %13, i64 %4)
br label %33
25: ; preds = %15, %25
%26 = phi i64 [ %31, %25 ], [ 0, %15 ]
%27 = phi i8* [ %30, %25 ], [ %16, %15 ]
%28 = getelementptr inbounds i8*, i8** %1, i64 %26
%29 = load i8*, i8** %28
%30 = tail call i8* @inject_param(i8* %27, i64 %26, i8* %29)
%31 = add nuw i64 %26, 1
%32 = icmp eq i64 %31, %2
br i1 %32, label %18, label %25
33: ; preds = %3, %18
%34 = phi i8* [ %23, %18 ], [ null, %3 ]
ret i8* %34
}