#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*/ lock();
void *malloc(size_t);
void *realloc_internal(void *, size_t);
int /*<<< orphan*/ unlock();
void *realloc(void *ptr, size_t n) {
  if (n == 0) {
    free(ptr);
    return NULL;
  } else if (ptr == NULL)
    return malloc(n);
  else {
    lock();
    void *result = realloc_internal(ptr, n);
    unlock();
    return result;
  }
}


x86 assembly (clang, -O0):
<realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <realloc+0x31>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <realloc+0x75>
cmpq   $0x0,-0x10(%rbp)
jne    119e <realloc+0x4e>
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11c5 <realloc+0x75>
mov    $0x0,%al
call   1050 <lock@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc_internal@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <unlock@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rsi,%rsi
je     1185 <realloc+0x35>
mov    %rsi,%rbx
test   %r14,%r14
je     1197 <realloc+0x47>
xor    %eax,%eax
call   1050 <lock@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc_internal@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <unlock@plt>
mov    %rbx,%rax
jmp    118f <realloc+0x3f>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <malloc@plt>

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

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