#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 */
void *domalloc(size_t);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *dorealloc(void *op, size_t osz, size_t nsz) {
  void *np;
  size_t copysz;
  np = domalloc(nsz);
  if (op != NULL) {
    copysz = osz < nsz ? osz : nsz;
    memcpy(np, op, copysz);
    free(op);
  }
  return np;
}


x86 assembly (clang, -O0):
<dorealloc>:
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   1040 <domalloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a1 <dorealloc+0x71>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1177 <dorealloc+0x47>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    117f <dorealloc+0x4f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dorealloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rdx,%rdi
call   1040 <domalloc@plt>
mov    %rax,%r14
test   %r15,%r15
je     116e <dorealloc+0x3e>
cmp    %rbx,%r12
cmovb  %r12,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @dorealloc(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* @domalloc(i64 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %29
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
br label %21
19: ; preds = %13
%20 = load i64, i64* %6
br label %21
21: ; preds = %19, %17
%22 = phi i64 [ %18, %17 ], [ %20, %19 ]
store i64 %22, i64* %8
%23 = load i8*, i8** %7
%24 = load i8*, i8** %4
%25 = load i64, i64* %8
%26 = call i32 @memcpy(i8* %23, i8* %24, i64 %25)
%27 = load i8*, i8** %4
%28 = call i32 @free(i8* %27)
br label %29
29: ; preds = %21, %3
%30 = load i8*, i8** %7
ret i8* %30
}

LLVM IR (-O2):
define i8* @dorealloc(i8* %0, i64 %1, i64 %2) {
%4 = tail call i8* @domalloc(i64 %2)
%5 = icmp eq i8* %0, null
br i1 %5, label %11, 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)
%10 = tail call i32 @free(i8* %0)
br label %11
11: ; preds = %6, %3
ret i8* %4
}