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

/* Variables and functions */
char *malloc(int);
void *myalloc1(int);
void *myalloc2(int);
int /*<<< orphan*/ printf(char *);
void *strdup(char *);
scalar_t__ strncmp(void *, char *, int);
int main() {
  printf("[BEGIN] interpose-malloc\n");
  char *p1 = malloc(10);
  if (strncmp(p1 + 10, "##########", 10) != 0) {
    printf("[FAIL] interpose-malloc malloc() from main executable not "
           "interposed\n");
    return 0;
  }
  void *p2 = myalloc1(6);
  if (strncmp(p2 + 6, "######", 6) != 0) {
    printf("[FAIL] interpose-malloc myalloc1() from libfoo.dylib not "
           "interposed\n");
    return 0;
  }
  void *p3 = myalloc2(10);
  if (strncmp(p3 + 10, "##########", 10) != 0) {
    printf("[FAIL] interpose-malloc myalloc2() from libfoo.dylib not "
           "interposed\n");
    return 0;
  }
  void *p4 = strdup("hello");
  if (strncmp(p4 + 6, "#######", 6) != 0) {
    printf("[FAIL] interpose-malloc malloc() from strdup not interposed\n");
    return 0;
  }
  printf("[PASS] interpose-malloc\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1050 <printf@plt>
mov    $0xa,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0xa,%rdi
lea    0xe82(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11c4 <main+0x64>
lea    0xe72(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <main+0x154>
mov    $0x6,%edi
call   1040 <myalloc1@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x6,%rdi
lea    0xe3d(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     120d <main+0xad>
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <main+0x154>
mov    $0xa,%edi
call   1070 <myalloc2@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0xa,%rdi
lea    0xdf0(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1256 <main+0xf6>
lea    0xe6b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <main+0x154>
lea    0xe98(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x6,%rdi
lea    0xda8(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     12a1 <main+0x141>
lea    0xe6b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <main+0x154>
lea    0xe90(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xea9(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1060 <malloc@plt>
lea    0xa(%rax),%rdi
lea    0xe7e(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     119d <main+0x3d>
lea    0xf6e(%rip),%rdi
jmp    122d <main+0xcd>
mov    $0x6,%edi
call   1050 <myalloc1@plt>
lea    0x6(%rax),%rdi
lea    0xe52(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11ca <main+0x6a>
lea    0xefa(%rip),%rdi
jmp    122d <main+0xcd>
mov    $0xa,%edi
call   1070 <myalloc2@plt>
lea    0xa(%rax),%rdi
lea    0xe21(%rip),%rsi
mov    $0xa,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11f7 <main+0x97>
lea    0xe89(%rip),%rdi
jmp    122d <main+0xcd>
lea    0xe0d(%rip),%rdi
call   1080 <strdup@plt>
lea    0x6(%rax),%rdi
lea    0xdf5(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
lea    0xe08(%rip),%rax
lea    0xe19(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"[BEGIN] interpose-malloc\0A\00"
@.str.1 = [11 x i8] c"##########\00"
@.str.2 = [70 x i8] c"[FAIL] interpose-malloc malloc() from main executable not interposed\0A\00"
@.str.3 = [7 x i8] c"######\00"
@.str.4 = [69 x i8] c"[FAIL] interpose-malloc myalloc1() from libfoo.dylib not interposed\0A\00"
@.str.5 = [69 x i8] c"[FAIL] interpose-malloc myalloc2() from libfoo.dylib not interposed\0A\00"
@.str.6 = [6 x i8] c"hello\00"
@.str.7 = [8 x i8] c"#######\00"
@.str.8 = [61 x i8] c"[FAIL] interpose-malloc malloc() from strdup not interposed\0A\00"
@.str.9 = [25 x i8] c"[PASS] interpose-malloc\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%7 = call i8* @malloc(i32 10)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = getelementptr inbounds i8, i8* %8, i64 10
%10 = call i64 @strncmp(i8* %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 10)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %40
14: ; preds = %0
%15 = call i8* @myalloc1(i32 6)
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = getelementptr i8, i8* %16, i64 6
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %40
22: ; preds = %14
%23 = call i8* @myalloc2(i32 10)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = getelementptr i8, i8* %24, i64 10
%26 = call i64 @strncmp(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 10)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %40
30: ; preds = %22
%31 = call i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = getelementptr i8, i8* %32, i64 6
%34 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 6)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %40
38: ; preds = %30
%39 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %1
br label %40
40: ; preds = %38, %36, %28, %20, %12
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"##########\00"
@.str.3 = [7 x i8] c"######\00"
@.str.6 = [6 x i8] c"hello\00"
@.str.7 = [8 x i8] c"#######\00"
@str = [25 x i8] c"[BEGIN] interpose-malloc\00"
@str.10 = [24 x i8] c"[PASS] interpose-malloc\00"
@str.11 = [60 x i8] c"[FAIL] interpose-malloc malloc() from strdup not interposed\00"
@str.12 = [68 x i8] c"[FAIL] interpose-malloc myalloc2() from libfoo.dylib not interposed\00"
@str.13 = [68 x i8] c"[FAIL] interpose-malloc myalloc1() from libfoo.dylib not interposed\00"
@str.14 = [69 x i8] c"[FAIL] interpose-malloc malloc() from main executable not interposed\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%2 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%3 = getelementptr inbounds i8, i8* %2, i64 10
%4 = tail call i64 @strncmp(i8* %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 10)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %22
6: ; preds = %0
%7 = tail call i8* @myalloc1(i32 6)
%8 = getelementptr i8, i8* %7, i64 6
%9 = tail call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %6
%12 = tail call i8* @myalloc2(i32 10)
%13 = getelementptr i8, i8* %12, i64 10
%14 = tail call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 10)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = tail call dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%18 = getelementptr i8, i8* %17, i64 6
%19 = tail call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 6)
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0), i8* getelementptr inbounds ([60 x i8], [60 x i8]* @str.11, i64 0, i64 0)
br label %22
22: ; preds = %16, %11, %6, %0
%23 = phi i8* [ getelementptr inbounds ([69 x i8], [69 x i8]* @str.14, i64 0, i64 0), %0 ], [ getelementptr inbounds ([68 x i8], [68 x i8]* @str.13, i64 0, i64 0), %6 ], [ getelementptr inbounds ([68 x i8], [68 x i8]* @str.12, i64 0, i64 0), %11 ], [ %21, %16 ]
%24 = tail call i32 @puts(i8* dereferenceable(1) %23)
ret i32 0
}