#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ check_arakoon_free(void *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
void *realloc(void *, size_t);
int /*<<< orphan*/ stderr;
void *check_arakoon_realloc(void *ptr, size_t s) {
  size_t s0 = 0, len = 0;
  void *ret = NULL;
  if (s == 0) {
    if (ptr != NULL) {
      check_arakoon_free(ptr);
    }
    return NULL;
  }
  if (ptr != NULL) {
    ptr = ((void *)((char *)ptr - sizeof(uint32_t) - sizeof(size_t)));
    s0 = (*(((size_t *)ptr)));
    if ((*(((uint32_t *)((char *)((size_t *)ptr) + sizeof(size_t))))) !=
        (0xdeadbeef)) {
      fprintf(stderr, "realloc: Prefix sentinel corrupted");
      fflush(stderr);
      abort();
    }
    if ((*(((uint32_t *)((char *)ptr + s0 - sizeof(uint32_t))))) !=
        (0xcafebabe)) {
      fprintf(stderr, "realloc: Suffix sentinel corrupted");
      fflush(stderr);
      abort();
    }
  }
  len = (s + sizeof(size_t) + sizeof(uint32_t) + sizeof(uint32_t));
  ret = realloc(ptr, len);
  if (ret == NULL) {
    fprintf(stderr, "realloc: realloc returned NULL");
    fflush(stderr);
    abort();
  }
  do {
    *((size_t *)ret) = len;
  } while (0);
  do {
    *(((uint32_t *)((char *)((size_t *)ret) + sizeof(size_t)))) = (0xdeadbeef);
  } while (0);
  do {
    *(((uint32_t *)((char *)ret + len - sizeof(uint32_t)))) = (0xcafebabe);
  } while (0);
  return ((void *)((char *)ret + sizeof(size_t) + sizeof(uint32_t)));
}


x86 assembly (clang, -O0):
<check_arakoon_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a4 <check_arakoon_realloc+0x54>
cmpq   $0x0,-0x10(%rbp)
je     1197 <check_arakoon_realloc+0x47>
mov    -0x10(%rbp),%rdi
call   1040 <check_arakoon_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <check_arakoon_realloc+0x199>
cmpq   $0x0,-0x10(%rbp)
je     124a <check_arakoon_realloc+0xfa>
mov    -0x10(%rbp),%rax
add    $0xfffffffffffffffc,%rax
add    $0xfffffffffffffff8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xdeadbeef,0x8(%rax)
je     1205 <check_arakoon_realloc+0xb5>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmpl   $0xcafebabe,-0x4(%rax,%rcx,1)
ca
je     1245 <check_arakoon_realloc+0xf5>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    124a <check_arakoon_realloc+0xfa>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
add    $0x4,%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12a4 <check_arakoon_realloc+0x154>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    12a9 <check_arakoon_realloc+0x159>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
jmp    12b9 <check_arakoon_realloc+0x169>
mov    -0x30(%rbp),%rax
movl   $0xdeadbeef,0x8(%rax)
jmp    12c9 <check_arakoon_realloc+0x179>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movl   $0xcafebabe,-0x4(%rax,%rcx,1)
ca
mov    -0x30(%rbp),%rax
add    $0x8,%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_arakoon_realloc>:
push   %rbx
test   %rsi,%rsi
je     118a <check_arakoon_realloc+0x3a>
mov    %rsi,%rbx
test   %rdi,%rdi
je     1198 <check_arakoon_realloc+0x48>
cmpl   $0xdeadbeef,-0x4(%rdi)
jne    11d5 <check_arakoon_realloc+0x85>
add    $0xfffffffffffffff4,%rdi
mov    (%rdi),%rax
cmpl   $0xcafebabe,-0x4(%rdi,%rax,1)
ca
je     119a <check_arakoon_realloc+0x4a>
mov    0x2e61(%rip),%rbx
mov    (%rbx),%edi
lea    0xe9b(%rip),%rsi
jmp    11e5 <check_arakoon_realloc+0x95>
test   %rdi,%rdi
je     1194 <check_arakoon_realloc+0x44>
call   1040 <check_arakoon_free@plt>
xor    %eax,%eax
pop    %rbx
ret
xor    %edi,%edi
add    $0x10,%rbx
mov    %rbx,%rsi
call   1070 <realloc@plt>
test   %rax,%rax
je     11c3 <check_arakoon_realloc+0x73>
mov    %rbx,(%rax)
movl   $0xdeadbeef,0x8(%rax)
movl   $0xcafebabe,-0x4(%rax,%rbx,1)
ca
add    $0xc,%rax
pop    %rbx
ret
mov    0x2e16(%rip),%rbx
mov    (%rbx),%edi
lea    0xe73(%rip),%rsi
jmp    11e5 <check_arakoon_realloc+0x95>
mov    0x2e04(%rip),%rbx
mov    (%rbx),%edi
lea    0xe1b(%rip),%rsi
call   1050 <fprintf@plt>
mov    (%rbx),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"realloc: Prefix sentinel corrupted\00"
@.str.1 = [35 x i8] c"realloc: Suffix sentinel corrupted\00"
@.str.2 = [31 x i8] c"realloc: realloc returned NULL\00"
define i8* @check_arakoon_realloc(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
store i8* null, i8** %8
%9 = load i64, i64* %5
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i8*, i8** %4
%16 = call i32 @check_arakoon_free(i8* %15)
br label %17
17: ; preds = %14, %11
store i8* null, i8** %3
br label %95
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %56
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 -4
%24 = getelementptr inbounds i8, i8* %23, i64 -8
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = bitcast i8* %25 to i64*
%27 = load i64, i64* %26
store i64 %27, i64* %6
%28 = load i8*, i8** %4
%29 = bitcast i8* %28 to i64*
%30 = bitcast i64* %29 to i8*
%31 = getelementptr inbounds i8, i8* %30, i64 8
%32 = bitcast i8* %31 to i32*
%33 = load i32, i32* %32
%34 = icmp ne i32 %33, -559038737
br i1 %34, label %35, label %41
35: ; preds = %21
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%38 = load i32, i32* @stderr
%39 = call i32 @fflush(i32 %38)
%40 = call i32 (...) @abort()
unreachable
41: ; preds = %21
%42 = load i8*, i8** %4
%43 = load i64, i64* %6
%44 = getelementptr inbounds i8, i8* %42, i64 %43
%45 = getelementptr inbounds i8, i8* %44, i64 -4
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
%48 = icmp ne i32 %47, -889275714
br i1 %48, label %49, label %55
49: ; preds = %41
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%52 = load i32, i32* @stderr
%53 = call i32 @fflush(i32 %52)
%54 = call i32 (...) @abort()
unreachable
55: ; preds = %41
br label %56
56: ; preds = %55, %18
%57 = load i64, i64* %5
%58 = add i64 %57, 8
%59 = add i64 %58, 4
%60 = add i64 %59, 4
store i64 %60, i64* %7
%61 = load i8*, i8** %4
%62 = load i64, i64* %7
%63 = call i8* @realloc(i8* %61, i64 %62)
store i8* %63, i8** %8
%64 = load i8*, i8** %8
%65 = icmp eq i8* %64, null
br i1 %65, label %66, label %72
66: ; preds = %56
%67 = load i32, i32* @stderr
%68 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%69 = load i32, i32* @stderr
%70 = call i32 @fflush(i32 %69)
%71 = call i32 (...) @abort()
unreachable
72: ; preds = %56
br label %73
73: ; preds = %72
%74 = load i64, i64* %7
%75 = load i8*, i8** %8
%76 = bitcast i8* %75 to i64*
store i64 %74, i64* %76
br label %77
77: ; preds = %73
br label %78
78: ; preds = %77
%79 = load i8*, i8** %8
%80 = bitcast i8* %79 to i64*
%81 = bitcast i64* %80 to i8*
%82 = getelementptr inbounds i8, i8* %81, i64 8
%83 = bitcast i8* %82 to i32*
store i32 -559038737, i32* %83
br label %84
84: ; preds = %78
br label %85
85: ; preds = %84
%86 = load i8*, i8** %8
%87 = load i64, i64* %7
%88 = getelementptr inbounds i8, i8* %86, i64 %87
%89 = getelementptr inbounds i8, i8* %88, i64 -4
%90 = bitcast i8* %89 to i32*
store i32 -889275714, i32* %90
br label %91
91: ; preds = %85
%92 = load i8*, i8** %8
%93 = getelementptr inbounds i8, i8* %92, i64 8
%94 = getelementptr inbounds i8, i8* %93, i64 4
store i8* %94, i8** %3
br label %95
95: ; preds = %91, %17
%96 = load i8*, i8** %3
ret i8* %96
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"realloc: Prefix sentinel corrupted\00"
@.str.1 = [35 x i8] c"realloc: Suffix sentinel corrupted\00"
@.str.2 = [31 x i8] c"realloc: realloc returned NULL\00"
define noalias i8* @check_arakoon_realloc(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = icmp eq i8* %0, null
br i1 %3, label %5, label %8
5: ; preds = %2
br i1 %4, label %54, label %6
6: ; preds = %5
%7 = tail call i32 @check_arakoon_free(i8* %0)
br label %54
8: ; preds = %2
br i1 %4, label %35, label %9
9: ; preds = %8
%10 = getelementptr inbounds i8, i8* %0, i64 -12
%11 = getelementptr inbounds i8, i8* %0, i64 -4
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, -559038737
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = tail call i32 @fflush(i32 %18)
%20 = tail call i32 (...) @abort()
unreachable
21: ; preds = %9
%22 = bitcast i8* %10 to i64*
%23 = load i64, i64* %22
%24 = getelementptr inbounds i8, i8* %10, i64 %23
%25 = getelementptr inbounds i8, i8* %24, i64 -4
%26 = bitcast i8* %25 to i32*
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, -889275714
br i1 %28, label %35, label %29
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @stderr
%33 = tail call i32 @fflush(i32 %32)
%34 = tail call i32 (...) @abort()
unreachable
35: ; preds = %21, %8
%36 = phi i8* [ %10, %21 ], [ null, %8 ]
%37 = add i64 %1, 16
%38 = tail call i8* @realloc(i8* %36, i64 %37)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @stderr
%44 = tail call i32 @fflush(i32 %43)
%45 = tail call i32 (...) @abort()
unreachable
46: ; preds = %35
%47 = bitcast i8* %38 to i64*
store i64 %37, i64* %47
%48 = getelementptr inbounds i8, i8* %38, i64 8
%49 = bitcast i8* %48 to i32*
store i32 -559038737, i32* %49
%50 = getelementptr inbounds i8, i8* %38, i64 %37
%51 = getelementptr inbounds i8, i8* %50, i64 -4
%52 = bitcast i8* %51 to i32*
store i32 -889275714, i32* %52
%53 = getelementptr inbounds i8, i8* %38, i64 12
br label %54
54: ; preds = %5, %6, %46
%55 = phi i8* [ %53, %46 ], [ null, %6 ], [ null, %5 ]
ret i8* %55
}