#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ hdr;
struct TYPE_4__ {
  int /*<<< orphan*/ e_ident;
};
typedef TYPE_1__ Elf32_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ ELFMAG;
int /*<<< orphan*/ SELFMAG;
int /*<<< orphan*/ err(int, char *);
unsigned long load_bzimage(int, unsigned long *);
unsigned long map_elf(int, TYPE_1__ *, unsigned long *);
scalar_t__ memcmp(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, TYPE_1__ *, int);
unsigned long load_kernel(int fd, unsigned long *page_offset) {
  Elf32_Ehdr hdr;
  if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
    err(1, "Reading kernel");
  if (memcmp(hdr.e_ident, ELFMAG, SELFMAG) == 0)
    return map_elf(fd, &hdr, page_offset);
  return load_bzimage(fd, page_offset);
}


x86 assembly (clang, -O0):
<load_kernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
cltq
cmp    $0x4,%rax
je     118d <load_kernel+0x3d>
mov    $0x1,%edi
lea    0xe78(%rip),%rsi
call   1060 <err@plt>
mov    -0x20(%rbp),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    11ca <load_kernel+0x7a>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rsi
call   1050 <map_elf@plt>
mov    %rax,-0x8(%rbp)
jmp    11da <load_kernel+0x8a>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <load_bzimage@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_kernel>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x4,%edx
call   1030 <read@plt>
cmp    $0x4,%eax
je     117b <load_kernel+0x2b>
lea    0xe8f(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    (%rsp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     11aa <load_kernel+0x5a>
mov    %ebp,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <load_bzimage@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <map_elf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32 }
@.str = [15 x i8] c"Reading kernel\00"
@ELFMAG = global i32 0
@SELFMAG = global i32 0
define i64 @load_kernel(i32 %0, i64* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64*
%6 = alloca %struct.TYPE_4__
store i32 %0, i32* %4
store i64* %1, i64** %5
%7 = load i32, i32* %4
%8 = call i32 @read(i32 %7, %struct.TYPE_4__* %6, i32 4)
%9 = sext i32 %8 to i64
%10 = icmp ne i64 %9, 4
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @ELFMAG
%17 = load i32, i32* @SELFMAG
%18 = call i64 @memcmp(i32 %15, i32 %16, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = load i32, i32* %4
%22 = load i64*, i64** %5
%23 = call i64 @map_elf(i32 %21, %struct.TYPE_4__* %6, i64* %22)
store i64 %23, i64* %3
br label %28
24: ; preds = %13
%25 = load i32, i32* %4
%26 = load i64*, i64** %5
%27 = call i64 @load_bzimage(i32 %25, i64* %26)
store i64 %27, i64* %3
br label %28
28: ; preds = %24, %20
%29 = load i64, i64* %3
ret i64 %29
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32 }
@.str = [15 x i8] c"Reading kernel\00"
@ELFMAG = global i32 0
@SELFMAG = global i32 0
define i64 @load_kernel(i32 %0, i64* %1) {
%3 = alloca %struct.TYPE_4__
%4 = bitcast %struct.TYPE_4__* %3 to i8*
%5 = call i32 @read(i32 %0, %struct.TYPE_4__* %3, i32 4)
%6 = icmp eq i32 %5, 4
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @ELFMAG
%13 = load i32, i32* @SELFMAG
%14 = tail call i64 @memcmp(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i64 @map_elf(i32 %0, %struct.TYPE_4__* %3, i64* %1)
br label %20
18: ; preds = %9
%19 = tail call i64 @load_bzimage(i32 %0, i64* %1)
br label %20
20: ; preds = %18, %16
%21 = phi i64 [ %17, %16 ], [ %19, %18 ]
ret i64 %21
}