#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(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void test_append(char *ary[], int len, char *astr) {
  int i;
  int size1, size2;
  char *str;
  size2 = strlen(astr);
  for (i = 0; i <= len - 1; i++) {
    size1 = strlen(ary[i]);
    str = (char *)malloc(size1 + size2 + 1);
    strcpy(str, ary[i]);
    strcat(str, astr);
    ary[i] = str;
  };
}


x86 assembly (clang, -O0):
<test_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     11dd <test_append+0x9d>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
add    -0x24(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x30(%rbp),%rdx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <test_append+0x26>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r13
mov    %rdx,%rdi
call   1040 <strlen@plt>
test   %ebp,%ebp
jle    11ac <test_append+0x6c>
mov    %eax,%r15d
add    $0x1,%r15d
mov    %ebp,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13,%rbx,8),%rdi
call   1040 <strlen@plt>
lea    (%rax,%r15,1),%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    0x0(%r13,%rbx,8),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbp,0x0(%r13,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1170 <test_append+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @test_append(i8** nocapture %0, i32 %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %22
6: ; preds = %3
%7 = add i32 %4, 1
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %6, %9
%10 = phi i64 [ 0, %6 ], [ %20, %9 ]
%11 = getelementptr inbounds i8*, i8** %0, i64 %10
%12 = load i8*, i8** %11
%13 = tail call i32 @strlen(i8* %12)
%14 = add i32 %7, %13
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = load i8*, i8** %11
%18 = tail call i32 @strcpy(i8* %16, i8* %17)
%19 = tail call i32 @strcat(i8* %16, i8* %2)
store i8* %16, i8** %11
%20 = add nuw nsw i64 %10, 1
%21 = icmp eq i64 %20, %8
br i1 %21, label %22, label %9
22: ; preds = %9, %3
ret void
}