#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*/ memcpy(void *, void *, int);
void *sStbAlloc(int);
int /*<<< orphan*/ sStbFree(void *);
void *chaos_realloc(void *src, int newsize, int oldsize) {
  void *dest;
  if (newsize == 0) {
    sStbFree(src);
    return 0;
  }
  if (src == 0) {
    return sStbAlloc(newsize);
  }
  if (newsize <= oldsize) {
    return src;
  }
  dest = sStbAlloc(newsize);
  memcpy(dest, src, oldsize);
  sStbFree(src);
  return dest;
}


x86 assembly (clang, -O0):
<chaos_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1162 <chaos_realloc+0x32>
mov    -0x10(%rbp),%rdi
call   1040 <sStbFree@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <chaos_realloc+0x94>
cmpq   $0x0,-0x10(%rbp)
jne    117e <chaos_realloc+0x4e>
mov    -0x14(%rbp),%edi
call   1050 <sStbAlloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11c4 <chaos_realloc+0x94>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1197 <chaos_realloc+0x67>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c4 <chaos_realloc+0x94>
mov    -0x14(%rbp),%edi
call   1050 <sStbAlloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <sStbFree@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaos_realloc>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %esi,%esi
je     116a <chaos_realloc+0x3a>
test   %rbx,%rbx
je     117c <chaos_realloc+0x4c>
mov    %edx,%ebp
cmp    %edx,%esi
jle    1174 <chaos_realloc+0x44>
mov    %esi,%edi
call   1050 <sStbAlloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <sStbFree@plt>
mov    %r14,%rbx
jmp    1174 <chaos_realloc+0x44>
mov    %rbx,%rdi
call   1040 <sStbFree@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %esi,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <sStbAlloc@plt>

LLVM IR (-O0):
define i8* @chaos_realloc(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = call i32 @sStbFree(i8* %12)
store i8* null, i8** %4
br label %36
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %6
%19 = call i8* @sStbAlloc(i32 %18)
store i8* %19, i8** %4
br label %36
20: ; preds = %14
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = icmp sle i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i8*, i8** %5
store i8* %25, i8** %4
br label %36
26: ; preds = %20
%27 = load i32, i32* %6
%28 = call i8* @sStbAlloc(i32 %27)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = load i8*, i8** %5
%31 = load i32, i32* %7
%32 = call i32 @memcpy(i8* %29, i8* %30, i32 %31)
%33 = load i8*, i8** %5
%34 = call i32 @sStbFree(i8* %33)
%35 = load i8*, i8** %8
store i8* %35, i8** %4
br label %36
36: ; preds = %26, %24, %17, %11
%37 = load i8*, i8** %4
ret i8* %37
}

LLVM IR (-O2):
define i8* @chaos_realloc(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @sStbFree(i8* %0)
br label %17
7: ; preds = %3
%8 = icmp eq i8* %0, null
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i8* @sStbAlloc(i32 %1)
br label %17
11: ; preds = %7
%12 = icmp sgt i32 %1, %2
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = tail call i8* @sStbAlloc(i32 %1)
%15 = tail call i32 @memcpy(i8* %14, i8* %0, i32 %2)
%16 = tail call i32 @sStbFree(i8* %0)
br label %17
17: ; preds = %11, %13, %9, %5
%18 = phi i8* [ null, %5 ], [ %10, %9 ], [ %14, %13 ], [ %0, %11 ]
ret i8* %18
}