#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_EXEC;
int PROT_READ;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int munmap(void *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int stub1(int, int);
int main(void) {
  int fd;
  int ret;
  void *mem;
  struct stat st;
  fd = open("x.bin-32bit", O_RDONLY);
  if (fd == -1) {
    perror("x.bin-32bit");
    return 1;
  }
  if (fstat(fd, &st) == -1) {
    perror("fstat");
    return 1;
  }
  mem = mmap(NULL, st.st_size, PROT_EXEC | PROT_READ, MAP_PRIVATE, fd, 0);
  if (mem == MAP_FAILED) {
    perror("mmap");
    return 1;
  }
  ret = ((int (*)(int, int))mem)(5, 11);
  printf("Result of function is: %d\n", ret);
  if (munmap(mem, st.st_size) == -1) {
    perror("munmap");
    return 1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b9 <main+0x49>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x12f>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11e6 <main+0x76>
lea    0xe37(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x12f>
mov    -0x20(%rbp),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1244 <main+0xd4>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x12f>
mov    $0x5,%edi
mov    $0xb,%esi
call   *-0x18(%rbp)
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1290 <main+0x120>
lea    0xdb3(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <main+0x12f>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     121d <main+0xad>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     1226 <main+0xb6>
mov    0x8(%rsp),%r14d
mov    0x2e23(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2ddd(%rip),%rax
cmp    (%rax),%rbp
je     122f <main+0xbf>
mov    $0x5,%edi
mov    $0xb,%esi
call   *%rbp
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
je     1238 <main+0xc8>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1249 <main+0xd9>
lea    0xddc(%rip),%rdi
jmp    123f <main+0xcf>
lea    0xddf(%rip),%rdi
jmp    123f <main+0xcf>
lea    0xddc(%rip),%rdi
jmp    123f <main+0xcf>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"x.bin-32bit\00"
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"fstat\00"
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [27 x i8] c"Result of function is: %d\0A\00"
@.str.4 = [7 x i8] c"munmap\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %48
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i32 @fstat(i32 %13, %struct.stat* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %48
18: ; preds = %12
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @PROT_EXEC
%22 = load i32, i32* @PROT_READ
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_PRIVATE
%25 = load i32, i32* %2
%26 = call i8* @mmap(i32* null, i32 %20, i32 %23, i32 %24, i32 %25, i32 0)
store i8* %26, i8** %4
%27 = load i8*, i8** %4
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %48
32: ; preds = %18
%33 = load i8*, i8** %4
%34 = bitcast i8* %33 to i32 (i32, i32)*
%35 = call i32 %34(i32 5, i32 11)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %4
%39 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @munmap(i8* %38, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %32
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %48
45: ; preds = %32
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
store i32 0, i32* %1
br label %48
48: ; preds = %45, %43, %30, %16, %10
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"x.bin-32bit\00"
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"fstat\00"
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [27 x i8] c"Result of function is: %d\0A\00"
@.str.4 = [7 x i8] c"munmap\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %35
8: ; preds = %0
%9 = call i32 @fstat(i32 %4, %struct.stat* %1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %35
13: ; preds = %8
%14 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @PROT_EXEC
%17 = load i32, i32* @PROT_READ
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_PRIVATE
%20 = tail call i8* @mmap(i32* null, i32 %15, i32 %18, i32 %19, i32 %4, i32 0)
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %35
25: ; preds = %13
%26 = bitcast i8* %20 to i32 (i32, i32)*
%27 = tail call i32 %26(i32 5, i32 11)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = tail call i32 @munmap(i8* %20, i32 %15)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %35
33: ; preds = %25
%34 = tail call i32 @close(i32 %4)
br label %35
35: ; preds = %33, %31, %23, %11, %6
%36 = phi i32 [ 1, %6 ], [ 1, %11 ], [ 1, %23 ], [ 1, %31 ], [ 0, %33 ]
ret i32 %36
}