#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(int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t string_array_len(char const **);
size_t strlen(char const *);
char **clone_string_array(const char *array[]) {
  size_t i;
  size_t array_len = string_array_len(array);
  size_t string_len = 0;
  char **new_array = (char **)malloc(sizeof(char *) * (array_len + 1));
  new_array[array_len] = NULL;
  for (i = 0; i < array_len; i++) {
    string_len = strlen(array[i]);
    new_array[i] = (char *)malloc(sizeof(char) * (string_len + 1));
    memcpy(new_array[i], array[i], string_len);
    new_array[i][string_len] = '\0';
  }
  return new_array;
}


x86 assembly (clang, -O0):
<clone_string_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <string_array_len@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    121b <clone_string_array+0xdb>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1190 <clone_string_array+0x50>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clone_string_array>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
call   1050 <string_array_len@plt>
mov    %rax,%r14
lea    0x8(,%rax,8),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
movq   $0x0,(%rax,%r14,8)
test   %r14,%r14
je     11b9 <clone_string_array+0x79>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15,%rbx,8),%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,(%r12,%rbx,8)
mov    (%r15,%rbx,8),%rsi
mov    %rax,%rdi
mov    %r13,%rdx
call   1040 <memcpy@plt>
mov    (%r12,%rbx,8),%rax
movb   $0x0,(%rax,%r13,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1180 <clone_string_array+0x40>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8** @clone_string_array(i8** %0) {
%2 = tail call i64 @string_array_len(i8** %0)
%3 = trunc i64 %2 to i32
%4 = shl i32 %3, 3
%5 = add i32 %4, 8
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8**
%8 = getelementptr inbounds i8*, i8** %7, i64 %2
store i8* null, i8** %8
%9 = icmp eq i64 %2, 0
br i1 %9, label %26, label %10
10: ; preds = %1, %10
%11 = phi i64 [ %24, %10 ], [ 0, %1 ]
%12 = getelementptr inbounds i8*, i8** %0, i64 %11
%13 = load i8*, i8** %12
%14 = tail call i64 @strlen(i8* dereferenceable(1) %13)
%15 = trunc i64 %14 to i32
%16 = add i32 %15, 1
%17 = tail call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = getelementptr inbounds i8*, i8** %7, i64 %11
store i8* %18, i8** %19
%20 = load i8*, i8** %12
%21 = tail call i32 @memcpy(i8* %18, i8* %20, i64 %14)
%22 = load i8*, i8** %19
%23 = getelementptr inbounds i8, i8* %22, i64 %14
store i8 0, i8* %23
%24 = add nuw i64 %11, 1
%25 = icmp eq i64 %24, %2
br i1 %25, label %26, label %10
26: ; preds = %10, %1
ret i8** %7
}