#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*/ __kern_mempageset(int, int);
scalar_t__ __kern_pmem_writable(char *);
int /*<<< orphan*/ kprintf(char *, unsigned int, unsigned int);
void __kern_pmem_init(void) {
  unsigned char p, q;
  unsigned int romsize = 0;
  unsigned int ramsize = 0;
  char *ptr = (char *)0xa000U;
  __kern_mempageset(0x2600, 0);
  for (p = 0; p <= 255U && !__kern_pmem_writable(ptr);
       __kern_mempageset(0x2600, ++p))
    ;
  romsize = (unsigned int)p << 5;
  for (q = p; q <= 255U && __kern_pmem_writable(ptr);
       __kern_mempageset(0x2600, ++q))
    ;
  ramsize = ((unsigned int)q << 5) - romsize;
  __kern_mempageset(0x2600, 5);
  kprintf("MEM:   %ukB ROM, %ukB RAM\n", romsize, ramsize);
}


x86 assembly (clang, -O0):
<__kern_pmem_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0xa000,%eax
mov    %rax,-0x18(%rbp)
mov    $0x2600,%edi
xor    %esi,%esi
call   1040 <__kern_mempageset@plt>
movb   $0x0,-0x1(%rbp)
movzbl -0x1(%rbp),%ecx
xor    %eax,%eax
cmp    $0xff,%ecx
mov    %al,-0x19(%rbp)
ja     1189 <__kern_pmem_init+0x59>
mov    -0x18(%rbp),%rdi
call   1030 <__kern_pmem_writable@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1199 <__kern_pmem_init+0x69>
jmp    11b8 <__kern_pmem_init+0x88>
jmp    119e <__kern_pmem_init+0x6e>
mov    -0x1(%rbp),%al
add    $0x1,%al
mov    %al,-0x1(%rbp)
movzbl %al,%esi
mov    $0x2600,%edi
call   1040 <__kern_mempageset@plt>
jmp    115f <__kern_pmem_init+0x2f>
movzbl -0x1(%rbp),%eax
shl    $0x5,%eax
mov    %eax,-0x8(%rbp)
mov    -0x1(%rbp),%al
mov    %al,-0x2(%rbp)
movzbl -0x2(%rbp),%ecx
xor    %eax,%eax
cmp    $0xff,%ecx
mov    %al,-0x1a(%rbp)
ja     11f0 <__kern_pmem_init+0xc0>
mov    -0x18(%rbp),%rdi
call   1030 <__kern_pmem_writable@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
test   $0x1,%al
jne    1200 <__kern_pmem_init+0xd0>
jmp    121f <__kern_pmem_init+0xef>
jmp    1205 <__kern_pmem_init+0xd5>
mov    -0x2(%rbp),%al
add    $0x1,%al
mov    %al,-0x2(%rbp)
movzbl %al,%esi
mov    $0x2600,%edi
call   1040 <__kern_mempageset@plt>
jmp    11c8 <__kern_pmem_init+0x98>
movzbl -0x2(%rbp),%eax
shl    $0x5,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x2600,%edi
mov    $0x5,%esi
call   1040 <__kern_mempageset@plt>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xdb8(%rip),%rdi
call   1050 <kprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__kern_pmem_init>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
mov    $0x2600,%edi
xor    %esi,%esi
call   1040 <__kern_mempageset@plt>
mov    $0xa000,%edi
call   1030 <__kern_pmem_writable@plt>
test   %rax,%rax
jne    1181 <__kern_pmem_init+0x51>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%bpl
movzbl %bpl,%esi
mov    $0x2600,%edi
call   1040 <__kern_mempageset@plt>
mov    $0xa000,%edi
call   1030 <__kern_pmem_writable@plt>
test   %rax,%rax
je     1160 <__kern_pmem_init+0x30>
movzbl %bpl,%r14d
mov    $0xa000,%edi
call   1030 <__kern_pmem_writable@plt>
mov    %r14d,%ebx
test   %rax,%rax
je     11c2 <__kern_pmem_init+0x92>
add    $0x1,%bpl
nopl   0x0(%rax,%rax,1)
movzbl %bpl,%ebx
mov    $0x2600,%edi
mov    %ebx,%esi
call   1040 <__kern_mempageset@plt>
mov    $0xa000,%edi
call   1030 <__kern_pmem_writable@plt>
lea    0x1(%rbx),%ebp
test   %rax,%rax
jne    11a0 <__kern_pmem_init+0x70>
sub    %r14d,%ebx
shl    $0x5,%r14d
shl    $0x5,%ebx
mov    $0x2600,%edi
mov    $0x5,%esi
call   1040 <__kern_mempageset@plt>
lea    0xe1e(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <kprintf@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"MEM: %ukB ROM, %ukB RAM\0A\00"
define void @__kern_pmem_init() {
%1 = alloca i8
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %3
store i32 0, i32* %4
store i8* inttoptr (i64 40960 to i8*), i8** %5
%6 = call i32 @__kern_mempageset(i32 9728, i32 0)
store i8 0, i8* %1
br label %7
7: ; preds = %19, %0
%8 = load i8, i8* %1
%9 = zext i8 %8 to i32
%10 = icmp ule i32 %9, 255
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i8*, i8** %5
%13 = call i64 @__kern_pmem_writable(i8* %12)
%14 = icmp ne i64 %13, 0
%15 = xor i1 %14, true
br label %16
16: ; preds = %11, %7
%17 = phi i1 [ false, %7 ], [ %15, %11 ]
br i1 %17, label %18, label %24
18: ; preds = %16
br label %19
19: ; preds = %18
%20 = load i8, i8* %1
%21 = add i8 %20, 1
store i8 %21, i8* %1
%22 = zext i8 %21 to i32
%23 = call i32 @__kern_mempageset(i32 9728, i32 %22)
br label %7
24: ; preds = %16
%25 = load i8, i8* %1
%26 = zext i8 %25 to i32
%27 = shl i32 %26, 5
store i32 %27, i32* %3
%28 = load i8, i8* %1
store i8 %28, i8* %2
br label %29
29: ; preds = %40, %24
%30 = load i8, i8* %2
%31 = zext i8 %30 to i32
%32 = icmp ule i32 %31, 255
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i8*, i8** %5
%35 = call i64 @__kern_pmem_writable(i8* %34)
%36 = icmp ne i64 %35, 0
br label %37
37: ; preds = %33, %29
%38 = phi i1 [ false, %29 ], [ %36, %33 ]
br i1 %38, label %39, label %45
39: ; preds = %37
br label %40
40: ; preds = %39
%41 = load i8, i8* %2
%42 = add i8 %41, 1
store i8 %42, i8* %2
%43 = zext i8 %42 to i32
%44 = call i32 @__kern_mempageset(i32 9728, i32 %43)
br label %29
45: ; preds = %37
%46 = load i8, i8* %2
%47 = zext i8 %46 to i32
%48 = shl i32 %47, 5
%49 = load i32, i32* %3
%50 = sub i32 %48, %49
store i32 %50, i32* %4
%51 = call i32 @__kern_mempageset(i32 9728, i32 5)
%52 = load i32, i32* %3
%53 = load i32, i32* %4
%54 = call i32 @kprintf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %52, i32 %53)
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"MEM: %ukB ROM, %ukB RAM\0A\00"
define void @__kern_pmem_init() {
%1 = tail call i32 @__kern_mempageset(i32 9728, i32 0)
%2 = tail call i64 @__kern_pmem_writable(i8* inttoptr (i64 40960 to i8*))
%3 = icmp eq i64 %2, 0
br i1 %3, label %9, label %4
4: ; preds = %9, %0
%5 = phi i8 [ 0, %0 ], [ %11, %9 ]
%6 = zext i8 %5 to i32
%7 = tail call i64 @__kern_pmem_writable(i8* inttoptr (i64 40960 to i8*))
%8 = icmp eq i64 %7, 0
br i1 %8, label %23, label %16
9: ; preds = %0, %9
%10 = phi i8 [ %11, %9 ], [ 0, %0 ]
%11 = add i8 %10, 1
%12 = zext i8 %11 to i32
%13 = tail call i32 @__kern_mempageset(i32 9728, i32 %12)
%14 = tail call i64 @__kern_pmem_writable(i8* inttoptr (i64 40960 to i8*))
%15 = icmp eq i64 %14, 0
br i1 %15, label %9, label %4
16: ; preds = %4, %16
%17 = phi i8 [ %18, %16 ], [ %5, %4 ]
%18 = add i8 %17, 1
%19 = zext i8 %18 to i32
%20 = tail call i32 @__kern_mempageset(i32 9728, i32 %19)
%21 = tail call i64 @__kern_pmem_writable(i8* inttoptr (i64 40960 to i8*))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %16
23: ; preds = %16, %4
%24 = phi i32 [ %6, %4 ], [ %19, %16 ]
%25 = shl nuw nsw i32 %6, 5
%26 = sub nsw i32 %24, %6
%27 = shl nsw i32 %26, 5
%28 = tail call i32 @__kern_mempageset(i32 9728, i32 5)
%29 = tail call i32 @kprintf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %27)
ret void
}