#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 */
char *calloc(int, int);
int /*<<< orphan*/ dynstr_reset(char **);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
void dynstr_copy(char **this_ptr, const char *new_str, size_t max_size) {
  dynstr_reset(this_ptr);
  if (!new_str || !(*new_str) || (max_size < strlen(new_str)))
    return;
  (*this_ptr) = calloc(1, strlen(new_str) + 1);
  strcpy((*this_ptr), new_str);
}


x86 assembly (clang, -O0):
<dynstr_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <dynstr_reset@plt>
cmpq   $0x0,-0x10(%rbp)
je     1198 <dynstr_copy+0x58>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1198 <dynstr_copy+0x58>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    119d <dynstr_copy+0x5d>
jmp    11cf <dynstr_copy+0x8f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dynstr_copy>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
call   1060 <dynstr_reset@plt>
test   %rbx,%rbx
je     116c <dynstr_copy+0x2c>
cmpb   $0x0,(%rbx)
je     116c <dynstr_copy+0x2c>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmp    %r15,%rax
jbe    1172 <dynstr_copy+0x32>
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <strcpy@plt>

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

LLVM IR (-O2):
define void @dynstr_copy(i8** %0, i8* %1, i64 %2) {
%4 = tail call i32 @dynstr_reset(i8** %0)
%5 = icmp eq i8* %1, null
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = load i8, i8* %1
%8 = icmp eq i8 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %6
%10 = tail call i32 @strlen(i8* %1)
%11 = sext i32 %10 to i64
%12 = icmp ugt i64 %11, %2
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = tail call i32 @strlen(i8* %1)
%15 = add nsw i32 %14, 1
%16 = tail call i8* @calloc(i32 1, i32 %15)
store i8* %16, i8** %0
%17 = tail call i32 @strcpy(i8* %16, i8* %1)
br label %18
18: ; preds = %3, %6, %9, %13
ret void
}