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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int ci20_load_elf(struct ci20_dev *, void *, int /*<<< orphan*/ *);
int errno;
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int ci20_load_elf_fd(struct ci20_dev *dev, int filedes, uint32_t *entry) {
  struct stat st;
  void *base;
  int err;
  if (fstat(filedes, &st))
    return errno;
  base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, filedes, 0);
  if (base == MAP_FAILED)
    return errno;
  err = ci20_load_elf(dev, base, entry);
  munmap(base, st.st_size);
  return err;
}


x86 assembly (clang, -O0):
<ci20_load_elf_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
je     117a <ci20_load_elf_fd+0x3a>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <ci20_load_elf_fd+0xae>
mov    -0x28(%rbp),%esi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
mov    0x2e43(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <ci20_load_elf_fd+0x88>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <ci20_load_elf_fd+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <ci20_load_elf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1050 <munmap@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ci20_load_elf_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    %ebp,%edi
call   1060 <fstat@plt>
test   %rax,%rax
je     1169 <ci20_load_elf_fd+0x29>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
jmp    11bb <ci20_load_elf_fd+0x7b>
mov    (%rsp),%r15d
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
mov    0x2e53(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2e2f(%rip),%rax
cmp    (%rax),%rbp
je     115e <ci20_load_elf_fd+0x1e>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1030 <ci20_load_elf@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %r15d,%esi
call   1050 <munmap@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ci20_dev = type { i32 }
%struct.stat = type { i32 }
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @ci20_load_elf_fd(%struct.ci20_dev* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.ci20_dev*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i32
store %struct.ci20_dev* %0, %struct.ci20_dev** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%11 = load i32, i32* %6
%12 = call i64 @fstat(i32 %11, %struct.stat* %8)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* @errno
store i32 %15, i32* %4
br label %38
16: ; preds = %3
%17 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_PRIVATE
%21 = load i32, i32* %6
%22 = call i8* @mmap(i32* null, i32 %18, i32 %19, i32 %20, i32 %21, i32 0)
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = load i32, i32* @errno
store i32 %27, i32* %4
br label %38
28: ; preds = %16
%29 = load %struct.ci20_dev*, %struct.ci20_dev** %5
%30 = load i8*, i8** %9
%31 = load i32*, i32** %7
%32 = call i32 @ci20_load_elf(%struct.ci20_dev* %29, i8* %30, i32* %31)
store i32 %32, i32* %10
%33 = load i8*, i8** %9
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @munmap(i8* %33, i32 %35)
%37 = load i32, i32* %10
store i32 %37, i32* %4
br label %38
38: ; preds = %28, %26, %14
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.ci20_dev = type { i32 }
%struct.stat = type { i32 }
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @ci20_load_elf_fd(%struct.ci20_dev* %0, i32 %1, i32* %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @fstat(i32 %1, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = load i32, i32* @errno
br label %23
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_PRIVATE
%15 = tail call i8* @mmap(i32* null, i32 %12, i32 %13, i32 %14, i32 %1, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = load i32, i32* @errno
br label %23
20: ; preds = %10
%21 = tail call i32 @ci20_load_elf(%struct.ci20_dev* %0, i8* %15, i32* %2)
%22 = tail call i32 @munmap(i8* %15, i32 %12)
br label %23
23: ; preds = %20, %18, %8
%24 = phi i32 [ %9, %8 ], [ %19, %18 ], [ %21, %20 ]
ret i32 %24
}