#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 TheDebuginator {
  int dummy;
};

/* Variables and functions */
scalar_t__ debuginator__allocate(struct TheDebuginator *, int);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
scalar_t__ strlen(char const *);
char *debuginator_copy_string(struct TheDebuginator *debuginator,
                              const char *string, int length) {
  if (length == 0) {
    length = (int)strlen(string);
  }
  char *memory = (char *)debuginator__allocate(debuginator, length + 1);
  memcpy(memory, string, (unsigned int)length);
  memory[length] = '\0';
  return memory;
}


x86 assembly (clang, -O0):
<debuginator_copy_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1159 <debuginator_copy_string+0x29>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1030 <debuginator__allocate@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debuginator_copy_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
test   %edx,%edx
jne    1150 <debuginator_copy_string+0x20>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
lea    0x1(%r12),%esi
mov    %r15,%rdi
call   1030 <debuginator__allocate@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
movslq %r12d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TheDebuginator = type { i32 }
define i8* @debuginator_copy_string(%struct.TheDebuginator* %0, i8* %1, i32 %2) {
%4 = alloca %struct.TheDebuginator*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store %struct.TheDebuginator* %0, %struct.TheDebuginator** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %6
br label %14
14: ; preds = %10, %3
%15 = load %struct.TheDebuginator*, %struct.TheDebuginator** %4
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
%18 = call i64 @debuginator__allocate(%struct.TheDebuginator* %15, i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = call i32 @memcpy(i8* %20, i8* %21, i32 %22)
%24 = load i8*, i8** %7
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %7
ret i8* %28
}

LLVM IR (-O2):
%struct.TheDebuginator = type { i32 }
define i8* @debuginator_copy_string(%struct.TheDebuginator* %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%7 = trunc i64 %6 to i32
br label %8
8: ; preds = %5, %3
%9 = phi i32 [ %7, %5 ], [ %2, %3 ]
%10 = add nsw i32 %9, 1
%11 = tail call i64 @debuginator__allocate(%struct.TheDebuginator* %0, i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @memcpy(i8* %12, i8* %1, i32 %9)
%14 = sext i32 %9 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8 0, i8* %15
ret i8* %12
}