#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 /*<<< orphan*/ memcpy(char *, char const *const, size_t);
size_t strlen(char const *const);
size_t arritostr(char *restrict buf, size_t bsz, size_t i,
                 const char *const *arr, size_t narr) {
  size_t ncp;
  size_t len;
  if (i > narr) {
    return 0;
  }
  len = strlen(arr[i]);
  ncp = bsz > len ? len : bsz;
  memcpy(buf, arr[i], ncp);
  return ncp;
}


x86 assembly (clang, -O0):
<arritostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1157 <arritostr+0x37>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <arritostr+0x98>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x40(%rbp),%rax
jbe    1187 <arritostr+0x67>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    118f <arritostr+0x6f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arritostr>:
push   %r15
push   %r14
push   %rbx
cmp    %r8,%rdx
jbe    112e <arritostr+0xe>
xor    %ebx,%ebx
jmp    1155 <arritostr+0x35>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rcx,%rdx,8),%r15
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    %rbx,%rax
cmovb  %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @arritostr(i8* noalias %0, i64 %1, i64 %2, i8** %3, i64 %4) {
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8**
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i8** %3, i8*** %10
store i64 %4, i64* %11
%14 = load i64, i64* %9
%15 = load i64, i64* %11
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %5
store i64 0, i64* %6
br label %41
18: ; preds = %5
%19 = load i8**, i8*** %10
%20 = load i64, i64* %9
%21 = getelementptr inbounds i8*, i8** %19, i64 %20
%22 = load i8*, i8** %21
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %13
%24 = load i64, i64* %8
%25 = load i64, i64* %13
%26 = icmp ugt i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = load i64, i64* %13
br label %31
29: ; preds = %18
%30 = load i64, i64* %8
br label %31
31: ; preds = %29, %27
%32 = phi i64 [ %28, %27 ], [ %30, %29 ]
store i64 %32, i64* %12
%33 = load i8*, i8** %7
%34 = load i8**, i8*** %10
%35 = load i64, i64* %9
%36 = getelementptr inbounds i8*, i8** %34, i64 %35
%37 = load i8*, i8** %36
%38 = load i64, i64* %12
%39 = call i32 @memcpy(i8* %33, i8* %37, i64 %38)
%40 = load i64, i64* %12
store i64 %40, i64* %6
br label %41
41: ; preds = %31, %17
%42 = load i64, i64* %6
ret i64 %42
}

LLVM IR (-O2):
define i64 @arritostr(i8* noalias %0, i64 %1, i64 %2, i8** nocapture %3, i64 %4) {
%6 = icmp ugt i64 %2, %4
br i1 %6, label %14, label %7
7: ; preds = %5
%8 = getelementptr inbounds i8*, i8** %3, i64 %2
%9 = load i8*, i8** %8
%10 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%11 = icmp ult i64 %10, %1
%12 = select i1 %11, i64 %10, i64 %1
%13 = tail call i32 @memcpy(i8* %0, i8* %9, i64 %12)
br label %14
14: ; preds = %5, %7
%15 = phi i64 [ %12, %7 ], [ 0, %5 ]
ret i64 %15
}