#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, char *);
int /*<<< orphan*/ printf(char *, int);
void printPdeItem(unsigned startAddr, char *mem, int mem_size) {
  int j;
  j = 2 * 1024;
  int pdeItemNo = 0;
  for (; j < 4 * 1024; j = j + 4) {
    unsigned pgdCandidate = *(unsigned *)((unsigned)mem + startAddr + j);
    if ((pgdCandidate & ~0xfff) > mem_size)
      continue;
    if ((pgdCandidate & 0x3) == 0x3 && (pgdCandidate & 0x20) == 0x20 &&
        (pgdCandidate & 0x618) == 0) {
      pdeItemNo++;
    }
  }
  printf("pde item no: %d\n", pdeItemNo);
  FILE *out_data;
  out_data = fopen("PGDData", "a+");
  extern char *snapshot;
  fprintf(out_data, "%d\t%s\n", pdeItemNo, snapshot);
  fclose(out_data);
}


x86 assembly (clang, -O0):
<printPdeItem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x800,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1000,-0x18(%rbp)
jge    11df <printPdeItem+0x9f>
mov    -0x10(%rbp),%rax
add    -0x4(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,%eax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xfffff000,%eax
cmp    -0x14(%rbp),%eax
jbe    1194 <printPdeItem+0x54>
jmp    11d1 <printPdeItem+0x91>
mov    -0x20(%rbp),%eax
and    $0x3,%eax
cmp    $0x3,%eax
jne    11cc <printPdeItem+0x8c>
mov    -0x20(%rbp),%eax
and    $0x20,%eax
cmp    $0x20,%eax
jne    11cc <printPdeItem+0x8c>
mov    -0x20(%rbp),%eax
and    $0x618,%eax
cmp    $0x0,%eax
jne    11cc <printPdeItem+0x8c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d1 <printPdeItem+0x91>
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    1160 <printPdeItem+0x20>
mov    -0x1c(%rbp),%esi
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
lea    0xe1c(%rip),%rdi
lea    0xe1d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    0x2dcd(%rip),%rax
mov    (%rax),%rcx
lea    0xdff(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printPdeItem>:
push   %r14
push   %rbx
push   %rax
lea    (%rdi,%rsi,1),%eax
add    $0x804,%eax
xor    %ebx,%ebx
mov    $0x7fc,%r8d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x4(%rax),%esi
mov    (%rsi),%esi
mov    %esi,%edi
and    $0xfffff000,%edi
cmp    %edx,%edi
setbe  %dil
and    $0x63b,%esi
cmp    $0x23,%esi
sete   %cl
and    %dil,%cl
movzbl %cl,%ecx
add    %ebx,%ecx
mov    %eax,%esi
mov    (%rsi),%esi
mov    %esi,%edi
and    $0xfffff000,%edi
cmp    %edx,%edi
setbe  %dil
and    $0x63b,%esi
cmp    $0x23,%esi
sete   %bl
and    %dil,%bl
movzbl %bl,%ebx
add    %ecx,%ebx
add    $0x8,%eax
add    $0x8,%r8
cmp    $0xffc,%r8
jb     1160 <printPdeItem+0x20>
lea    0xe3c(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0xe3f(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    0x2df8(%rip),%rax
mov    (%rax),%rcx
lea    0xe2a(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1050 <fprintf@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"pde item no: %d\0A\00"
@.str.1 = [8 x i8] c"PGDData\00"
@.str.2 = [3 x i8] c"a+\00"
@.str.3 = [7 x i8] c"%d\09%s\0A\00"
@snapshot = external global i8*
define void @printPdeItem(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 2048, i32* %7
store i32 0, i32* %8
br label %11
11: ; preds = %45, %3
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 4096
br i1 %13, label %14, label %48
14: ; preds = %11
%15 = load i8*, i8** %5
%16 = ptrtoint i8* %15 to i32
%17 = load i32, i32* %4
%18 = add i32 %16, %17
%19 = load i32, i32* %7
%20 = add i32 %18, %19
%21 = zext i32 %20 to i64
%22 = inttoptr i64 %21 to i32*
%23 = load i32, i32* %22
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = and i32 %24, -4096
%26 = load i32, i32* %6
%27 = icmp ugt i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %14
br label %45
29: ; preds = %14
%30 = load i32, i32* %9
%31 = and i32 %30, 3
%32 = icmp eq i32 %31, 3
br i1 %32, label %33, label %44
33: ; preds = %29
%34 = load i32, i32* %9
%35 = and i32 %34, 32
%36 = icmp eq i32 %35, 32
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i32, i32* %9
%39 = and i32 %38, 1560
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %44
44: ; preds = %41, %37, %33, %29
br label %45
45: ; preds = %44, %28
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 4
store i32 %47, i32* %7
br label %11
48: ; preds = %11
%49 = load i32, i32* %8
%50 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %49)
%51 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %51, i32** %10
%52 = load i32*, i32** %10
%53 = load i32, i32* %8
%54 = load i8*, i8** @snapshot
%55 = call i32 @fprintf(i32* %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %53, i8* %54)
%56 = load i32*, i32** %10
%57 = call i32 @fclose(i32* %56)
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"pde item no: %d\0A\00"
@.str.1 = [8 x i8] c"PGDData\00"
@.str.2 = [3 x i8] c"a+\00"
@.str.3 = [7 x i8] c"%d\09%s\0A\00"
@snapshot = external global i8*
define void @printPdeItem(i32 %0, i8* %1, i32 %2) {
%4 = ptrtoint i8* %1 to i64
%5 = trunc i64 %4 to i32
%6 = add i32 %5, %0
br label %7
7: ; preds = %7, %3
%8 = phi i64 [ 2048, %3 ], [ %35, %7 ]
%9 = phi i32 [ 0, %3 ], [ %34, %7 ]
%10 = trunc i64 %8 to i32
%11 = add i32 %6, %10
%12 = zext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = load i32, i32* %13
%15 = and i32 %14, -4096
%16 = icmp ule i32 %15, %2
%17 = and i32 %14, 1595
%18 = icmp eq i32 %17, 35
%19 = and i1 %16, %18
%20 = zext i1 %19 to i32
%21 = add nuw nsw i32 %9, %20
%22 = or i64 %8, 4
%23 = trunc i64 %22 to i32
%24 = add i32 %6, %23
%25 = zext i32 %24 to i64
%26 = inttoptr i64 %25 to i32*
%27 = load i32, i32* %26
%28 = and i32 %27, -4096
%29 = icmp ule i32 %28, %2
%30 = and i32 %27, 1595
%31 = icmp eq i32 %30, 35
%32 = and i1 %29, %31
%33 = zext i1 %32 to i32
%34 = add nuw nsw i32 %21, %33
%35 = add nuw nsw i64 %8, 8
%36 = icmp ult i64 %22, 4092
br i1 %36, label %7, label %37
37: ; preds = %7
%38 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %34)
%39 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%40 = load i8*, i8** @snapshot
%41 = tail call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %40)
%42 = tail call i32 @fclose(i32* %39)
ret void
}