#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*/ assert(int);
int /*<<< orphan*/ err(int, char *, size_t, int);
int /*<<< orphan*/ warnx(char *, size_t);
void hvt_x86_mem_size(size_t *mem_size) {
  size_t mem;
  mem = (*mem_size / 0x200000) * 0x200000;
  assert(mem <= *mem_size);
  if (mem < *mem_size)
    warnx("adjusting memory to %zu bytes", mem);
  if (mem > 0x200000 * 512)
    err(1, "guest memory size %zu bytes exceeds the max size %u bytes", mem,
        0x200000 * 512);
  *mem_size = mem;
}


x86 assembly (clang, -O0):
<hvt_x86_mem_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
shr    $0x15,%rax
shl    $0x15,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%rax
jae    1188 <hvt_x86_mem_size+0x58>
mov    -0x10(%rbp),%rsi
lea    0xe7d(%rip),%rdi
call   1030 <warnx@plt>
cmpq   $0x40000000,-0x10(%rbp)
40
jbe    11b0 <hvt_x86_mem_size+0x80>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe78(%rip),%rsi
mov    $0x40000000,%ecx
call   1050 <err@plt>
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hvt_x86_mem_size>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0xffffffffffe00000,%rbx
and    (%rdi),%rbx
mov    $0x1,%edi
call   1040 <assert@plt>
cmp    (%r14),%rbx
jae    115f <hvt_x86_mem_size+0x2f>
lea    0xea9(%rip),%rdi
mov    %rbx,%rsi
call   1030 <warnx@plt>
cmp    $0x40000001,%rbx
jb     1181 <hvt_x86_mem_size+0x51>
lea    0xeaf(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
mov    $0x40000000,%ecx
call   1050 <err@plt>
mov    %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [30 x i8] c"adjusting memory to %zu bytes\00"
@.str.1 = [58 x i8] c"guest memory size %zu bytes exceeds the max size %u bytes\00"
define void @hvt_x86_mem_size(i64* %0) {
%2 = alloca i64*
%3 = alloca i64
store i64* %0, i64** %2
%4 = load i64*, i64** %2
%5 = load i64, i64* %4
%6 = udiv i64 %5, 2097152
%7 = mul i64 %6, 2097152
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = load i64*, i64** %2
%10 = load i64, i64* %9
%11 = icmp ule i64 %8, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i64, i64* %3
%15 = load i64*, i64** %2
%16 = load i64, i64* %15
%17 = icmp ult i64 %14, %16
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i64, i64* %3
%20 = call i32 @warnx(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %19)
br label %21
21: ; preds = %18, %1
%22 = load i64, i64* %3
%23 = icmp ugt i64 %22, 1073741824
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i64, i64* %3
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %25, i32 1073741824)
br label %27
27: ; preds = %24, %21
%28 = load i64, i64* %3
%29 = load i64*, i64** %2
store i64 %28, i64* %29
ret void
}

LLVM IR (-O2):
@.str = [30 x i8] c"adjusting memory to %zu bytes\00"
@.str.1 = [58 x i8] c"guest memory size %zu bytes exceeds the max size %u bytes\00"
define void @hvt_x86_mem_size(i64* nocapture %0) {
%2 = load i64, i64* %0
%3 = and i64 %2, -2097152
%4 = tail call i32 @assert(i32 1)
%5 = load i64, i64* %0
%6 = icmp ult i64 %3, %5
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @warnx(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %3)
br label %9
9: ; preds = %7, %1
%10 = icmp ugt i64 %3, 1073741824
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %3, i32 1073741824)
br label %13
13: ; preds = %11, %9
store i64 %3, i64* %0
ret void
}