#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 */
scalar_t__ malloc(unsigned int);
unsigned char *deep_copy_string(unsigned char *str) {
  unsigned int i = 0;
  unsigned int num_chars = 0;
  unsigned char *c = str;
  unsigned char *rtn;
  while (*c) {
    c++;
    num_chars++;
  }
  num_chars++;
  rtn = (unsigned char *)malloc(num_chars * sizeof(unsigned char));
  c = str;
  while (*c) {
    rtn[i] = *c;
    i++;
    c++;
  }
  rtn[i] = '\0';
  return rtn;
}


x86 assembly (clang, -O0):
<deep_copy_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1159 <deep_copy_string+0x49>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1132 <deep_copy_string+0x22>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11b3 <deep_copy_string+0xa3>
mov    -0x18(%rbp),%rax
mov    (%rax),%dl
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    117c <deep_copy_string+0x6c>
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deep_copy_string>:
push   %rbx
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     112c <deep_copy_string+0x1c>
mov    $0x1,%edi
xchg   %ax,%ax
cmpb   $0x0,(%rbx,%rdi,1)
lea    0x1(%rdi),%rdi
jne    1120 <deep_copy_string+0x10>
jmp    1131 <deep_copy_string+0x21>
mov    $0x1,%edi
call   1030 <malloc@plt>
mov    (%rbx),%cl
test   %cl,%cl
je     1156 <deep_copy_string+0x46>
xor    %edx,%edx
xchg   %ax,%ax
mov    %edx,%esi
mov    %cl,(%rax,%rsi,1)
movzbl 0x1(%rbx,%rdx,1),%ecx
add    $0x1,%rdx
test   %cl,%cl
jne    1140 <deep_copy_string+0x30>
mov    %edx,%ecx
jmp    1158 <deep_copy_string+0x48>
xor    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
ret

LLVM IR (-O0):
define i8* @deep_copy_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
%7 = load i8*, i8** %2
store i8* %7, i8** %5
br label %8
8: ; preds = %12, %1
%9 = load i8*, i8** %5
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %5
%15 = load i32, i32* %4
%16 = add i32 %15, 1
store i32 %16, i32* %4
br label %8
17: ; preds = %8
%18 = load i32, i32* %4
%19 = add i32 %18, 1
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = zext i32 %20 to i64
%22 = mul i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %6
%26 = load i8*, i8** %2
store i8* %26, i8** %5
br label %27
27: ; preds = %31, %17
%28 = load i8*, i8** %5
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = load i8, i8* %32
%34 = load i8*, i8** %6
%35 = load i32, i32* %3
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
%38 = load i32, i32* %3
%39 = add i32 %38, 1
store i32 %39, i32* %3
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %5
br label %27
42: ; preds = %27
%43 = load i8*, i8** %6
%44 = load i32, i32* %3
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %6
ret i8* %47
}

LLVM IR (-O2):
define i8* @deep_copy_string(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %11, label %4
4: ; preds = %1, %4
%5 = phi i8* [ %7, %4 ], [ %0, %1 ]
%6 = phi i32 [ %8, %4 ], [ 1, %1 ]
%7 = getelementptr inbounds i8, i8* %5, i64 1
%8 = add i32 %6, 1
%9 = load i8, i8* %7
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %4
11: ; preds = %4, %1
%12 = phi i32 [ 1, %1 ], [ %8, %4 ]
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %11, %17
%18 = phi i8 [ %25, %17 ], [ %15, %11 ]
%19 = phi i32 [ %23, %17 ], [ 0, %11 ]
%20 = phi i8* [ %24, %17 ], [ %0, %11 ]
%21 = zext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %14, i64 %21
store i8 %18, i8* %22
%23 = add i32 %19, 1
%24 = getelementptr inbounds i8, i8* %20, i64 1
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %17
27: ; preds = %17
%28 = zext i32 %23 to i64
br label %29
29: ; preds = %27, %11
%30 = phi i64 [ 0, %11 ], [ %28, %27 ]
%31 = getelementptr inbounds i8, i8* %14, i64 %30
store i8 0, i8* %31
ret i8* %14
}