#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(scalar_t__, int);
char **realloc(char **, int);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char **strv_copy(char **str_src) {
  char **str_dest = NULL;
  char *tmp = *str_src;
  int i = 0;
  while (tmp != NULL) {
    str_dest = realloc(str_dest, (i + 1) * sizeof(char *));
    str_dest[i] = calloc(strlen(tmp) + 1, sizeof(char));
    strcpy(str_dest[i], tmp);
    tmp = *(str_src + ++i);
  }
  str_dest = realloc(str_dest, (i + 1) * sizeof(char *));
  str_dest[i] = NULL;
  return str_dest;
}


x86 assembly (clang, -O0):
<strv_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e7 <strv_copy+0xa7>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
jmp    1166 <strv_copy+0x26>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strv_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%rbx
test   %rbx,%rbx
je     11b8 <strv_copy+0x78>
mov    %rdi,%r14
mov    $0x8,%ebp
xor    %r12d,%r12d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%r13
add    $0x1,%r12
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,(%r15,%r13,8)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x8(%r14,%r13,8),%rbx
add    $0x8,%ebp
test   %rbx,%rbx
jne    1170 <strv_copy+0x30>
jmp    11be <strv_copy+0x7e>
xor    %r15d,%r15d
xor    %r12d,%r12d
lea    0x8(,%r12,8),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %r12d,%ecx
movq   $0x0,(%rax,%rcx,8)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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