#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 uint32_t;
typedef int /*<<< orphan*/ len;

/* Variables and functions */
int /*<<< orphan*/ MFD_CLOEXEC;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ SYS_memfd_create;
int check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ copy_fd_len(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ readn(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ syscall(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int read_binary() {
  int fd = check(syscall(SYS_memfd_create, "", MFD_CLOEXEC), "memfd_create");
  puts("please send me a 64 bit binary to run. Format: len as uint32 LE || "
       "data");
  uint32_t len = 0;
  readn(STDIN_FILENO, &len, sizeof(len));
  printf("reading 0x%x bytes\n", len);
  copy_fd_len(STDIN_FILENO, fd, len);
  return fd;
}


x86 assembly (clang, -O0):
<read_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
lea    0xee7(%rip),%rsi
call   1070 <syscall@plt>
mov    %eax,%edi
lea    0xe71(%rip),%rsi
call   1080 <check@plt>
mov    %eax,-0x4(%rbp)
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x8(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <readn@plt>
mov    -0x8(%rbp),%esi
lea    0xe8a(%rip),%rdi
call   1050 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1040 <copy_fd_len@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_binary>:
push   %r14
push   %rbx
push   %rax
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
lea    0xeeb(%rip),%rsi
call   1070 <syscall@plt>
lea    0xe77(%rip),%rsi
mov    %eax,%edi
call   1080 <check@plt>
mov    %eax,%ebx
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,0x4(%rsp)
mov    0x2e23(%rip),%r14
mov    (%r14),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1060 <readn@plt>
mov    0x4(%rsp),%esi
lea    0xe8b(%rip),%rdi
call   1050 <printf@plt>
mov    (%r14),%edi
mov    0x4(%rsp),%edx
mov    %ebx,%esi
call   1040 <copy_fd_len@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SYS_memfd_create = global i32 0
@.str = [1 x i8] zeroinitializer
@MFD_CLOEXEC = global i32 0
@.str.1 = [13 x i8] c"memfd_create\00"
@.str.2 = [72 x i8] c"please send me a 64 bit binary to run. Format: len as uint32 LE || data\00"
@STDIN_FILENO = global i32 0
@.str.3 = [20 x i8] c"reading 0x%x bytes\0A\00"
define i32 @read_binary() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @SYS_memfd_create
%4 = load i32, i32* @MFD_CLOEXEC
%5 = call i32 @syscall(i32 %3, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @check(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 %6, i32* %1
%7 = call i32 @puts(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @readn(i32 %8, i32* %2, i32 4)
%10 = load i32, i32* %2
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* %1
%14 = load i32, i32* %2
%15 = call i32 @copy_fd_len(i32 %12, i32 %13, i32 %14)
%16 = load i32, i32* %1
ret i32 %16
}

LLVM IR (-O2):
@SYS_memfd_create = global i32 0
@.str = [1 x i8] zeroinitializer
@MFD_CLOEXEC = global i32 0
@.str.1 = [13 x i8] c"memfd_create\00"
@.str.2 = [72 x i8] c"please send me a 64 bit binary to run. Format: len as uint32 LE || data\00"
@STDIN_FILENO = global i32 0
@.str.3 = [20 x i8] c"reading 0x%x bytes\0A\00"
define i32 @read_binary() {
%1 = alloca i32
%2 = load i32, i32* @SYS_memfd_create
%3 = load i32, i32* @MFD_CLOEXEC
%4 = tail call i32 @syscall(i32 %2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @check(i32 %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
%7 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @readn(i32 %8, i32* %1, i32 4)
%10 = load i32, i32* %1
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @STDIN_FILENO
%13 = load i32, i32* %1
%14 = call i32 @copy_fd_len(i32 %12, i32 %5, i32 %13)
ret i32 %5
}