#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_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ p_memsz;
};
typedef TYPE_1__ Elf64_Phdr;
typedef int /*<<< orphan*/ Elf64_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ AT_PHDR;
int /*<<< orphan*/ _munmap(void *, int /*<<< orphan*/);
void *auxv_base_addr(void *);
scalar_t__ auxv_read(void *, int /*<<< orphan*/);
int /*<<< orphan*/ *extract_elf(TYPE_1__ *);
int /*<<< orphan*/ load_interp(int /*<<< orphan*/ *);
int /*<<< orphan*/ pt_load(int /*<<< orphan*/ *);
void extract(void *stack) {
  Elf64_Ehdr *elf_hdr;
  Elf64_Phdr *phdr;
  void *auxv;
  auxv = auxv_base_addr(stack);
  phdr = (Elf64_Phdr *)auxv_read(auxv, AT_PHDR);
  ++phdr;
  elf_hdr = extract_elf(phdr);
  pt_load(elf_hdr);
  load_interp(elf_hdr);
  _munmap((void *)(0xffff400000), phdr->p_memsz);
}


x86 assembly (clang, -O0):
<extract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <auxv_base_addr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1080 <auxv_read@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <extract_elf@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <pt_load@plt>
mov    -0x10(%rbp),%rdi
call   1070 <load_interp@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
movabs $0xffff400000,%rdi
call   1060 <_munmap@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract>:
push   %r14
push   %rbx
push   %rax
call   1030 <auxv_base_addr@plt>
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1080 <auxv_read@plt>
mov    %rax,%r14
lea    0x4(%rax),%rdi
call   1050 <extract_elf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <pt_load@plt>
mov    %rbx,%rdi
call   1070 <load_interp@plt>
mov    0x4(%r14),%esi
movabs $0xffff400000,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <_munmap@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@AT_PHDR = global i32 0
define void @extract(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.TYPE_3__*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @auxv_base_addr(i8* %6)
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = load i32, i32* @AT_PHDR
%10 = call i64 @auxv_read(i8* %8, i32 %9)
%11 = inttoptr i64 %10 to %struct.TYPE_3__*
store %struct.TYPE_3__* %11, %struct.TYPE_3__** %4
%12 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i32 1
store %struct.TYPE_3__* %13, %struct.TYPE_3__** %4
%14 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%15 = call i32* @extract_elf(%struct.TYPE_3__* %14)
store i32* %15, i32** %3
%16 = load i32*, i32** %3
%17 = call i32 @pt_load(i32* %16)
%18 = load i32*, i32** %3
%19 = call i32 @load_interp(i32* %18)
%20 = load %struct.TYPE_3__*, %struct.TYPE_3__** %4
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @_munmap(i8* inttoptr (i64 1099499044864 to i8*), i32 %22)
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@AT_PHDR = global i32 0
define void @extract(i8* %0) {
%2 = tail call i8* @auxv_base_addr(i8* %0)
%3 = load i32, i32* @AT_PHDR
%4 = tail call i64 @auxv_read(i8* %2, i32 %3)
%5 = inttoptr i64 %4 to %struct.TYPE_3__*
%6 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i64 1
%7 = tail call i32* @extract_elf(%struct.TYPE_3__* %6)
%8 = tail call i32 @pt_load(i32* %7)
%9 = tail call i32 @load_interp(i32* %7)
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @_munmap(i8* inttoptr (i64 1099499044864 to i8*), i32 %11)
ret void
}