#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*/ STDIN_FILENO;
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, char *, int);
int read_binary() {
  int fd = check(syscall(319, "", 0x0001U), "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    $0x13f,%edi
lea    0xef4(%rip),%rsi
mov    $0x1,%edx
call   1070 <syscall@plt>
mov    %eax,%edi
lea    0xe79(%rip),%rsi
call   1080 <check@plt>
mov    %eax,-0x4(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x8(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <readn@plt>
mov    -0x8(%rbp),%esi
lea    0xe92(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e11(%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
lea    0xefd(%rip),%rsi
mov    $0x13f,%edi
mov    $0x1,%edx
call   1070 <syscall@plt>
lea    0xe7f(%rip),%rsi
mov    %eax,%edi
call   1080 <check@plt>
mov    %eax,%ebx
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,0x4(%rsp)
mov    0x2e3b(%rip),%r14
mov    (%r14),%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1060 <readn@plt>
mov    0x4(%rsp),%esi
lea    0xe93(%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):
@.str = [1 x i8] zeroinitializer
@.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 = call i32 @syscall(i32 319, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 1)
%4 = call i32 @check(i32 %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 %4, i32* %1
%5 = call i32 @puts(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
%6 = load i32, i32* @STDIN_FILENO
%7 = call i32 @readn(i32 %6, i32* %2, i32 4)
%8 = load i32, i32* %2
%9 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @STDIN_FILENO
%11 = load i32, i32* %1
%12 = load i32, i32* %2
%13 = call i32 @copy_fd_len(i32 %10, i32 %11, i32 %12)
%14 = load i32, i32* %1
ret i32 %14
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.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 = tail call i32 @syscall(i32 319, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 1)
%3 = tail call i32 @check(i32 %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
%5 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%6 = load i32, i32* @STDIN_FILENO
%7 = call i32 @readn(i32 %6, i32* %1, i32 4)
%8 = load i32, i32* %1
%9 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @STDIN_FILENO
%11 = load i32, i32* %1
%12 = call i32 @copy_fd_len(i32 %10, i32 %3, i32 %11)
ret i32 %3
}