#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*/ abort();
int /*<<< orphan*/ block_free(void *, size_t);
void *block_malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *block_realloc(void *vmem, size_t oldsize, size_t newsize) {
  void *vnewmem;
  if (vmem == NULL && newsize == 0)
    abort();
  if (vmem == NULL)
    return block_malloc(newsize);
  if (newsize == 0) {
    block_free(vmem, oldsize);
    return NULL;
  }
  vnewmem = block_malloc(newsize);
  if (vnewmem) {
    memcpy(vnewmem, vmem, (oldsize < newsize ? oldsize : newsize));
    block_free(vmem, oldsize);
  }
  return vnewmem;
}


x86 assembly (clang, -O0):
<block_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1171 <block_realloc+0x31>
cmpq   $0x0,-0x20(%rbp)
jne    1171 <block_realloc+0x31>
mov    $0x0,%al
call   1040 <abort@plt>
cmpq   $0x0,-0x10(%rbp)
jne    118e <block_realloc+0x4e>
mov    -0x20(%rbp),%rdi
call   1050 <block_malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    1224 <block_realloc+0xe4>
cmpq   $0x0,-0x20(%rbp)
jne    11b3 <block_realloc+0x73>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <block_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1224 <block_realloc+0xe4>
mov    -0x20(%rbp),%rdi
call   1050 <block_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121c <block_realloc+0xdc>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11f6 <block_realloc+0xb6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11fe <block_realloc+0xbe>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <block_free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_realloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
jne    115b <block_realloc+0x1b>
test   %rbx,%rbx
je     11c7 <block_realloc+0x87>
test   %r15,%r15
je     1197 <block_realloc+0x57>
test   %rbx,%rbx
je     11aa <block_realloc+0x6a>
mov    %rbx,%rdi
call   1050 <block_malloc@plt>
test   %rax,%rax
je     11b5 <block_realloc+0x75>
mov    %rax,%r12
cmp    %rbx,%r14
cmovb  %r14,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <block_free@plt>
jmp    11b8 <block_realloc+0x78>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <block_malloc@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <block_free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
define i8* @block_realloc(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load i64, i64* %7
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %11, %3
%17 = load i8*, i8** %5
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i64, i64* %7
%21 = call i8* @block_malloc(i64 %20)
store i8* %21, i8** %4
br label %52
22: ; preds = %16
%23 = load i64, i64* %7
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i8*, i8** %5
%27 = load i64, i64* %6
%28 = call i32 @block_free(i8* %26, i64 %27)
store i8* null, i8** %4
br label %52
29: ; preds = %22
%30 = load i64, i64* %7
%31 = call i8* @block_malloc(i64 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %50
34: ; preds = %29
%35 = load i8*, i8** %8
%36 = load i8*, i8** %5
%37 = load i64, i64* %6
%38 = load i64, i64* %7
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = load i64, i64* %6
br label %44
42: ; preds = %34
%43 = load i64, i64* %7
br label %44
44: ; preds = %42, %40
%45 = phi i64 [ %41, %40 ], [ %43, %42 ]
%46 = call i32 @memcpy(i8* %35, i8* %36, i64 %45)
%47 = load i8*, i8** %5
%48 = load i64, i64* %6
%49 = call i32 @block_free(i8* %47, i64 %48)
br label %50
50: ; preds = %44, %29
%51 = load i8*, i8** %8
store i8* %51, i8** %4
br label %52
52: ; preds = %50, %25, %19
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
define i8* @block_realloc(i8* %0, i64 %1, i64 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i64 %2, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %3
br i1 %4, label %10, label %12
10: ; preds = %9
%11 = tail call i8* @block_malloc(i64 %2)
br label %23
12: ; preds = %9
br i1 %5, label %13, label %15
13: ; preds = %12
%14 = tail call i32 @block_free(i8* %0, i64 %1)
br label %23
15: ; preds = %12
%16 = tail call i8* @block_malloc(i64 %2)
%17 = icmp eq i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = icmp ult i64 %1, %2
%20 = select i1 %19, i64 %1, i64 %2
%21 = tail call i32 @memcpy(i8* %16, i8* %0, i64 %20)
%22 = tail call i32 @block_free(i8* %0, i64 %1)
br label %23
23: ; preds = %15, %18, %13, %10
%24 = phi i8* [ %11, %10 ], [ null, %13 ], [ %16, %18 ], [ null, %15 ]
ret i8* %24
}