#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  scalar_t__ *e_ident;
  int e_phnum;
  int e_phoff;
  int e_phentsize;
};
struct TYPE_3__ {
  scalar_t__ p_type;
  unsigned long p_filesz;
  int p_offset;
  int p_flags;
};
typedef int /*<<< orphan*/ Elf64_Shdr;
typedef TYPE_1__ Elf64_Phdr;
typedef TYPE_2__ Elf64_Ehdr;

/* Variables and functions */
size_t EI_MAG0;
size_t EI_MAG1;
size_t EI_MAG2;
size_t EI_MAG3;
scalar_t__ ELFMAG0;
scalar_t__ ELFMAG1;
scalar_t__ ELFMAG2;
scalar_t__ ELFMAG3;
int PF_X;
scalar_t__ PT_LOAD;
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ privify_memory(char *, unsigned long);
void privify_elf(char *elfbase) {
  Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfbase;
  Elf64_Phdr *phdr;
  Elf64_Shdr *shdr;
  char *elfaddr;
  unsigned long size;
  int h;
  if (!((*ehdr).e_ident[EI_MAG0] == ELFMAG0 &&
        (*ehdr).e_ident[EI_MAG1] == ELFMAG1 &&
        (*ehdr).e_ident[EI_MAG2] == ELFMAG2 &&
        (*ehdr).e_ident[EI_MAG3] == ELFMAG3))
    panic("Kernel image does not have an ELF header.\n");
  for (h = 0; h < ehdr->e_phnum; h++) {
    phdr = (Elf64_Phdr *)(elfbase + ehdr->e_phoff + (h * ehdr->e_phentsize));
    printf("h=%d, phdr=%p,phdr->p_type=%lx", h, phdr, phdr->p_type);
    if ((phdr->p_type != PT_LOAD)) {
      printf("\n");
      continue;
    }
    size = phdr->p_filesz;
    elfaddr = elfbase + phdr->p_offset;
    printf(",elfaddr=%p,size=%d,phdr->p_flags=%lx\n", elfaddr, size,
           phdr->p_flags);
    if (phdr->p_flags & PF_X)
      privify_memory(elfaddr, size);
  }
}


x86 assembly (clang, -O0):
<privify_elf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <privify_elf+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <privify_elf+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <privify_elf+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
je     11e4 <privify_elf+0xb4>
lea    0xe21(%rip),%rdi
call   1040 <panic@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%eax
jge    12da <privify_elf+0x1aa>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq 0xc(%rcx),%rcx
add    %rcx,%rax
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rdx
imul   0x10(%rdx),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x34(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%rax
je     1265 <privify_elf+0x135>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12cc <privify_elf+0x19c>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq 0x10(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    0x14(%rax),%ecx
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x14(%rax),%eax
mov    0x2d29(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c7 <privify_elf+0x197>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <privify_memory@plt>
jmp    12cc <privify_elf+0x19c>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11eb <privify_elf+0xbb>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<privify_elf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    (%rdi),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rcx
mov    0x2e52(%rip),%rdx
cmp    (%rdx),%rcx
jne    11b9 <privify_elf+0x79>
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rcx
mov    0x2e70(%rip),%rdx
cmp    (%rdx),%rcx
jne    11b9 <privify_elf+0x79>
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rcx
mov    0x2dfe(%rip),%rdx
cmp    (%rdx),%rcx
jne    11b9 <privify_elf+0x79>
mov    0x2e02(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
je     11c5 <privify_elf+0x85>
lea    0xe40(%rip),%rdi
call   1050 <panic@plt>
cmpl   $0x0,0x8(%r12)
jle    1274 <privify_elf+0x134>
xor    %r14d,%r14d
jmp    11f5 <privify_elf+0xb5>
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r14d
cmp    0x8(%r12),%r14d
jge    1274 <privify_elf+0x134>
movslq 0xc(%r12),%rbp
add    %r12,%rbp
movslq 0x10(%r12),%rbx
movslq %r14d,%rsi
imul   %rsi,%rbx
lea    (%rbx,%rbp,1),%r15
mov    (%rbx,%rbp,1),%rcx
lea    0xe13(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx,%rbp,1),%rax
mov    0x2d93(%rip),%rcx
cmp    (%rcx),%rax
jne    11e0 <privify_elf+0xa0>
mov    0x8(%r15),%r13
movslq 0x10(%r15),%rbp
add    %r12,%rbp
mov    0x14(%r15),%ecx
lea    0xe02(%rip),%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
test   %eax,0x14(%r15)
je     11ea <privify_elf+0xaa>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1060 <privify_memory@plt>
jmp    11ea <privify_elf+0xaa>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i64*, i32, i32, i32 }
%struct.TYPE_3__ = type { i64, i64, i32, i32 }
@EI_MAG0 = global i64 0
@ELFMAG0 = global i64 0
@EI_MAG1 = global i64 0
@ELFMAG1 = global i64 0
@EI_MAG2 = global i64 0
@ELFMAG2 = global i64 0
@EI_MAG3 = global i64 0
@ELFMAG3 = global i64 0
@.str = [43 x i8] c"Kernel image does not have an ELF header.\0A\00"
@.str.1 = [31 x i8] c"h=%d, phdr=%p,phdr->p_type=%lx\00"
@PT_LOAD = global i64 0
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [39 x i8] c",elfaddr=%p,size=%d,phdr->p_flags=%lx\0A\00"
@PF_X = global i32 0
define void @privify_elf(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.TYPE_4__*
%4 = alloca %struct.TYPE_3__*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to %struct.TYPE_4__*
store %struct.TYPE_4__* %10, %struct.TYPE_4__** %3
%11 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %11, i32 0, i32 0
%13 = load i64*, i64** %12
%14 = load i64, i64* @EI_MAG0
%15 = getelementptr inbounds i64, i64* %13, i64 %14
%16 = load i64, i64* %15
%17 = load i64, i64* @ELFMAG0
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %46
19: ; preds = %1
%20 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%21 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 0
%22 = load i64*, i64** %21
%23 = load i64, i64* @EI_MAG1
%24 = getelementptr inbounds i64, i64* %22, i64 %23
%25 = load i64, i64* %24
%26 = load i64, i64* @ELFMAG1
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %46
28: ; preds = %19
%29 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%30 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %29, i32 0, i32 0
%31 = load i64*, i64** %30
%32 = load i64, i64* @EI_MAG2
%33 = getelementptr inbounds i64, i64* %31, i64 %32
%34 = load i64, i64* %33
%35 = load i64, i64* @ELFMAG2
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %46
37: ; preds = %28
%38 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%39 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %38, i32 0, i32 0
%40 = load i64*, i64** %39
%41 = load i64, i64* @EI_MAG3
%42 = getelementptr inbounds i64, i64* %40, i64 %41
%43 = load i64, i64* %42
%44 = load i64, i64* @ELFMAG3
%45 = icmp eq i64 %43, %44
br i1 %45, label %48, label %46
46: ; preds = %37, %28, %19, %1
%47 = call i32 @panic(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %48
48: ; preds = %46, %37
store i32 0, i32* %8
br label %49
49: ; preds = %110, %48
%50 = load i32, i32* %8
%51 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = icmp slt i32 %50, %53
br i1 %54, label %55, label %113
55: ; preds = %49
%56 = load i8*, i8** %2
%57 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %57, i32 0, i32 2
%59 = load i32, i32* %58
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %56, i64 %60
%62 = load i32, i32* %8
%63 = load %struct.TYPE_4__*, %struct.TYPE_4__** %3
%64 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %63, i32 0, i32 3
%65 = load i32, i32* %64
%66 = mul nsw i32 %62, %65
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %61, i64 %67
%69 = bitcast i8* %68 to %struct.TYPE_3__*
store %struct.TYPE_3__* %69, %struct.TYPE_3__** %4
%70 = load i32, i32* %8
%71 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%72 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%73 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %72, i32 0, i32 0
%74 = load i64, i64* %73
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %70, %struct.TYPE_3__* %71, i64 %74)
%76 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%77 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %76, i32 0, i32 0
%78 = load i64, i64* %77
%79 = load i64, i64* @PT_LOAD
%80 = icmp ne i64 %78, %79
br i1 %80, label %81, label %83
81: ; preds = %55
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %110
83: ; preds = %55
%84 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%85 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %84, i32 0, i32 1
%86 = load i64, i64* %85
store i64 %86, i64* %7
%87 = load i8*, i8** %2
%88 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%89 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %88, i32 0, i32 2
%90 = load i32, i32* %89
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %87, i64 %91
store i8* %92, i8** %6
%93 = load i8*, i8** %6
%94 = load i64, i64* %7
%95 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%96 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %95, i32 0, i32 3
%97 = load i32, i32* %96
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %93, i64 %94, i32 %97)
%99 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%100 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %99, i32 0, i32 3
%101 = load i32, i32* %100
%102 = load i32, i32* @PF_X
%103 = and i32 %101, %102
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %109
105: ; preds = %83
%106 = load i8*, i8** %6
%107 = load i64, i64* %7
%108 = call i32 @privify_memory(i8* %106, i64 %107)
br label %109
109: ; preds = %105, %83
br label %110
110: ; preds = %109, %81
%111 = load i32, i32* %8
%112 = add nsw i32 %111, 1
store i32 %112, i32* %8
br label %49
113: ; preds = %49
ret void
}

LLVM IR (-O2):
@EI_MAG0 = global i64 0
@ELFMAG0 = global i64 0
@EI_MAG1 = global i64 0
@ELFMAG1 = global i64 0
@EI_MAG2 = global i64 0
@ELFMAG2 = global i64 0
@EI_MAG3 = global i64 0
@ELFMAG3 = global i64 0
@.str = [43 x i8] c"Kernel image does not have an ELF header.\0A\00"
@.str.1 = [31 x i8] c"h=%d, phdr=%p,phdr->p_type=%lx\00"
@PT_LOAD = global i64 0
@.str.3 = [39 x i8] c",elfaddr=%p,size=%d,phdr->p_flags=%lx\0A\00"
@PF_X = global i32 0
define void @privify_elf(i8* %0) {
%2 = bitcast i8* %0 to i64**
%3 = load i64*, i64** %2
%4 = load i64, i64* @EI_MAG0
%5 = getelementptr inbounds i64, i64* %3, i64 %4
%6 = load i64, i64* %5
%7 = load i64, i64* @ELFMAG0
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %27
9: ; preds = %1
%10 = load i64, i64* @EI_MAG1
%11 = getelementptr inbounds i64, i64* %3, i64 %10
%12 = load i64, i64* %11
%13 = load i64, i64* @ELFMAG1
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %27
15: ; preds = %9
%16 = load i64, i64* @EI_MAG2
%17 = getelementptr inbounds i64, i64* %3, i64 %16
%18 = load i64, i64* %17
%19 = load i64, i64* @ELFMAG2
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i64, i64* @EI_MAG3
%23 = getelementptr inbounds i64, i64* %3, i64 %22
%24 = load i64, i64* %23
%25 = load i64, i64* @ELFMAG3
%26 = icmp eq i64 %24, %25
br i1 %26, label %29, label %27
27: ; preds = %21, %15, %9, %1
%28 = tail call i32 @panic(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = getelementptr inbounds i8, i8* %0, i64 8
%31 = bitcast i8* %30 to i32*
%32 = load i32, i32* %31
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %79
34: ; preds = %29
%35 = getelementptr inbounds i8, i8* %0, i64 12
%36 = bitcast i8* %35 to i32*
%37 = getelementptr inbounds i8, i8* %0, i64 16
%38 = bitcast i8* %37 to i32*
br label %39
39: ; preds = %34, %75
%40 = phi i32 [ 0, %34 ], [ %76, %75 ]
%41 = load i32, i32* %36
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %0, i64 %42
%44 = load i32, i32* %38
%45 = mul nsw i32 %44, %40
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
%48 = bitcast i8* %47 to i64*
%49 = load i64, i64* %48
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* %47, i64 %49)
%51 = load i64, i64* %48
%52 = load i64, i64* @PT_LOAD
%53 = icmp eq i64 %51, %52
br i1 %53, label %56, label %54
54: ; preds = %39
%55 = tail call i32 @putchar(i32 10)
br label %75
56: ; preds = %39
%57 = getelementptr inbounds i8, i8* %47, i64 8
%58 = bitcast i8* %57 to i64*
%59 = load i64, i64* %58
%60 = getelementptr inbounds i8, i8* %47, i64 16
%61 = bitcast i8* %60 to i32*
%62 = load i32, i32* %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %0, i64 %63
%65 = getelementptr inbounds i8, i8* %47, i64 20
%66 = bitcast i8* %65 to i32*
%67 = load i32, i32* %66
%68 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %64, i64 %59, i32 %67)
%69 = load i32, i32* %66
%70 = load i32, i32* @PF_X
%71 = and i32 %70, %69
%72 = icmp eq i32 %71, 0
br i1 %72, label %75, label %73
73: ; preds = %56
%74 = tail call i32 @privify_memory(i8* %64, i64 %59)
br label %75
75: ; preds = %56, %73, %54
%76 = add nuw nsw i32 %40, 1
%77 = load i32, i32* %31
%78 = icmp slt i32 %76, %77
br i1 %78, label %39, label %79
79: ; preds = %75, %29
ret void
}