#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 random();
int strlen(char *);
int randstr(char *str, const size_t len) {
  char charset[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  int cslen = strlen(charset);
  unsigned int a = 0;
  for (a = 0; a < len; a++)
    str[a] = charset[(random() % cslen)];
  str[len] = 0;
  return a;
}


x86 assembly (clang, -O0):
<randstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xeb5(%rip),%rsi
mov    $0x25,%edx
call   1040 <memcpy@plt>
lea    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x10(%rbp),%rax
jae    11a6 <randstr+0x76>
mov    $0x0,%al
call   1050 <random@plt>
cltd
idivl  -0x44(%rbp)
movslq %edx,%rax
mov    -0x40(%rbp,%rax,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x48(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    116f <randstr+0x3f>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randstr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
movaps 0xed9(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xebd(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x7a797877767574,%rax
mov    %rax,0x1d(%rsp)
mov    %rsp,%rdi
call   1030 <strlen@plt>
test   %r15,%r15
je     118f <randstr+0x6f>
mov    %eax,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <random@plt>
cltd
idiv   %ebp
movslq %edx,%rax
movzbl (%rsp,%rax,1),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%ebx
cmp    %r15,%rbx
jb     1170 <randstr+0x50>
jmp    1191 <randstr+0x71>
xor    %ebx,%ebx
movb   $0x0,(%r14,%r15,1)
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.randstr.charset = [37 x i8] c"0123456789abcdefghijklmnopqrstuvwxyz\00"
define i32 @randstr(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [37 x i8]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = bitcast [37 x i8]* %5 to i8*
%9 = getelementptr inbounds [37 x i8], [37 x i8]* %5, i64 0, i64 0
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %6
store i32 0, i32* %7
store i32 0, i32* %7
br label %11
11: ; preds = %27, %2
%12 = load i32, i32* %7
%13 = zext i32 %12 to i64
%14 = load i64, i64* %4
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %11
%17 = call i32 (...) @random()
%18 = load i32, i32* %6
%19 = srem i32 %17, %18
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [37 x i8], [37 x i8]* %5, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %3
%24 = load i32, i32* %7
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
br label %27
27: ; preds = %16
%28 = load i32, i32* %7
%29 = add i32 %28, 1
store i32 %29, i32* %7
br label %11
30: ; preds = %11
%31 = load i8*, i8** %3
%32 = load i64, i64* %4
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* %7
ret i32 %34
}

LLVM IR (-O2):
@__const.randstr.charset = [37 x i8] c"0123456789abcdefghijklmnopqrstuvwxyz\00"
define i32 @randstr(i8* nocapture writeonly %0, i64 %1) {
%3 = alloca [37 x i8]
%4 = getelementptr inbounds [37 x i8], [37 x i8]* %3, i64 0, i64 0
%5 = call i32 @strlen(i8* %4)
%6 = icmp eq i64 %1, 0
br i1 %6, label %19, label %7
7: ; preds = %2, %7
%8 = phi i64 [ %17, %7 ], [ 0, %2 ]
%9 = trunc i64 %8 to i32
%10 = call i32 (...) @random()
%11 = srem i32 %10, %5
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [37 x i8], [37 x i8]* %3, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %14, i8* %15
%16 = add i32 %9, 1
%17 = zext i32 %16 to i64
%18 = icmp ult i64 %17, %1
br i1 %18, label %7, label %19
19: ; preds = %7, %2
%20 = phi i32 [ 0, %2 ], [ %16, %7 ]
%21 = getelementptr inbounds i8, i8* %0, i64 %1
store i8 0, i8* %21
ret i32 %20
}