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

/* Variables and functions */
int /*<<< orphan*/ debug_print_addr(char *, void *);
int /*<<< orphan*/ debug_print_int(char *, unsigned char);
int /*<<< orphan*/ early_puts(char *);
int /*<<< orphan*/ virtio_panic(char *);
int /*<<< orphan*/ virtio_puts(char *);
unsigned char virtio_read_many(int, void *, int);
unsigned long virtio_load_direct(unsigned long rec_list1,
                                 unsigned long rec_list2,
                                 unsigned long subchan_id, void *load_addr) {
  unsigned char status;
  int sec = rec_list1;
  int sec_num = (((rec_list2 >> 32) + 1) & 0xffff);
  int sec_len = rec_list2 >> 48;
  unsigned long addr = (unsigned long)load_addr;
  int i;
  early_puts("_load_direct\n");
  debug_print_int("rec_list1", rec_list1);
  debug_print_int("rec_list2", rec_list2);
  debug_print_int("subchan_id", subchan_id);
  debug_print_addr("load_addr", load_addr);
  if (sec_len != 512) {
    debug_print_int("sector size invalid", sec_len);
    return -1;
  }
  virtio_puts(".");
  status = virtio_read_many(sec, (void *)addr, sec_num);
  if (status) {
    debug_print_int("load_direct status", status);
    virtio_panic("I/O Error");
  }
  addr += sec_num * 512;
  return addr;
}


x86 assembly (clang, -O0):
<virtio_load_direct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x20,%rax
add    $0x1,%rax
and    $0xffff,%rax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x30,%rax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    0xe52(%rip),%rdi
call   1030 <early_puts@plt>
mov    -0x10(%rbp),%rax
lea    0xe50(%rip),%rdi
movzbl %al,%esi
call   1080 <debug_print_int@plt>
mov    -0x18(%rbp),%rax
lea    0xe47(%rip),%rdi
movzbl %al,%esi
call   1080 <debug_print_int@plt>
mov    -0x20(%rbp),%rax
lea    0xe3e(%rip),%rdi
movzbl %al,%esi
call   1080 <debug_print_int@plt>
mov    -0x28(%rbp),%rsi
lea    0xe36(%rip),%rdi
call   1060 <debug_print_addr@plt>
cmpl   $0x200,-0x38(%rbp)
je     1228 <virtio_load_direct+0xc8>
mov    -0x38(%rbp),%eax
lea    0xe24(%rip),%rdi
movzbl %al,%esi
call   1080 <debug_print_int@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1284 <virtio_load_direct+0x124>
lea    0xe1c(%rip),%rdi
call   1040 <virtio_puts@plt>
mov    -0x30(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1050 <virtio_read_many@plt>
mov    %al,-0x29(%rbp)
cmpb   $0x0,-0x29(%rbp)
je     126c <virtio_load_direct+0x10c>
lea    0xdf6(%rip),%rdi
movzbl -0x29(%rbp),%esi
call   1080 <debug_print_int@plt>
lea    0xdf9(%rip),%rdi
call   1070 <virtio_panic@plt>
mov    -0x34(%rbp),%eax
shl    $0x9,%eax
cltq
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<virtio_load_direct>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%r13
shr    $0x30,%r13
lea    0xe7d(%rip),%rdi
call   1030 <early_puts@plt>
movzbl %r15b,%esi
lea    0xe7b(%rip),%rdi
call   1080 <debug_print_int@plt>
movzbl %bl,%esi
lea    0xe76(%rip),%rdi
call   1080 <debug_print_int@plt>
movzbl %r12b,%esi
lea    0xe70(%rip),%rdi
call   1080 <debug_print_int@plt>
lea    0xe6f(%rip),%rdi
mov    %r14,%rsi
call   1060 <debug_print_addr@plt>
cmp    $0x200,%r13
jne    1219 <virtio_load_direct+0xb9>
shr    $0x20,%rbx
add    $0x1,%ebx
movzwl %bx,%ebx
lea    0xe6b(%rip),%rdi
call   1040 <virtio_puts@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <virtio_read_many@plt>
test   %al,%al
je     1211 <virtio_load_direct+0xb1>
movzbl %al,%esi
lea    0xe4d(%rip),%rdi
call   1080 <debug_print_int@plt>
lea    0xe54(%rip),%rdi
call   1070 <virtio_panic@plt>
shl    $0x9,%ebx
add    %r14,%rbx
jmp    1230 <virtio_load_direct+0xd0>
movzbl %r13b,%esi
lea    0xe13(%rip),%rdi
call   1080 <debug_print_int@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"_load_direct\0A\00"
@.str.1 = [10 x i8] c"rec_list1\00"
@.str.2 = [10 x i8] c"rec_list2\00"
@.str.3 = [11 x i8] c"subchan_id\00"
@.str.4 = [10 x i8] c"load_addr\00"
@.str.5 = [20 x i8] c"sector size invalid\00"
@.str.6 = [2 x i8] c".\00"
@.str.7 = [19 x i8] c"load_direct status\00"
@.str.8 = [10 x i8] c"I/O Error\00"
define i64 @virtio_load_direct(i64 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
store i64 %0, i64* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%16 = load i64, i64* %6
%17 = trunc i64 %16 to i32
store i32 %17, i32* %11
%18 = load i64, i64* %7
%19 = lshr i64 %18, 32
%20 = add i64 %19, 1
%21 = and i64 %20, 65535
%22 = trunc i64 %21 to i32
store i32 %22, i32* %12
%23 = load i64, i64* %7
%24 = lshr i64 %23, 48
%25 = trunc i64 %24 to i32
store i32 %25, i32* %13
%26 = load i8*, i8** %9
%27 = ptrtoint i8* %26 to i64
store i64 %27, i64* %14
%28 = call i32 @early_puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%29 = load i64, i64* %6
%30 = trunc i64 %29 to i8
%31 = call i32 @debug_print_int(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %30)
%32 = load i64, i64* %7
%33 = trunc i64 %32 to i8
%34 = call i32 @debug_print_int(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %33)
%35 = load i64, i64* %8
%36 = trunc i64 %35 to i8
%37 = call i32 @debug_print_int(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %36)
%38 = load i8*, i8** %9
%39 = call i32 @debug_print_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %13
%41 = icmp ne i32 %40, 512
br i1 %41, label %42, label %46
42: ; preds = %4
%43 = load i32, i32* %13
%44 = trunc i32 %43 to i8
%45 = call i32 @debug_print_int(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8 zeroext %44)
store i64 -1, i64* %5
br label %66
46: ; preds = %4
%47 = call i32 @virtio_puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* %11
%49 = load i64, i64* %14
%50 = inttoptr i64 %49 to i8*
%51 = load i32, i32* %12
%52 = call zeroext i8 @virtio_read_many(i32 %48, i8* %50, i32 %51)
store i8 %52, i8* %10
%53 = load i8, i8* %10
%54 = icmp ne i8 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %46
%56 = load i8, i8* %10
%57 = call i32 @debug_print_int(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8 zeroext %56)
%58 = call i32 @virtio_panic(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %59
59: ; preds = %55, %46
%60 = load i32, i32* %12
%61 = mul nsw i32 %60, 512
%62 = sext i32 %61 to i64
%63 = load i64, i64* %14
%64 = add i64 %63, %62
store i64 %64, i64* %14
%65 = load i64, i64* %14
store i64 %65, i64* %5
br label %66
66: ; preds = %59, %42
%67 = load i64, i64* %5
ret i64 %67
}

LLVM IR (-O2):
@.str = [14 x i8] c"_load_direct\0A\00"
@.str.1 = [10 x i8] c"rec_list1\00"
@.str.2 = [10 x i8] c"rec_list2\00"
@.str.3 = [11 x i8] c"subchan_id\00"
@.str.4 = [10 x i8] c"load_addr\00"
@.str.5 = [20 x i8] c"sector size invalid\00"
@.str.6 = [2 x i8] c".\00"
@.str.7 = [19 x i8] c"load_direct status\00"
@.str.8 = [10 x i8] c"I/O Error\00"
define i64 @virtio_load_direct(i64 %0, i64 %1, i64 %2, i8* %3) {
%5 = lshr i64 %1, 32
%6 = trunc i64 %5 to i32
%7 = add i32 %6, 1
%8 = and i32 %7, 65535
%9 = lshr i64 %1, 48
%10 = ptrtoint i8* %3 to i64
%11 = tail call i32 @early_puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = trunc i64 %0 to i8
%13 = tail call i32 @debug_print_int(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %12)
%14 = trunc i64 %1 to i8
%15 = tail call i32 @debug_print_int(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %14)
%16 = trunc i64 %2 to i8
%17 = tail call i32 @debug_print_int(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %16)
%18 = tail call i32 @debug_print_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%19 = icmp eq i64 %9, 512
br i1 %19, label %23, label %20
20: ; preds = %4
%21 = trunc i64 %9 to i8
%22 = tail call i32 @debug_print_int(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8 zeroext %21)
br label %35
23: ; preds = %4
%24 = trunc i64 %0 to i32
%25 = tail call i32 @virtio_puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%26 = tail call zeroext i8 @virtio_read_many(i32 %24, i8* %3, i32 %8)
%27 = icmp eq i8 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = tail call i32 @debug_print_int(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8 zeroext %26)
%30 = tail call i32 @virtio_panic(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %31
31: ; preds = %28, %23
%32 = shl nuw nsw i32 %8, 9
%33 = zext i32 %32 to i64
%34 = add i64 %33, %10
br label %35
35: ; preds = %31, %20
%36 = phi i64 [ -1, %20 ], [ %34, %31 ]
ret i64 %36
}