#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 */
int CSLCount(char **);
scalar_t__ malloc(int);
char *strdup(char *);
char **CSLDuplicate(char **papszStrList) {
  char **papszNewList, **papszSrc, **papszDst;
  int nLines;
  nLines = CSLCount(papszStrList);
  if (nLines == 0)
    return NULL;
  papszNewList = (char **)malloc((nLines + 1) * sizeof(char *));
  papszSrc = papszStrList;
  papszDst = papszNewList;
  while (*papszSrc != NULL) {
    *papszDst = strdup(*papszSrc);
    papszSrc++;
    papszDst++;
  }
  *papszDst = NULL;
  return papszNewList;
}


x86 assembly (clang, -O0):
<CSLDuplicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <CSLCount@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    115f <CSLDuplicate+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11da <CSLDuplicate+0xaa>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11c7 <CSLDuplicate+0x97>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    1186 <CSLDuplicate+0x56>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CSLDuplicate>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <CSLCount@plt>
test   %eax,%eax
je     117a <CSLDuplicate+0x4a>
lea    0x8(,%rax,8),%edi
call   1030 <malloc@plt>
mov    %rax,%r14
mov    (%r15),%rdi
test   %rdi,%rdi
je     117f <CSLDuplicate+0x4f>
add    $0x8,%r15
mov    %r14,%rbx
nop
call   1050 <strdup@plt>
mov    %rax,(%rbx)
add    $0x8,%rbx
mov    (%r15),%rdi
add    $0x8,%r15
test   %rdi,%rdi
jne    1160 <CSLDuplicate+0x30>
jmp    1182 <CSLDuplicate+0x52>
xor    %r14d,%r14d
jmp    1189 <CSLDuplicate+0x59>
mov    %r14,%rbx
movq   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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