#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(size_t, int);
char *strdup(char const *);
char **copy_string_array(const char **orig) {
  size_t num_entries = 0;
  for (const char **current = orig; *current; current++) {
    num_entries += 1;
  }
  char **copy = calloc(num_entries + 1, sizeof(char *));
  for (size_t i = 0; i < num_entries; i++) {
    copy[i] = strdup(orig[i]);
  }
  return copy;
}


x86 assembly (clang, -O0):
<copy_string_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1167 <copy_string_array+0x47>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    113c <copy_string_array+0x1c>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11c4 <copy_string_array+0xa4>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strdup@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1185 <copy_string_array+0x65>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_string_array>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmpq   $0x0,(%rdi)
je     1197 <copy_string_array+0x77>
mov    %rdi,%r14
mov    $0x1,%r12d
nopw   0x0(%rax,%rax,1)
cmpq   $0x0,(%r14,%r12,8)
lea    0x1(%r12),%r12
jne    1140 <copy_string_array+0x20>
mov    %r12,%rdi
mov    $0x8,%esi
call   1030 <calloc@plt>
mov    %rax,%r15
cmp    $0x1,%r12
je     1188 <copy_string_array+0x68>
add    $0xfffffffffffffffe,%r12
mov    $0xffffffffffffffff,%rbx
nopl   (%rax)
mov    0x8(%r14,%rbx,8),%rdi
call   1040 <strdup@plt>
mov    %rax,0x8(%r15,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1170 <copy_string_array+0x50>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    $0x1,%edi
mov    $0x8,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <calloc@plt>

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

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