#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*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(char *, char, size_t);
scalar_t__ realloc(char *, size_t);
size_t __do_textcpy(char **p_dest, size_t sz_dest, const char *p_src,
                    size_t sz_src) {
  size_t cursiz = sz_dest;
  if (sz_src > sz_dest) {
    *p_dest = (char *)realloc(*p_dest, (sz_src + 1) * sizeof(char));
    cursiz = sz_src;
  }
  memset(*p_dest, '\0', (cursiz + 1) * sizeof(char));
  memcpy(*p_dest, p_src, (cursiz + 1) * sizeof(char));
  return sz_src;
}


x86 assembly (clang, -O0):
<__do_textcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    1188 <__do_textcpy+0x58>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
shl    $0x0,%rsi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
shl    $0x0,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
shl    $0x0,%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__do_textcpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rdi,%r12
mov    (%rdi),%rdi
cmp    %rsi,%rcx
jbe    115e <__do_textcpy+0x2e>
lea    0x1(%r15),%rsi
call   1050 <realloc@plt>
mov    %rax,%rdi
mov    %rax,(%r12)
mov    %r15,%rbx
jmp    1161 <__do_textcpy+0x31>
mov    %rsi,%rbx
add    $0x1,%rbx
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <memset@plt>
mov    (%r12),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @__do_textcpy(i8** %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load i64, i64* %6
store i64 %10, i64* %9
%11 = load i64, i64* %8
%12 = load i64, i64* %6
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %24
14: ; preds = %4
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
%17 = load i64, i64* %8
%18 = add i64 %17, 1
%19 = mul i64 %18, 1
%20 = call i64 @realloc(i8* %16, i64 %19)
%21 = inttoptr i64 %20 to i8*
%22 = load i8**, i8*** %5
store i8* %21, i8** %22
%23 = load i64, i64* %8
store i64 %23, i64* %9
br label %24
24: ; preds = %14, %4
%25 = load i8**, i8*** %5
%26 = load i8*, i8** %25
%27 = load i64, i64* %9
%28 = add i64 %27, 1
%29 = mul i64 %28, 1
%30 = call i32 @memset(i8* %26, i8 signext 0, i64 %29)
%31 = load i8**, i8*** %5
%32 = load i8*, i8** %31
%33 = load i8*, i8** %7
%34 = load i64, i64* %9
%35 = add i64 %34, 1
%36 = mul i64 %35, 1
%37 = call i32 @memcpy(i8* %32, i8* %33, i64 %36)
%38 = load i64, i64* %8
ret i64 %38
}

LLVM IR (-O2):
define i64 @__do_textcpy(i8** nocapture %0, i64 %1, i8* %2, i64 returned %3) {
%5 = icmp ugt i64 %3, %1
%6 = load i8*, i8** %0
br i1 %5, label %7, label %11
7: ; preds = %4
%8 = add i64 %3, 1
%9 = tail call i64 @realloc(i8* %6, i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %0
br label %11
11: ; preds = %4, %7
%12 = phi i8* [ %10, %7 ], [ %6, %4 ]
%13 = phi i64 [ %3, %7 ], [ %1, %4 ]
%14 = add i64 %13, 1
%15 = tail call i32 @memset(i8* %12, i8 signext 0, i64 %14)
%16 = load i8*, i8** %0
%17 = tail call i32 @memcpy(i8* %16, i8* %2, i64 %14)
ret i64 %3
}