#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 ci20_dev {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int ci20_load_elf_fd(struct ci20_dev *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int /*<<< orphan*/);
int ci20_load_elf_path(struct ci20_dev *dev, const char *path,
                       uint32_t *entry) {
  int filedes, err;
  filedes = open(path, O_RDONLY);
  if (filedes == -1)
    return errno;
  err = ci20_load_elf_fd(dev, filedes, entry);
  close(filedes);
  return err;
}


x86 assembly (clang, -O0):
<ci20_load_elf_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1174 <ci20_load_elf_path+0x44>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <ci20_load_elf_path+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <ci20_load_elf_fd@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ci20_load_elf_path>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rax
mov    %rdi,%rbx
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     116f <ci20_load_elf_path+0x3f>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r14,%rdx
call   1040 <ci20_load_elf_fd@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
jmp    1178 <ci20_load_elf_path+0x48>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ci20_dev = type { i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @ci20_load_elf_path(%struct.ci20_dev* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.ci20_dev*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store %struct.ci20_dev* %0, %struct.ci20_dev** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%10 = load i8*, i8** %6
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* @errno
store i32 %16, i32* %4
br label %25
17: ; preds = %3
%18 = load %struct.ci20_dev*, %struct.ci20_dev** %5
%19 = load i32, i32* %8
%20 = load i32*, i32** %7
%21 = call i32 @ci20_load_elf_fd(%struct.ci20_dev* %18, i32 %19, i32* %20)
store i32 %21, i32* %9
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %9
store i32 %24, i32* %4
br label %25
25: ; preds = %17, %15
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
%struct.ci20_dev = type { i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @ci20_load_elf_path(%struct.ci20_dev* %0, i8* nocapture %1, i32* %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %1, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32, i32* @errno
br label %12
9: ; preds = %3
%10 = tail call i32 @ci20_load_elf_fd(%struct.ci20_dev* %0, i32 %5, i32* %2)
%11 = tail call i32 @close(i32 %5)
br label %12
12: ; preds = %9, %7
%13 = phi i32 [ %8, %7 ], [ %10, %9 ]
ret i32 %13
}