#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 *);
char **strbuf_copy(char **buf, size_t len) {
  char **copy;
  size_t i;
  copy = malloc(sizeof(char *) * (len + 1));
  if (copy == NULL)
    return NULL;
  for (i = 0; i < len; ++i)
    copy[i] = strdup(buf[i]);
  copy[len] = NULL;
  return copy;
}


x86 assembly (clang, -O0):
<strbuf_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    115f <strbuf_copy+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    11be <strbuf_copy+0x9e>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11a6 <strbuf_copy+0x86>
mov    -0x10(%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    1167 <strbuf_copy+0x47>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strbuf_copy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(,%rsi,8),%edi
call   1030 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     116e <strbuf_copy+0x4e>
test   %r15,%r15
je     1166 <strbuf_copy+0x46>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r14,%rbx,8),%rdi
call   1040 <strdup@plt>
mov    %rax,(%r12,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <strbuf_copy+0x30>
movq   $0x0,(%r12,%r15,8)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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