#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 escape_json(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
char *escape_json_ext(char *buf, char **dst) {
  int size;
  char *tmp = NULL;
  size = escape_json(buf, NULL);
  if (size != 0) {
    if (*dst != NULL) {
      free(*dst);
    }
    tmp = (char *)malloc(size);
    escape_json(buf, (char *)tmp);
    *dst = tmp;
    return tmp;
  } else {
    return buf;
  }
}


x86 assembly (clang, -O0):
<escape_json_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <escape_json@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ad <escape_json_ext+0x7d>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     117c <escape_json_ext+0x4c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <escape_json@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <escape_json_ext+0x85>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_json_ext>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %esi,%esi
call   1050 <escape_json@plt>
test   %eax,%eax
je     116f <escape_json_ext+0x3f>
mov    %eax,%ebx
mov    (%r14),%rdi
test   %rdi,%rdi
je     1154 <escape_json_ext+0x24>
call   1030 <free@plt>
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1050 <escape_json@plt>
mov    %rbx,(%r14)
mov    %rbx,%rbp
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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