#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 */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  char *a = malloc(0x100 - 8);
  char *b = malloc(0x100 - 8);
  char *c = malloc(0x80 - 8);
  printf("---------------init---------------\n");
  printf("a: %p -> %p\n", a, a + 0x100 - 8);
  printf("b: %p -> %p\n", b, b + 0x100 - 8);
  printf("c: %p -> %p\n", c, c + 0x80 - 8);
  printf("----------------------------------\n");
  a[0x100 - 8] = 0x81;
  printf("overflow: %x\n", a[0x100 - 8]);
  free(b);
  free(c);
  b = malloc(0x100 + 0x80 - 8);
  printf("------------overflowed------------\n");
  printf("b: %p -> %p [*]\n", b, b + 0x100 + 0x80 - 8);
  printf("a: %p -> %p\nc: %p -> %p\n", a, a + 0x100 - 8, c, c + 0x80 - 8);
  printf("----------------------------------\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xf8,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0xf8,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x78,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x100,%rdx
add    $0xfffffffffffffff8,%rdx
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x100,%rdx
add    $0xfffffffffffffff8,%rdx
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x80,%rdx
add    $0xfffffffffffffff8,%rdx
lea    0xede(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
movb   $0x81,0xf8(%rax)
mov    -0x18(%rbp),%rax
movsbl 0xf8(%rax),%esi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0x178,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x100,%rdx
add    $0x80,%rdx
add    $0xfffffffffffffff8,%rdx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x100,%rdx
add    $0xfffffffffffffff8,%rdx
mov    -0x28(%rbp),%rcx
mov    -0x28(%rbp),%r8
add    $0x80,%r8
add    $0xfffffffffffffff8,%r8
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xf8,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0xf8,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0x78,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
lea    0xed9(%rip),%rdi
call   1040 <puts@plt>
lea    0xf8(%r14),%r15
lea    0xe74(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf8(%rbx),%rdx
lea    0xe66(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x78(%r13),%r12
lea    0xe89(%rip),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xec8(%rip),%rbp
mov    %rbp,%rdi
call   1040 <puts@plt>
movb   $0x81,0xf8(%r14)
81
lea    0xe33(%rip),%rdi
mov    $0xffffff81,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x178,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
lea    0x178(%rbx),%rdx
lea    0xdfe(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdfe(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r13,%rcx
mov    %r12,%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"---------------init---------------\0A\00"
@.str.1 = [13 x i8] c"a: %p -> %p\0A\00"
@.str.2 = [13 x i8] c"b: %p -> %p\0A\00"
@.str.3 = [13 x i8] c"c: %p -> %p\0A\00"
@.str.4 = [36 x i8] c"----------------------------------\0A\00"
@.str.5 = [14 x i8] c"overflow: %x\0A\00"
@.str.6 = [36 x i8] c"------------overflowed------------\0A\00"
@.str.7 = [17 x i8] c"b: %p -> %p [*]\0A\00"
@.str.8 = [25 x i8] c"a: %p -> %p\0Ac: %p -> %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = call i8* @malloc(i32 248)
store i8* %8, i8** %5
%9 = call i8* @malloc(i32 248)
store i8* %9, i8** %6
%10 = call i8* @malloc(i32 120)
store i8* %10, i8** %7
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%12 = load i8*, i8** %5
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 256
%15 = getelementptr inbounds i8, i8* %14, i64 -8
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %15)
%17 = load i8*, i8** %6
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 256
%20 = getelementptr inbounds i8, i8* %19, i64 -8
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %20)
%22 = load i8*, i8** %7
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i64 128
%25 = getelementptr inbounds i8, i8* %24, i64 -8
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %22, i8* %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i64 248
store i8 -127, i8* %29
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 248
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
%39 = call i8* @malloc(i32 376)
store i8* %39, i8** %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%41 = load i8*, i8** %6
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i64 256
%44 = getelementptr inbounds i8, i8* %43, i64 128
%45 = getelementptr inbounds i8, i8* %44, i64 -8
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %41, i8* %45)
%47 = load i8*, i8** %5
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i64 256
%50 = getelementptr inbounds i8, i8* %49, i64 -8
%51 = load i8*, i8** %7
%52 = load i8*, i8** %7
%53 = getelementptr inbounds i8, i8* %52, i64 128
%54 = getelementptr inbounds i8, i8* %53, i64 -8
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %47, i8* %50, i8* %51, i8* %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"a: %p -> %p\0A\00"
@.str.2 = [13 x i8] c"b: %p -> %p\0A\00"
@.str.3 = [13 x i8] c"c: %p -> %p\0A\00"
@.str.5 = [14 x i8] c"overflow: %x\0A\00"
@.str.7 = [17 x i8] c"b: %p -> %p [*]\0A\00"
@.str.8 = [25 x i8] c"a: %p -> %p\0Ac: %p -> %p\0A\00"
@str = [35 x i8] c"---------------init---------------\00"
@str.10 = [35 x i8] c"------------overflowed------------\00"
@str.11 = [35 x i8] c"----------------------------------\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call dereferenceable_or_null(248) i8* @malloc(i32 248)
%4 = tail call dereferenceable_or_null(248) i8* @malloc(i32 248)
%5 = tail call dereferenceable_or_null(120) i8* @malloc(i32 120)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%7 = getelementptr inbounds i8, i8* %3, i64 248
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %7)
%9 = getelementptr inbounds i8, i8* %4, i64 248
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %9)
%11 = getelementptr inbounds i8, i8* %5, i64 120
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %11)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.11, i64 0, i64 0))
store i8 -127, i8* %7
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 -127)
%15 = tail call i32 @free(i8* %4)
%16 = tail call i32 @free(i8* %5)
%17 = tail call dereferenceable_or_null(376) i8* @malloc(i32 376)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.10, i64 0, i64 0))
%19 = getelementptr inbounds i8, i8* %17, i64 376
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i8* %17, i8* %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* %3, i8* %7, i8* %5, i8* %11)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.11, i64 0, i64 0))
ret i32 0
}