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

/* Variables and functions */
int /*<<< orphan*/ getcwd(char *, int);
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void __attribute__((constructor)) blah(void) {
  char cwd[128];
  void *p;
  int i;
  setbuf(stdout, NULL);
  setbuf(stderr, NULL);
  printf("[+] in library\n");
  getcwd(cwd, sizeof(cwd));
  printf("[+] current directory: %s\n", cwd);
  printf("[+] testing allocations...\n");
  for (i = 0;; i++) {
    p = malloc(1024 * 1024);
    printf("    %03d: %p\n", i, p);
  }
}


x86 assembly (clang, -O0):
<blah>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    $0x80,%esi
call   1050 <getcwd@plt>
lea    -0x80(%rbp),%rsi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8c(%rbp)
mov    $0x100000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x88(%rbp)
mov    -0x8c(%rbp),%esi
mov    -0x88(%rbp),%rdx
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    11b5 <blah+0x75>

x86 assembly (clang, -O2):
<blah>:
push   %rbp
push   %rbx
sub    $0x88,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1060 <getcwd@plt>
lea    0xe64(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
lea    0xe62(%rip),%rbx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
jmp    11c0 <blah+0x70>

LLVM IR (-O0):
@stdout = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[+] in library\0A\00"
@.str.1 = [27 x i8] c"[+] current directory: %s\0A\00"
@.str.2 = [28 x i8] c"[+] testing allocations...\0A\00"
@.str.3 = [14 x i8] c" %03d: %p\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @blah, i8* null }]
define void @blah() {
%1 = alloca [128 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = load i32, i32* @stdout
%5 = call i32 @setbuf(i32 %4, i32* null)
%6 = load i32, i32* @stderr
%7 = call i32 @setbuf(i32 %6, i32* null)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%10 = call i32 @getcwd(i8* %9, i32 128)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %14
14: ; preds = %19, %0
%15 = call i8* @malloc(i32 1048576)
store i8* %15, i8** %2
%16 = load i32, i32* %3
%17 = load i8*, i8** %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %16, i8* %17)
br label %19
19: ; preds = %14
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %14
}

LLVM IR (-O2):
@stdout = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"[+] current directory: %s\0A\00"
@.str.3 = [14 x i8] c" %03d: %p\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @blah, i8* null }]
@str = [15 x i8] c"[+] in library\00"
@str.4 = [27 x i8] c"[+] testing allocations...\00"
define void @blah() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdout
%4 = tail call i32 @setbuf(i32 %3, i32* null)
%5 = load i32, i32* @stderr
%6 = tail call i32 @setbuf(i32 %5, i32* null)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%8 = call i32 @getcwd(i8* %2, i32 128)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.4, i64 0, i64 0))
br label %11
11: ; preds = %11, %0
%12 = phi i32 [ 0, %0 ], [ %15, %11 ]
%13 = call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %12, i8* %13)
%15 = add nuw nsw i32 %12, 1
br label %11
}