#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 */
typedef int /*<<< orphan*/ charset;

/* Variables and functions */
scalar_t__ rand();
void rand_str(char *dest, size_t length) {
  char charset[] = "0123456789"
                   "abcdefghijklmnopqrstuvwxyz"
                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  while (length-- > 0) {
    size_t index = (size_t)rand() % (sizeof(charset) - 1);
    *dest++ = charset[index];
  }
  *dest = '\0';
}


x86 assembly (clang, -O0):
<rand_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xec5(%rip),%rsi
mov    $0x3f,%edx
call   1030 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
cmp    $0x0,%rax
jbe    1191 <rand_str+0x71>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x3e,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp,%rax,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
jmp    1145 <rand_str+0x25>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
test   %rsi,%rsi
je     117e <rand_str+0x6e>
mov    %rsi,%rbx
movabs $0x8421084210842109,%r15
08 21 84
lea    0xecc(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <rand@plt>
mov    %rax,%rcx
shr    %rax
mul    %r15
shr    $0x4,%rdx
mov    %rdx,%rax
shl    $0x6,%rax
mov    %rdx,%rsi
sub    %rax,%rsi
add    %rdx,%rsi
add    %rcx,%rsi
lea    0x1(%r14),%rax
movzbl (%r12,%rsi,1),%ecx
mov    %cl,(%r14)
mov    %rax,%r14
add    $0xffffffffffffffff,%rbx
jne    1140 <rand_str+0x30>
jmp    1181 <rand_str+0x71>
mov    %r14,%rax
movb   $0x0,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.rand_str.charset = [63 x i8] c"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define void @rand_str(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [63 x i8]
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = bitcast [63 x i8]* %5 to i8*
br label %8
8: ; preds = %12, %2
%9 = load i64, i64* %4
%10 = add i64 %9, -1
store i64 %10, i64* %4
%11 = icmp ugt i64 %9, 0
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = call i64 (...) @rand()
%14 = urem i64 %13, 62
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = getelementptr inbounds [63 x i8], [63 x i8]* %5, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
store i8 %17, i8* %18
br label %8
20: ; preds = %8
%21 = load i8*, i8** %3
store i8 0, i8* %21
ret void
}

LLVM IR (-O2):
@__const.rand_str.charset = [63 x i8] c"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define void @rand_str(i8* nocapture writeonly %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %14, label %4
4: ; preds = %2, %4
%5 = phi i8* [ %12, %4 ], [ %0, %2 ]
%6 = phi i64 [ %7, %4 ], [ %1, %2 ]
%7 = add i64 %6, -1
%8 = tail call i64 (...) @rand()
%9 = urem i64 %8, 62
%10 = getelementptr inbounds [63 x i8], [63 x i8]* @__const.rand_str.charset, i64 0, i64 %9
%11 = load i8, i8* %10
%12 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %11, i8* %5
%13 = icmp eq i64 %7, 0
br i1 %13, label %14, label %4
14: ; preds = %4, %2
%15 = phi i8* [ %0, %2 ], [ %12, %4 ]
store i8 0, i8* %15
ret void
}