#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 */
struct arena {
  int dummy;
};

/* Variables and functions */
void *arena_malloc(struct arena *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *const, size_t);
size_t strlen(char const *const);
char **dup_argv(int argc, const char *const *argv, struct arena *arena) {
  char **pointer_list = arena_malloc(arena, (argc + 1) * sizeof(char *));
  char **next_ptr = pointer_list;
  for (int i = 0; i < argc; ++i) {
    size_t len = strlen(argv[i]);
    char *str = arena_malloc(arena, len + 1);
    memcpy(str, argv[i], len + 1);
    *next_ptr = str;
    ++next_ptr;
  }
  *next_ptr = NULL;
  return pointer_list;
}


x86 assembly (clang, -O0):
<dup_argv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rsi
shl    $0x3,%rsi
call   1050 <arena_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11e4 <dup_argv+0xb4>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    $0x1,%rsi
call   1050 <arena_malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x38(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    116c <dup_argv+0x3c>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup_argv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
movslq %edi,%rbx
lea    0x8(,%rbx,8),%rsi
mov    %rdx,%rdi
call   1050 <arena_malloc@plt>
mov    %rax,%r13
test   %ebx,%ebx
jle    11b1 <dup_argv+0x81>
mov    %ebp,%ecx
shl    $0x3,%rcx
mov    %rcx,(%rsp)
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    (%r12,%r14,1),%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rbp
mov    %r15,%rdi
mov    %rbp,%rsi
call   1050 <arena_malloc@plt>
mov    %rax,%rbx
mov    (%r12,%r14,1),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
call   1040 <memcpy@plt>
mov    %rbx,0x0(%r13,%r14,1)
add    $0x8,%r14
cmp    %r14,(%rsp)
jne    1170 <dup_argv+0x40>
mov    %r13,%rcx
add    %r14,%rcx
jmp    11b4 <dup_argv+0x84>
mov    %r13,%rcx
movq   $0x0,(%rcx)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.arena = type { i32 }
define i8** @dup_argv(i32 %0, i8** nocapture %1, %struct.arena* %2) {
%4 = add nsw i32 %0, 1
%5 = sext i32 %4 to i64
%6 = shl nsw i64 %5, 3
%7 = tail call i8* @arena_malloc(%struct.arena* %2, i64 %6)
%8 = bitcast i8* %7 to i8**
%9 = icmp sgt i32 %0, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = zext i32 %0 to i64
br label %14
12: ; preds = %14, %3
%13 = phi i8** [ %8, %3 ], [ %24, %14 ]
store i8* null, i8** %13
ret i8** %8
14: ; preds = %10, %14
%15 = phi i64 [ 0, %10 ], [ %25, %14 ]
%16 = phi i8** [ %8, %10 ], [ %24, %14 ]
%17 = getelementptr inbounds i8*, i8** %1, i64 %15
%18 = load i8*, i8** %17
%19 = tail call i64 @strlen(i8* dereferenceable(1) %18)
%20 = add i64 %19, 1
%21 = tail call i8* @arena_malloc(%struct.arena* %2, i64 %20)
%22 = load i8*, i8** %17
%23 = tail call i32 @memcpy(i8* %21, i8* %22, i64 %20)
store i8* %21, i8** %16
%24 = getelementptr inbounds i8*, i8** %16, i64 1
%25 = add nuw nsw i64 %15, 1
%26 = icmp eq i64 %25, %11
br i1 %26, label %12, label %14
}