#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*/ ffi_type;
typedef scalar_t__ ffi_status;
typedef int /*<<< orphan*/ ffi_cif;

/* Variables and functions */
scalar_t__ FFI_BAD_ABI;
scalar_t__ FFI_BAD_TYPEDEF;
int /*<<< orphan*/ FFI_DEFAULT_ABI;
int /*<<< orphan*/ FFI_FN(void *);
scalar_t__ FFI_OK;
int /*<<< orphan*/ ffi_call(int /*<<< orphan*/ *, int /*<<< orphan*/, void **,
                            void **);
scalar_t__ ffi_prep_cif(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                        int /*<<< orphan*/ *, int /*<<< orphan*/ **);
int /*<<< orphan*/ *ffi_type_ctor(char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void *ffi_callX(int argc, const char **arg_string, ffi_type rtype, void *x_ptr,
                void **values) {
  ffi_status status;
  ffi_cif cif;
  ffi_type *args_types[argc];
  void *ret;
  for (int i = 0; i < argc; ++i) {
    if (ffi_type_ctor(arg_string[i]))
      args_types[i] = ffi_type_ctor(arg_string[i]);
  }
  status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argc, &rtype, args_types);
  if (status == FFI_BAD_TYPEDEF) {
    fprintf(stderr, "ffi_prep_cif returned FFI_BAD_TYPEDEF: %d\n", status);
    return NULL;
  } else if (status == FFI_BAD_ABI) {
    fprintf(stderr, "ffi_prep_cif returned FFI_BAD_ABI: %d\n", status);
    return NULL;
  } else if (status == FFI_OK) {
    fprintf(stderr, "ffi_prep_cif returned FFI_OK: %d\n", status);
  } else {
    fprintf(stderr, "ffi_prep_cif returned an error: %d\n", status);
    return NULL;
  }
  fprintf(stdout, "before the ffi call\n");
  ffi_call(&cif, FFI_FN(x_ptr), &ret, values);
  fprintf(stdout, "after the ffi call\n");
  return ret;
}


x86 assembly (clang, -O0):
<ffi_callX>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f2 <ffi_callX+0xa2>
mov    -0x18(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <ffi_type_ctor@plt>
cmp    $0x0,%rax
je     11df <ffi_callX+0x8f>
mov    -0x18(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <ffi_type_ctor@plt>
mov    %rax,%rdx
mov    -0x68(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    11e4 <ffi_callX+0x94>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1198 <ffi_callX+0x48>
mov    -0x68(%rbp),%r8
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
lea    -0x3c(%rbp),%rdi
lea    -0x1c(%rbp),%rcx
call   1030 <ffi_prep_cif@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    1256 <ffi_callX+0x106>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x60(%rbp)
jmp    135f <ffi_callX+0x20f>
mov    -0x38(%rbp),%rax
mov    0x2d5f(%rip),%rcx
cmp    (%rcx),%rax
jne    1299 <ffi_callX+0x149>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x60(%rbp)
jmp    135f <ffi_callX+0x20f>
mov    -0x38(%rbp),%rax
mov    0x2d44(%rip),%rcx
cmp    (%rcx),%rax
jne    12cd <ffi_callX+0x17d>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12fc <ffi_callX+0x1ac>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x60(%rbp)
jmp    135f <ffi_callX+0x20f>
jmp    1301 <ffi_callX+0x1b1>
jmp    1306 <ffi_callX+0x1b6>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <FFI_FN@plt>
mov    %eax,%esi
mov    -0x30(%rbp),%rcx
lea    -0x3c(%rbp),%rdi
lea    -0x58(%rbp),%rdx
call   1070 <ffi_call@plt>
mov    0x2c90(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x60(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffi_callX>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,-0x40(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %edi,%r12d
mov    %rsp,-0x48(%rbp)
mov    %edx,-0x30(%rbp)
mov    %edi,%r15d
mov    %rsp,%r13
lea    0xf(,%r15,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
test   %edi,%edi
jle    11c7 <ffi_callX+0x77>
mov    %rsi,%rbx
xor    %r14d,%r14d
jmp    11a9 <ffi_callX+0x59>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    %r14,%r15
je     11c7 <ffi_callX+0x77>
mov    (%rbx,%r14,8),%rdi
call   1040 <ffi_type_ctor@plt>
test   %rax,%rax
je     11a0 <ffi_callX+0x50>
mov    (%rbx,%r14,8),%rdi
call   1040 <ffi_type_ctor@plt>
mov    %rax,0x0(%r13,%r14,8)
jmp    11a0 <ffi_callX+0x50>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    -0x2c(%rbp),%rdi
lea    -0x30(%rbp),%rcx
mov    %r12d,%edx
mov    %r13,%r8
call   1030 <ffi_prep_cif@plt>
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <ffi_callX+0xb4>
mov    0x2de2(%rip),%rcx
mov    (%rcx),%edi
lea    0xe01(%rip),%rsi
jmp    1298 <ffi_callX+0x148>
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
jne    1222 <ffi_callX+0xd2>
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%edi
lea    0xe0b(%rip),%rsi
jmp    1298 <ffi_callX+0x148>
mov    0x2dbf(%rip),%rcx
mov    0x2da8(%rip),%rdx
mov    (%rdx),%edi
cmp    (%rcx),%rax
jne    1291 <ffi_callX+0x141>
lea    0xe14(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d81(%rip),%rbx
mov    (%rbx),%edi
lea    0xe40(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1060 <FFI_FN@plt>
lea    -0x2c(%rbp),%rdi
lea    -0x50(%rbp),%rdx
mov    %eax,%esi
mov    -0x40(%rbp),%rcx
call   1070 <ffi_call@plt>
mov    (%rbx),%edi
lea    0xe29(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    -0x50(%rbp),%rbx
jmp    12a4 <ffi_callX+0x154>
lea    0xddc(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FFI_DEFAULT_ABI = global i32 0
@FFI_BAD_TYPEDEF = global i64 0
@stderr = global i32 0
@.str = [43 x i8] c"ffi_prep_cif returned FFI_BAD_TYPEDEF: %d\0A\00"
@FFI_BAD_ABI = global i64 0
@.str.1 = [39 x i8] c"ffi_prep_cif returned FFI_BAD_ABI: %d\0A\00"
@FFI_OK = global i64 0
@.str.2 = [34 x i8] c"ffi_prep_cif returned FFI_OK: %d\0A\00"
@.str.3 = [36 x i8] c"ffi_prep_cif returned an error: %d\0A\00"
@stdout = global i32 0
@.str.4 = [21 x i8] c"before the ffi call\0A\00"
@.str.5 = [20 x i8] c"after the ffi call\0A\00"
define i8* @ffi_callX(i32 %0, i8** %1, i32 %2, i8* %3, i8** %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8**
%12 = alloca i64
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %7
store i8** %1, i8*** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i8** %4, i8*** %11
%19 = load i32, i32* %7
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %14
%22 = alloca i32*, i64 %20
store i64 %20, i64* %15
store i32 0, i32* %17
br label %23
23: ; preds = %46, %5
%24 = load i32, i32* %17
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %49
27: ; preds = %23
%28 = load i8**, i8*** %8
%29 = load i32, i32* %17
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
%33 = call i32* @ffi_type_ctor(i8* %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %45
35: ; preds = %27
%36 = load i8**, i8*** %8
%37 = load i32, i32* %17
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i32* @ffi_type_ctor(i8* %40)
%42 = load i32, i32* %17
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32*, i32** %22, i64 %43
store i32* %41, i32** %44
br label %45
45: ; preds = %35, %27
br label %46
46: ; preds = %45
%47 = load i32, i32* %17
%48 = add nsw i32 %47, 1
store i32 %48, i32* %17
br label %23
49: ; preds = %23
%50 = load i32, i32* @FFI_DEFAULT_ABI
%51 = load i32, i32* %7
%52 = call i64 @ffi_prep_cif(i32* %13, i32 %50, i32 %51, i32* %9, i32** %22)
store i64 %52, i64* %12
%53 = load i64, i64* %12
%54 = load i64, i64* @FFI_BAD_TYPEDEF
%55 = icmp eq i64 %53, %54
br i1 %55, label %56, label %60
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = load i64, i64* %12
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i64 %58)
store i8* null, i8** %6
store i32 1, i32* %18
br label %92
60: ; preds = %49
%61 = load i64, i64* %12
%62 = load i64, i64* @FFI_BAD_ABI
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = load i32, i32* @stderr
%66 = load i64, i64* %12
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %66)
store i8* null, i8** %6
store i32 1, i32* %18
br label %92
68: ; preds = %60
%69 = load i64, i64* %12
%70 = load i64, i64* @FFI_OK
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %76
72: ; preds = %68
%73 = load i32, i32* @stderr
%74 = load i64, i64* %12
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i64 %74)
br label %80
76: ; preds = %68
%77 = load i32, i32* @stderr
%78 = load i64, i64* %12
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i64 %78)
store i8* null, i8** %6
store i32 1, i32* %18
br label %92
80: ; preds = %72
br label %81
81: ; preds = %80
br label %82
82: ; preds = %81
%83 = load i32, i32* @stdout
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%85 = load i8*, i8** %10
%86 = call i32 @FFI_FN(i8* %85)
%87 = load i8**, i8*** %11
%88 = call i32 @ffi_call(i32* %13, i32 %86, i8** %16, i8** %87)
%89 = load i32, i32* @stdout
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%91 = load i8*, i8** %16
store i8* %91, i8** %6
store i32 1, i32* %18
br label %92
92: ; preds = %82, %76, %64, %56
%93 = load i8*, i8** %14
%94 = load i8*, i8** %6
ret i8* %94
}

LLVM IR (-O2):
@FFI_DEFAULT_ABI = global i32 0
@FFI_BAD_TYPEDEF = global i64 0
@stderr = global i32 0
@.str = [43 x i8] c"ffi_prep_cif returned FFI_BAD_TYPEDEF: %d\0A\00"
@FFI_BAD_ABI = global i64 0
@.str.1 = [39 x i8] c"ffi_prep_cif returned FFI_BAD_ABI: %d\0A\00"
@FFI_OK = global i64 0
@.str.2 = [34 x i8] c"ffi_prep_cif returned FFI_OK: %d\0A\00"
@.str.3 = [36 x i8] c"ffi_prep_cif returned an error: %d\0A\00"
@stdout = global i32 0
@.str.4 = [21 x i8] c"before the ffi call\0A\00"
@.str.5 = [20 x i8] c"after the ffi call\0A\00"
define i8* @ffi_callX(i32 %0, i8** nocapture %1, i32 %2, i8* %3, i8** %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %2, i32* %6
%9 = bitcast i32* %7 to i8*
%10 = zext i32 %0 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i32*, i64 %10
%13 = bitcast i8** %8 to i8*
%14 = icmp sgt i32 %0, 0
br i1 %14, label %20, label %15
15: ; preds = %30, %5
%16 = load i32, i32* @FFI_DEFAULT_ABI
%17 = call i64 @ffi_prep_cif(i32* %7, i32 %16, i32 %0, i32* %6, i32** %12)
%18 = load i64, i64* @FFI_BAD_TYPEDEF
%19 = icmp eq i64 %17, %18
br i1 %19, label %33, label %36
20: ; preds = %5, %30
%21 = phi i64 [ %31, %30 ], [ 0, %5 ]
%22 = getelementptr inbounds i8*, i8** %1, i64 %21
%23 = load i8*, i8** %22
%24 = tail call i32* @ffi_type_ctor(i8* %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = load i8*, i8** %22
%28 = tail call i32* @ffi_type_ctor(i8* %27)
%29 = getelementptr inbounds i32*, i32** %12, i64 %21
store i32* %28, i32** %29
br label %30
30: ; preds = %20, %26
%31 = add nuw nsw i64 %21, 1
%32 = icmp eq i64 %31, %10
br i1 %32, label %15, label %20
33: ; preds = %15
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i64 %17)
br label %57
36: ; preds = %15
%37 = load i64, i64* @FFI_BAD_ABI
%38 = icmp eq i64 %17, %37
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %17)
br label %57
42: ; preds = %36
%43 = load i64, i64* @FFI_OK
%44 = icmp eq i64 %17, %43
%45 = load i32, i32* @stderr
br i1 %44, label %46, label %55
46: ; preds = %42
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i64 %17)
%48 = load i32, i32* @stdout
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @FFI_FN(i8* %3)
%51 = call i32 @ffi_call(i32* %7, i32 %50, i8** %8, i8** %4)
%52 = load i32, i32* @stdout
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%54 = load i8*, i8** %8
br label %57
55: ; preds = %42
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i64 %17)
br label %57
57: ; preds = %46, %55, %39, %33
%58 = phi i8* [ null, %33 ], [ null, %39 ], [ %54, %46 ], [ null, %55 ]
ret i8* %58
}