#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_exec(void *);
void *malloc_exec(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *realloc_exec(void *ptr, size_t size, size_t newsize) {
  void *block = malloc_exec(newsize);
  if (block != NULL) {
    size_t copysize;
    if (size < newsize)
      copysize = size;
    else
      copysize = newsize;
    memcpy(block, ptr, copysize);
  }
  free_exec(ptr);
  return block;
}


x86 assembly (clang, -O0):
<realloc_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <malloc_exec@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1190 <realloc_exec+0x60>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1177 <realloc_exec+0x47>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    117f <realloc_exec+0x4f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1050 <free_exec@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc_exec>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rdi
call   1030 <malloc_exec@plt>
mov    %rax,%r15
test   %rax,%rax
je     1166 <realloc_exec+0x36>
cmp    %rbx,%r12
cmovb  %r12,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1050 <free_exec@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @realloc_exec(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = call i8* @malloc_exec(i64 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %26
13: ; preds = %3
%14 = load i64, i64* %5
%15 = load i64, i64* %6
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i64, i64* %5
store i64 %18, i64* %8
br label %21
19: ; preds = %13
%20 = load i64, i64* %6
store i64 %20, i64* %8
br label %21
21: ; preds = %19, %17
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = load i64, i64* %8
%25 = call i32 @memcpy(i8* %22, i8* %23, i64 %24)
br label %26
26: ; preds = %21, %3
%27 = load i8*, i8** %4
%28 = call i32 @free_exec(i8* %27)
%29 = load i8*, i8** %7
ret i8* %29
}

LLVM IR (-O2):
define i8* @realloc_exec(i8* %0, i64 %1, i64 %2) {
%4 = tail call i8* @malloc_exec(i64 %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = icmp ult i64 %1, %2
%8 = select i1 %7, i64 %1, i64 %2
%9 = tail call i32 @memcpy(i8* %4, i8* %0, i64 %8)
br label %10
10: ; preds = %6, %3
%11 = tail call i32 @free_exec(i8* %0)
ret i8* %4
}