#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 uint64_t;

/* Variables and functions */
int strtoull(char *, int /*<<< orphan*/ *, int);
uint64_t guess_kernel_base(char *str) {
  uint64_t addr = strtoull(str, NULL, 16);
  if (addr < 0x40000000UL)
    return 0x40000000UL;
  else if (addr < 0x80000000UL)
    return 0x80000000UL;
  else if (addr < 0xB0000000UL)
    return 0xB0000000UL;
  else if (addr < 0xC0000000UL)
    return 0xC0000000UL;
  else if (addr < 0x8000000000ULL)
    return 0xFFFFFF8000000000ULL;
  else if (addr < 0x40000000000ULL)
    return 0xFFFFFC0000000000ULL;
  else if (addr < 0x800000000000ULL)
    return 0xFFFF800000000000ULL;
  else
    return 0xFFFF000000000000ULL;
}


x86 assembly (clang, -O0):
<guess_kernel_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1030 <strtoull@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x40000000,%rax
jae    114d <guess_kernel_base+0x3d>
movl   $0x40000000,-0x4(%rbp)
jmp    1217 <guess_kernel_base+0x107>
movslq -0x14(%rbp),%rax
mov    $0x80000000,%ecx
cmp    %rcx,%rax
jae    116b <guess_kernel_base+0x5b>
movl   $0x80000000,-0x4(%rbp)
jmp    1217 <guess_kernel_base+0x107>
movslq -0x14(%rbp),%rax
mov    $0xb0000000,%ecx
cmp    %rcx,%rax
jae    1189 <guess_kernel_base+0x79>
movl   $0xb0000000,-0x4(%rbp)
jmp    1217 <guess_kernel_base+0x107>
movslq -0x14(%rbp),%rax
mov    $0xc0000000,%ecx
cmp    %rcx,%rax
jae    11a7 <guess_kernel_base+0x97>
movl   $0xc0000000,-0x4(%rbp)
jmp    1217 <guess_kernel_base+0x107>
movslq -0x14(%rbp),%rax
movabs $0x8000000000,%rcx
cmp    %rcx,%rax
jae    11ca <guess_kernel_base+0xba>
movl   $0x0,-0x4(%rbp)
jmp    1217 <guess_kernel_base+0x107>
movslq -0x14(%rbp),%rax
movabs $0x40000000000,%rcx
cmp    %rcx,%rax
jae    11ed <guess_kernel_base+0xdd>
movl   $0x0,-0x4(%rbp)
jmp    1217 <guess_kernel_base+0x107>
movslq -0x14(%rbp),%rax
movabs $0x800000000000,%rcx
cmp    %rcx,%rax
jae    1210 <guess_kernel_base+0x100>
movl   $0x0,-0x4(%rbp)
jmp    1217 <guess_kernel_base+0x107>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guess_kernel_base>:
push   %rax
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <strtoull@plt>
mov    %eax,%ecx
not    %ecx
and    $0x80000000,%ecx
cmp    $0x40000000,%eax
mov    $0x40000000,%eax
cmovae %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @guess_kernel_base(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @strtoull(i8* %5, i32* null, i32 16)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = icmp ult i64 %8, 1073741824
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 1073741824, i32* %2
br label %42
11: ; preds = %1
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = icmp ult i64 %13, 2147483648
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 -2147483648, i32* %2
br label %42
16: ; preds = %11
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = icmp ult i64 %18, 2952790016
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1342177280, i32* %2
br label %42
21: ; preds = %16
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = icmp ult i64 %23, 3221225472
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1073741824, i32* %2
br label %42
26: ; preds = %21
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %28, 549755813888
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 0, i32* %2
br label %42
31: ; preds = %26
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = icmp ult i64 %33, 4398046511104
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 0, i32* %2
br label %42
36: ; preds = %31
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = icmp ult i64 %38, 140737488355328
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 0, i32* %2
br label %42
41: ; preds = %36
store i32 0, i32* %2
br label %42
42: ; preds = %41, %40, %35, %30, %25, %20, %15, %10
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
define i32 @guess_kernel_base(i8* nocapture %0) {
%2 = tail call i32 @strtoull(i8* nocapture %0, i32* null, i32 16)
%3 = icmp ult i32 %2, 1073741824
%4 = and i32 %2, -2147483648
%5 = xor i32 %4, -2147483648
%6 = select i1 %3, i32 1073741824, i32 %5
ret i32 %6
}