#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ MFD_CLOEXEC;
void *check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ memfd_create(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
size_t write(int, char *, size_t);
int read_to_memfd(int fd, unsigned sz) {
  char buf[4096] = {0};
  int memfd = check(memfd_create("x", MFD_CLOEXEC), "memfd_create");
  while (sz) {
    size_t read_sz = check(read(fd, buf, (sizeof(buf) < sz ? sizeof(buf) : sz)),
                           "read(ELF)");
    if (read_sz == 0) {
      errx(1, "short read");
    }
    if (write(memfd, buf, read_sz) != read_sz) {
      errx(1, "short write");
    }
    sz -= read_sz;
  }
  return memfd;
}


x86 assembly (clang, -O0):
<read_to_memfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1030 <memfd_create@plt>
mov    %eax,%edi
lea    0xe60(%rip),%rsi
call   1080 <check@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1297 <read_to_memfd+0x137>
mov    -0x4(%rbp),%eax
mov    %eax,-0x102c(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1028(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
mov    $0x1000,%eax
cmp    %rcx,%rax
jae    11f2 <read_to_memfd+0x92>
mov    $0x1000,%eax
mov    %rax,-0x1038(%rbp)
jmp    11fc <read_to_memfd+0x9c>
mov    -0x8(%rbp),%eax
mov    %rax,-0x1038(%rbp)
mov    -0x1028(%rbp),%rsi
mov    -0x102c(%rbp),%edi
mov    -0x1038(%rbp),%rax
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,%edi
lea    0xdef(%rip),%rsi
call   1080 <check@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jne    124b <read_to_memfd+0xeb>
mov    $0x1,%edi
lea    0xdd3(%rip),%rsi
call   1050 <errx@plt>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1020(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x1020(%rbp),%rax
je     1282 <read_to_memfd+0x122>
mov    $0x1,%edi
lea    0xda7(%rip),%rsi
call   1050 <errx@plt>
mov    -0x1020(%rbp),%rcx
mov    -0x8(%rbp),%eax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
jmp    11ad <read_to_memfd+0x4d>
mov    -0x1014(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_to_memfd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %rsp,%rdi
mov    $0x1000,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1030 <memfd_create@plt>
lea    0xe61(%rip),%rsi
mov    %eax,%edi
call   1080 <check@plt>
mov    %rax,%r15
test   %ebx,%ebx
je     1227 <read_to_memfd+0xc7>
mov    %rsp,%rbp
lea    0xe56(%rip),%r12
jmp    11c5 <read_to_memfd+0x65>
nopl   0x0(%rax,%rax,1)
sub    %r13d,%ebx
je     1227 <read_to_memfd+0xc7>
cmp    $0x1000,%ebx
mov    $0x1000,%edx
cmovb  %ebx,%edx
mov    %r14d,%edi
mov    %rbp,%rsi
call   1070 <read@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1080 <check@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1201 <read_to_memfd+0xa1>
mov    $0x1,%edi
lea    0xe1d(%rip),%rsi
call   1050 <errx@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1040 <write@plt>
cmp    %r13,%rax
je     11c0 <read_to_memfd+0x60>
mov    $0x1,%edi
lea    0xe04(%rip),%rsi
call   1050 <errx@plt>
jmp    11c0 <read_to_memfd+0x60>
mov    %r15d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"x\00"
@MFD_CLOEXEC = global i32 0
@.str.1 = [13 x i8] c"memfd_create\00"
@.str.2 = [10 x i8] c"read(ELF)\00"
@.str.3 = [11 x i8] c"short read\00"
@.str.4 = [12 x i8] c"short write\00"
define i32 @read_to_memfd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = bitcast [4096 x i8]* %5 to i8*
%9 = load i32, i32* @MFD_CLOEXEC
%10 = call i32 @memfd_create(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i8* @check(i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%12 = ptrtoint i8* %11 to i32
store i32 %12, i32* %6
br label %13
13: ; preds = %45, %2
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %51
16: ; preds = %13
%17 = load i32, i32* %3
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%19 = load i32, i32* %4
%20 = zext i32 %19 to i64
%21 = icmp ult i64 4096, %20
br i1 %21, label %22, label %23
22: ; preds = %16
br label %26
23: ; preds = %16
%24 = load i32, i32* %4
%25 = zext i32 %24 to i64
br label %26
26: ; preds = %23, %22
%27 = phi i64 [ 4096, %22 ], [ %25, %23 ]
%28 = trunc i64 %27 to i32
%29 = call i32 @read(i32 %17, i8* %18, i32 %28)
%30 = call i8* @check(i32 %29, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%31 = ptrtoint i8* %30 to i64
store i64 %31, i64* %7
%32 = load i64, i64* %7
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 @errx(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %26
%37 = load i32, i32* %6
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%39 = load i64, i64* %7
%40 = call i64 @write(i32 %37, i8* %38, i64 %39)
%41 = load i64, i64* %7
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 @errx(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
%46 = load i64, i64* %7
%47 = load i32, i32* %4
%48 = zext i32 %47 to i64
%49 = sub i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* %4
br label %13
51: ; preds = %13
%52 = load i32, i32* %6
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"x\00"
@MFD_CLOEXEC = global i32 0
@.str.1 = [13 x i8] c"memfd_create\00"
@.str.2 = [10 x i8] c"read(ELF)\00"
@.str.3 = [11 x i8] c"short read\00"
@.str.4 = [12 x i8] c"short write\00"
define i32 @read_to_memfd(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @MFD_CLOEXEC
%6 = tail call i32 @memfd_create(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i8* @check(i32 %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%8 = ptrtoint i8* %7 to i64
%9 = trunc i64 %8 to i32
%10 = icmp eq i32 %1, 0
br i1 %10, label %30, label %11
11: ; preds = %2, %26
%12 = phi i32 [ %28, %26 ], [ %1, %2 ]
%13 = icmp ult i32 %12, 4096
%14 = select i1 %13, i32 %12, i32 4096
%15 = call i32 @read(i32 %0, i8* %4, i32 %14)
%16 = tail call i8* @check(i32 %15, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%17 = ptrtoint i8* %16 to i64
%18 = icmp eq i8* %16, null
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = call i64 @write(i32 %9, i8* %4, i64 %17)
%23 = icmp eq i64 %22, %17
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = trunc i64 %17 to i32
%28 = sub i32 %12, %27
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %11
30: ; preds = %26, %2
ret i32 %9
}