#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 */
size_t strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, unsigned int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *atostr(char *str, char **arr, const size_t num) {
  unsigned int a = 0, lsz = 512;
  if (num == 1) {
    strncpy(str, arr[0], 512);
    return str;
  }
  for (a = 0; a < num; a++) {
    strncat(str, arr[a], lsz);
    str[strlen(str)] = ' ';
    lsz -= (strlen(arr[a]) + 1);
  }
  str[(strlen(str) - 1)] = 0;
  return str;
}


x86 assembly (clang, -O0):
<atostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x200,-0x28(%rbp)
cmpq   $0x1,-0x20(%rbp)
jne    117f <atostr+0x4f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121c <atostr+0xec>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%rax
jae    11f4 <atostr+0xc4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,8),%rsi
mov    -0x28(%rbp),%edx
call   1050 <strncat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
movb   $0x20,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
mov    -0x28(%rbp),%eax
sub    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1186 <atostr+0x56>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atostr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r12
test   %rdx,%rdx
je     11a2 <atostr+0x72>
mov    %rdx,%r15
mov    %rsi,%r14
cmp    $0x1,%rdx
jne    115e <atostr+0x2e>
mov    (%r14),%rsi
mov    %r12,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
jmp    11b0 <atostr+0x80>
mov    $0x200,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14,%rbx,8),%rsi
mov    %r12,%rdi
mov    %ebp,%edx
call   1050 <strncat@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
movb   $0x20,(%r12,%rax,1)
mov    (%r14,%rbx,8),%rdi
call   1040 <strlen@plt>
not    %eax
add    %ebp,%eax
add    $0x1,%ebx
mov    %eax,%ebp
cmp    %r15,%rbx
jb     1170 <atostr+0x40>
mov    %r12,%rdi
call   1040 <strlen@plt>
movb   $0x0,-0x1(%rax,%r12,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @atostr(i8* %0, i8** %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i32 512, i32* %9
%10 = load i64, i64* %7
%11 = icmp eq i64 %10, 1
br i1 %11, label %12, label %19
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i8**, i8*** %6
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @strncpy(i8* %13, i8* %16, i32 512)
%18 = load i8*, i8** %5
store i8* %18, i8** %4
br label %59
19: ; preds = %3
store i32 0, i32* %8
br label %20
20: ; preds = %49, %19
%21 = load i32, i32* %8
%22 = zext i32 %21 to i64
%23 = load i64, i64* %7
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %52
25: ; preds = %20
%26 = load i8*, i8** %5
%27 = load i8**, i8*** %6
%28 = load i32, i32* %8
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = load i32, i32* %9
%33 = call i32 @strncat(i8* %26, i8* %31, i32 %32)
%34 = load i8*, i8** %5
%35 = load i8*, i8** %5
%36 = call i64 @strlen(i8* %35)
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 32, i8* %37
%38 = load i8**, i8*** %6
%39 = load i32, i32* %8
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
%43 = call i64 @strlen(i8* %42)
%44 = add i64 %43, 1
%45 = load i32, i32* %9
%46 = zext i32 %45 to i64
%47 = sub i64 %46, %44
%48 = trunc i64 %47 to i32
store i32 %48, i32* %9
br label %49
49: ; preds = %25
%50 = load i32, i32* %8
%51 = add i32 %50, 1
store i32 %51, i32* %8
br label %20
52: ; preds = %20
%53 = load i8*, i8** %5
%54 = load i8*, i8** %5
%55 = call i64 @strlen(i8* %54)
%56 = sub i64 %55, 1
%57 = getelementptr inbounds i8, i8* %53, i64 %56
store i8 0, i8* %57
%58 = load i8*, i8** %5
store i8* %58, i8** %4
br label %59
59: ; preds = %52, %12
%60 = load i8*, i8** %4
ret i8* %60
}

LLVM IR (-O2):
define i8* @atostr(i8* returned %0, i8** nocapture %1, i64 %2) {
switch i64 %2, label %7 [
i64 1, label %4
i64 0, label %23
]
4: ; preds = %3
%5 = load i8*, i8** %1
%6 = tail call i32 @strncpy(i8* %0, i8* %5, i32 512)
br label %27
7: ; preds = %3, %7
%8 = phi i64 [ %21, %7 ], [ 0, %3 ]
%9 = phi i32 [ %19, %7 ], [ 512, %3 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %8
%11 = load i8*, i8** %10
%12 = tail call i32 @strncat(i8* %0, i8* %11, i32 %9)
%13 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%14 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 32, i8* %14
%15 = load i8*, i8** %10
%16 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%17 = trunc i64 %16 to i32
%18 = xor i32 %17, -1
%19 = add i32 %9, %18
%20 = add nuw nsw i64 %8, 1
%21 = and i64 %20, 4294967295
%22 = icmp ult i64 %21, %2
br i1 %22, label %7, label %23
23: ; preds = %7, %3
%24 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%25 = add i64 %24, -1
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %23, %4
ret i8* %0
}