#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 map_elf(int, TYPE_1__ *);
scalar_t__ memcmp(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, TYPE_1__ *, int);
unsigned long load_kernel(int fd) {
  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);
  return load_bzimage(fd);
}


x86 assembly (clang, -O0):
<load_kernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
cltq
cmp    $0x4,%rax
je     1189 <load_kernel+0x39>
mov    $0x1,%edi
lea    0xe7c(%rip),%rsi
call   1060 <err@plt>
mov    -0x10(%rbp),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    11c2 <load_kernel+0x72>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <map_elf@plt>
mov    %rax,-0x8(%rbp)
jmp    11ce <load_kernel+0x7e>
mov    -0xc(%rbp),%edi
call   1070 <load_bzimage@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_kernel>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
cmp    $0x4,%eax
je     117c <load_kernel+0x2c>
lea    0xe8e(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x8(%rsp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     11a8 <load_kernel+0x58>
mov    %ebx,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1070 <load_bzimage@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <map_elf@plt>
add    $0x10,%rsp
pop    %rbx
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) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca %struct.TYPE_4__
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @read(i32 %5, %struct.TYPE_4__* %4, i32 4)
%7 = sext i32 %6 to i64
%8 = icmp ne i64 %7, 4
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @ELFMAG
%15 = load i32, i32* @SELFMAG
%16 = call i64 @memcmp(i32 %13, i32 %14, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* %3
%20 = call i64 @map_elf(i32 %19, %struct.TYPE_4__* %4)
store i64 %20, i64* %2
br label %24
21: ; preds = %11
%22 = load i32, i32* %3
%23 = call i64 @load_bzimage(i32 %22)
store i64 %23, i64* %2
br label %24
24: ; preds = %21, %18
%25 = load i64, i64* %2
ret i64 %25
}

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) {
%2 = alloca %struct.TYPE_4__
%3 = bitcast %struct.TYPE_4__* %2 to i8*
%4 = call i32 @read(i32 %0, %struct.TYPE_4__* %2, i32 4)
%5 = icmp eq i32 %4, 4
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @ELFMAG
%12 = load i32, i32* @SELFMAG
%13 = tail call i64 @memcmp(i32 %10, i32 %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = call i64 @map_elf(i32 %0, %struct.TYPE_4__* %2)
br label %19
17: ; preds = %8
%18 = tail call i64 @load_bzimage(i32 %0)
br label %19
19: ; preds = %17, %15
%20 = phi i64 [ %16, %15 ], [ %18, %17 ]
ret i64 %20
}