#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 uint32_t;
typedef scalar_t__ paddr;

/* Variables and functions */
int map_page_range(int, int, int, unsigned int);
int /*<<< orphan*/ panic(char *, int);
int map_mem_range(uint32_t vaddr, size_t sz, uint32_t paddr,
                  unsigned int attr) {
  int vpg_start, ppg_start, num_pages;
  if (!sz)
    return -1;
  if (((uint32_t)(paddr)&0xfff)) {
    panic("map_mem_range called with unaligned physical address: %x\n", paddr);
  }
  vpg_start = ((uint32_t)(vaddr) >> 12);
  ppg_start = paddr > 0 ? ((uint32_t)(paddr) >> 12) : -1;
  num_pages = ((uint32_t)(sz) >> 12) + 1;
  return map_page_range(vpg_start, num_pages, ppg_start, attr);
}


x86 assembly (clang, -O0):
<map_mem_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114c <map_mem_range+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <map_mem_range+0xa1>
mov    -0x14(%rbp),%eax
and    $0xfff,%eax
cmp    $0x0,%eax
je     116c <map_mem_range+0x4c>
mov    -0x14(%rbp),%esi
lea    0xe99(%rip),%rdi
call   1040 <panic@plt>
mov    -0x8(%rbp),%eax
sar    $0xc,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    118d <map_mem_range+0x6d>
mov    -0x14(%rbp),%eax
sar    $0xc,%eax
mov    %eax,-0x28(%rbp)
jmp    119a <map_mem_range+0x7a>
mov    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    119a <map_mem_range+0x7a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
sar    $0xc,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <map_page_range@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_mem_range>:
test   %rsi,%rsi
je     117d <map_mem_range+0x5d>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
test   $0xfff,%edx
je     114d <map_mem_range+0x2d>
lea    0xeba(%rip),%rdi
mov    %ebp,%esi
call   1040 <panic@plt>
sar    $0xc,%r15d
mov    %ebp,%eax
sar    $0xc,%eax
test   %ebp,%ebp
mov    $0xffffffff,%edx
cmovg  %eax,%edx
sar    $0xc,%ebx
add    $0x1,%ebx
mov    %r15d,%edi
mov    %ebx,%esi
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <map_page_range@plt>
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
@.str = [58 x i8] c"map_mem_range called with unaligned physical address: %x\0A\00"
define i32 @map_mem_range(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i64, i64* %7
%14 = icmp ne i64 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %4
store i32 -1, i32* %5
br label %43
16: ; preds = %4
%17 = load i32, i32* %8
%18 = and i32 %17, 4095
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %8
%22 = call i32 @panic(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %16
%24 = load i32, i32* %6
%25 = ashr i32 %24, 12
store i32 %25, i32* %10
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* %8
%30 = ashr i32 %29, 12
br label %32
31: ; preds = %23
br label %32
32: ; preds = %31, %28
%33 = phi i32 [ %30, %28 ], [ -1, %31 ]
store i32 %33, i32* %11
%34 = load i64, i64* %7
%35 = trunc i64 %34 to i32
%36 = ashr i32 %35, 12
%37 = add nsw i32 %36, 1
store i32 %37, i32* %12
%38 = load i32, i32* %10
%39 = load i32, i32* %12
%40 = load i32, i32* %11
%41 = load i32, i32* %9
%42 = call i32 @map_page_range(i32 %38, i32 %39, i32 %40, i32 %41)
store i32 %42, i32* %5
br label %43
43: ; preds = %32, %15
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@.str = [58 x i8] c"map_mem_range called with unaligned physical address: %x\0A\00"
define i32 @map_mem_range(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = icmp eq i64 %1, 0
br i1 %5, label %20, label %6
6: ; preds = %4
%7 = and i32 %2, 4095
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @panic(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %11
11: ; preds = %9, %6
%12 = ashr i32 %0, 12
%13 = icmp sgt i32 %2, 0
%14 = ashr i32 %2, 12
%15 = select i1 %13, i32 %14, i32 -1
%16 = trunc i64 %1 to i32
%17 = ashr i32 %16, 12
%18 = add nsw i32 %17, 1
%19 = tail call i32 @map_page_range(i32 %12, i32 %18, i32 %15, i32 %3)
br label %20
20: ; preds = %4, %11
%21 = phi i32 [ %19, %11 ], [ -1, %4 ]
ret i32 %21
}