#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 rand();
int strlen(char *);
int randstr(char *buf, int len) {
  int i, idx;
  char token[] = "qwertyuioplkjhgfdsazxcvbnm1234567890";
  for (i = 0; i < len; i++) {
    idx = rand() % strlen(token);
    buf[i] = token[idx];
  }
  return 0;
}


x86 assembly (clang, -O0):
<randstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xeb6(%rip),%rsi
mov    $0x25,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a6 <randstr+0x76>
mov    $0x0,%al
call   1050 <rand@plt>
mov    %eax,-0x44(%rbp)
lea    -0x40(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
movslq -0x14(%rbp),%rax
mov    -0x40(%rbp,%rax,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    115b <randstr+0x2b>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movaps 0xedd(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xec1(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x30393837363534,%rax
mov    %rax,0x1d(%rsp)
test   %esi,%esi
jle    119c <randstr+0x7c>
mov    %rdi,%r15
mov    %esi,%r12d
mov    %rsp,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
movslq %edx,%rax
movzbl (%rsp,%rax,1),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1170 <randstr+0x50>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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