#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 *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *realloc(void *, size_t);
void *incmem(void *m, size_t x, size_t nx) {
  void *nm;
  if (!(nm = realloc(m, nx))) {
    if (x > nx)
      x = nx;
    nm = malloc(nx);
    memcpy(nm, m, x);
    free(m);
  }
  return nm;
}


x86 assembly (clang, -O0):
<incmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ac <incmem+0x6c>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1185 <incmem+0x45>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<incmem>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rsi
call   1060 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1189 <incmem+0x49>
cmp    %r12,%r14
cmova  %r12,%r14
mov    %r12,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @incmem(i8* %0, i64 %1, i64 %2) {
%4 = tail call i8* @realloc(i8* %0, i64 %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = icmp ugt i64 %1, %2
%8 = select i1 %7, i64 %2, i64 %1
%9 = tail call i8* @malloc(i64 %2)
%10 = tail call i32 @memcpy(i8* %9, i8* %0, i64 %8)
%11 = tail call i32 @free(i8* %0)
br label %12
12: ; preds = %6, %3
%13 = phi i8* [ %4, %3 ], [ %9, %6 ]
ret i8* %13
}