#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 **malloc(int);
char *strdup(char const *);
char **str_array_clone(const char **array) {
  int len = 0;
  while (array[len])
    ++len;
  char **new_array = malloc(sizeof(char *) * (len + 1));
  int i;
  for (i = 0; i < len; ++i)
    new_array[i] = strdup(array[i]);
  new_array[len] = NULL;
  return new_array;
}


x86 assembly (clang, -O0):
<str_array_clone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1154 <str_array_clone+0x34>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1133 <str_array_clone+0x13>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ac <str_array_clone+0x8c>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strdup@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1172 <str_array_clone+0x52>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_array_clone>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
mov    $0x1,%r13d
xor    %edi,%edi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xffffffffffffffff,%r13
cmpq   $0x0,(%r15,%rdi,1)
lea    0x8(%rdi),%rdi
jne    1140 <str_array_clone+0x20>
call   1030 <malloc@plt>
mov    %rax,%r14
mov    %r13d,%r12d
neg    %r12d
jae    1186 <str_array_clone+0x66>
neg    %r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15,%rbx,8),%rdi
call   1040 <strdup@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1170 <str_array_clone+0x50>
movq   $0x0,(%r14,%r12,8)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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