#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*/ close(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned long);
scalar_t__ malloc(int);
main() {
  static char file[] = "Emulator/malloc_base.h";
  unsigned long int wordsize = sizeof(unsigned long int) * 8;
  char *mp = (char *)malloc(1024);
  unsigned long int word = (unsigned long int)mp >> wordsize - 4;
  FILE *fd;
  if ((fd = fopen(file, "w")) != NULL) {
    fprintf(fd, "#define MALLOC_BASE 0x%lx\n", (word << wordsize - 4));
    close(fd);
    exit(0);
  } else {
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x40,-0x10(%rbp)
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x4,%rcx
shr    %cl,%rax
mov    %rax,-0x20(%rbp)
lea    0x2ec1(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11d8 <main+0x88>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
sub    $0x4,%rcx
shl    %cl,%rdx
lea    0xe3f(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <close@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0x2ee8(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
jne    1183 <main+0x33>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    %rax,%rbx
movabs $0xf000000000000000,%rax
and    %rax,%r14
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <close@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@main.file = internal global [23 x i8] c"Emulator/malloc_base.h\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [27 x i8] c"#define MALLOC_BASE 0x%lx\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
store i32 0, i32* %1
store i64 64, i64* %2
%6 = call i64 @malloc(i32 1024)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i64
%10 = load i64, i64* %2
%11 = sub i64 %10, 4
%12 = lshr i64 %9, %11
store i64 %12, i64* %4
%13 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @main.file, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %25
15: ; preds = %0
%16 = load i32*, i32** %5
%17 = load i64, i64* %4
%18 = load i64, i64* %2
%19 = sub i64 %18, 4
%20 = shl i64 %17, %19
%21 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %20)
%22 = load i32*, i32** %5
%23 = call i32 @close(i32* %22)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %0
%26 = call i32 @exit(i32 -1)
unreachable
}

LLVM IR (-O2):
@main.file = internal global [23 x i8] c"Emulator/malloc_base.h\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [27 x i8] c"#define MALLOC_BASE 0x%lx\0A\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 1024)
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @main.file, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %9, label %4
4: ; preds = %0
%5 = and i64 %1, -1152921504606846976
%6 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %5)
%7 = tail call i32 @close(i32* %2)
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %0
%10 = tail call i32 @exit(i32 -1)
unreachable
}