#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);
void *resize_array(int);
int /*<<< orphan*/ resize_array_add(void *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void *populate(int init, int num) {
  char *buf;
  void *array = resize_array(init);
  for (int i = 0; i < num; i++) {
    buf = (char *)malloc(9);
    sprintf(buf, "string%d", i);
    resize_array_add(array, buf);
  }
  return array;
}


x86 assembly (clang, -O0):
<populate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <resize_array@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11a9 <populate+0x69>
mov    $0x9,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe77(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <resize_array_add@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1161 <populate+0x21>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<populate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
call   1030 <resize_array@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    119d <populate+0x5d>
xor    %ebp,%ebp
lea    0xe9f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x9,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <resize_array_add@plt>
add    $0x1,%ebp
cmp    %ebp,%r14d
jne    1170 <populate+0x30>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"string%d\00"
define i8* @populate(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = call i8* @resize_array(i32 %8)
store i8* %9, i8** %6
store i32 0, i32* %7
br label %10
10: ; preds = %23, %2
%11 = load i32, i32* %7
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = call i64 @malloc(i32 9)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %6
%21 = load i8*, i8** %5
%22 = call i32 @resize_array_add(i8* %20, i8* %21)
br label %23
23: ; preds = %14
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
br label %10
26: ; preds = %10
%27 = load i8*, i8** %6
ret i8* %27
}

LLVM IR (-O2):
@.str = [9 x i8] c"string%d\00"
define i8* @populate(i32 %0, i32 %1) {
%3 = tail call i8* @resize_array(i32 %0)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %6, label %5
5: ; preds = %6, %2
ret i8* %3
6: ; preds = %2, %6
%7 = phi i32 [ %12, %6 ], [ 0, %2 ]
%8 = tail call i64 @malloc(i32 9)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%11 = tail call i32 @resize_array_add(i8* %3, i8* %9)
%12 = add nuw nsw i32 %7, 1
%13 = icmp eq i32 %12, %1
br i1 %13, label %5, label %6
}