#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 *);
int /*<<< orphan*/ kpfatal(char *, int);
void *malloc(int);
int /*<<< orphan*/ memcpy(void *, void *, int);
void *kp_realloc(void *p, int oldsz, int newsz) {
  void *p2;
  p2 = malloc(newsz);
  if (p2 == NULL)
    kpfatal("failed to allocate %d bytes", newsz);
  if (p) {
    int sz = (oldsz > newsz) ? newsz : oldsz;
    memcpy(p2, p, sz);
    free(p);
  }
  return p2;
}


x86 assembly (clang, -O0):
<kp_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1178 <kp_realloc+0x38>
mov    -0x10(%rbp),%esi
lea    0xe8d(%rip),%rdi
call   1040 <kpfatal@plt>
cmpq   $0x0,-0x8(%rbp)
je     11bf <kp_realloc+0x7f>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    119a <kp_realloc+0x5a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11a0 <kp_realloc+0x60>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kp_realloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r14
mov    %edx,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     118e <kp_realloc+0x4e>
test   %r14,%r14
je     1180 <kp_realloc+0x40>
cmp    %ebp,%r15d
cmovg  %ebp,%r15d
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6b(%rip),%rdi
mov    %ebp,%esi
call   1040 <kpfatal@plt>
test   %r14,%r14
jne    1163 <kp_realloc+0x23>
jmp    1180 <kp_realloc+0x40>

LLVM IR (-O0):
@.str = [28 x i8] c"failed to allocate %d bytes\00"
define i8* @kp_realloc(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* %6
%15 = call i32 @kpfatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %13, %3
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* %6
br label %27
25: ; preds = %19
%26 = load i32, i32* %5
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
store i32 %28, i32* %8
%29 = load i8*, i8** %7
%30 = load i8*, i8** %4
%31 = load i32, i32* %8
%32 = call i32 @memcpy(i8* %29, i8* %30, i32 %31)
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %27, %16
%36 = load i8*, i8** %7
ret i8* %36
}

LLVM IR (-O2):
@.str = [28 x i8] c"failed to allocate %d bytes\00"
define i8* @kp_realloc(i8* %0, i32 %1, i32 %2) {
%4 = tail call i8* @malloc(i32 %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @kpfatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %8
8: ; preds = %6, %3
%9 = icmp eq i8* %0, null
br i1 %9, label %15, label %10
10: ; preds = %8
%11 = icmp sgt i32 %1, %2
%12 = select i1 %11, i32 %2, i32 %1
%13 = tail call i32 @memcpy(i8* %4, i8* %0, i32 %12)
%14 = tail call i32 @free(i8* %0)
br label %15
15: ; preds = %10, %8
ret i8* %4
}