#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_multiply(int, scalar_t__);
char *strdup(char *);
int /*<<< orphan*/ strv_free(char **);
scalar_t__ strv_length(char **);
char **strv_copy(char **l) {
  char **r, **k;
  k = r = ((char **)malloc_multiply(sizeof(char *), (strv_length(l) + 1)));
  if (!r)
    return NULL;
  if (l)
    for (; *l; k++, l++) {
      *k = strdup(*l);
      if (!*k) {
        strv_free(r);
        return NULL;
      }
    }
  *k = NULL;
  return r;
}


x86 assembly (clang, -O0):
<strv_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strv_length@plt>
mov    %rax,%rsi
add    $0x1,%rsi
mov    $0x8,%edi
call   1050 <malloc_multiply@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1186 <strv_copy+0x46>
movq   $0x0,-0x8(%rbp)
jmp    1218 <strv_copy+0xd8>
cmpq   $0x0,-0x10(%rbp)
je     1205 <strv_copy+0xc5>
jmp    1196 <strv_copy+0x56>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1200 <strv_copy+0xc0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11de <strv_copy+0x9e>
mov    -0x18(%rbp),%rdi
call   1030 <strv_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1218 <strv_copy+0xd8>
jmp    11e3 <strv_copy+0xa3>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    1196 <strv_copy+0x56>
jmp    1205 <strv_copy+0xc5>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strv_copy>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strv_length@plt>
lea    0x1(%rax),%rsi
mov    $0x8,%edi
call   1050 <malloc_multiply@plt>
test   %rax,%rax
je     11b8 <strv_copy+0x78>
mov    %rax,%r14
test   %r15,%r15
je     11a4 <strv_copy+0x64>
mov    (%r15),%rdi
test   %rdi,%rdi
je     11a4 <strv_copy+0x64>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1060 <strdup@plt>
mov    %rax,(%r14,%rbx,1)
test   %rax,%rax
je     11b0 <strv_copy+0x70>
mov    0x8(%r15,%rbx,1),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    1180 <strv_copy+0x40>
mov    %r14,%rax
add    %rbx,%rax
jmp    11a7 <strv_copy+0x67>
mov    %r14,%rax
movq   $0x0,(%rax)
jmp    11bb <strv_copy+0x7b>
mov    %r14,%rdi
call   1030 <strv_free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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