#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 */
struct xmalloc_stat {
  int dummy;
};

/* Variables and functions */
void *realloc(void *, size_t);
int /*<<< orphan*/ xmalloc_stat_dec(struct xmalloc_stat *);
int /*<<< orphan*/ xmalloc_stat_inc(struct xmalloc_stat *);
void *xrealloc(void *ptr, size_t size, struct xmalloc_stat *st) {
  void *nptr = realloc(ptr, size);
  if (ptr == NULL) {
    if (nptr)
      xmalloc_stat_inc(st);
  } else if (size == 0) {
    xmalloc_stat_dec(st);
  }
  return nptr;
}


x86 assembly (clang, -O0):
<xrealloc>:
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    -0x10(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1179 <xrealloc+0x49>
cmpq   $0x0,-0x20(%rbp)
je     1174 <xrealloc+0x44>
mov    -0x18(%rbp),%rdi
call   1050 <xmalloc_stat_inc@plt>
jmp    1192 <xrealloc+0x62>
cmpq   $0x0,-0x10(%rbp)
jne    118d <xrealloc+0x5d>
mov    -0x18(%rbp),%rdi
call   1030 <xmalloc_stat_dec@plt>
jmp    1192 <xrealloc+0x62>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xrealloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbx
call   1040 <realloc@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     115d <xrealloc+0x2d>
test   %r12,%r12
jne    116a <xrealloc+0x3a>
mov    %r14,%rdi
call   1030 <xmalloc_stat_dec@plt>
jmp    116a <xrealloc+0x3a>
test   %r15,%r15
je     116a <xrealloc+0x3a>
mov    %r14,%rdi
call   1050 <xmalloc_stat_inc@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.xmalloc_stat = type { i32 }
define noalias i8* @xrealloc(i8* %0, i64 %1, %struct.xmalloc_stat* %2) {
%4 = tail call i8* @realloc(i8* %0, i64 %1)
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = icmp eq i8* %4, null
br i1 %7, label %14, label %8
8: ; preds = %6
%9 = tail call i32 @xmalloc_stat_inc(%struct.xmalloc_stat* %2)
br label %14
10: ; preds = %3
%11 = icmp eq i64 %1, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @xmalloc_stat_dec(%struct.xmalloc_stat* %2)
br label %14
14: ; preds = %10, %12, %6, %8
ret i8* %4
}