#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 */
double RAND_MAX;
scalar_t__ rand();
void rand_str(char *dest, size_t length) {
  char charset[] =
      "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  while (length-- > 0) {
    size_t index = (double)rand() / RAND_MAX * (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    0xed5(%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    11cd <rand_str+0xad>
xor    %eax,%eax
call   1040 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2e77(%rip),%rax
movsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe7f(%rip),%xmm1
mulsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xe6c(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-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     1192 <rand_str+0x82>
mov    %rsi,%rbx
mov    0x2ebe(%rip),%r15
lea    0xedf(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r15),%xmm0
mulsd  0xea4(%rip),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xe9c(%rip),%xmm0
sar    $0x3f,%rcx
cvttsd2si %xmm0,%rdx
and    %rcx,%rdx
or     %rax,%rdx
lea    0x1(%r14),%rax
movzbl (%rdx,%r12,1),%ecx
mov    %cl,(%r14)
mov    %rax,%r14
add    $0xffffffffffffffff,%rbx
jne    1140 <rand_str+0x30>
jmp    1195 <rand_str+0x85>
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"
@RAND_MAX = global double 0.000000e+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 %24
12: ; preds = %8
%13 = call i64 (...) @rand()
%14 = sitofp i64 %13 to double
%15 = load double, double* @RAND_MAX
%16 = fdiv double %14, %15
%17 = fmul double %16, 6.200000e+01
%18 = fptoui double %17 to i64
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = getelementptr inbounds [63 x i8], [63 x i8]* %5, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %3
store i8 %21, i8* %22
br label %8
24: ; preds = %8
%25 = load i8*, i8** %3
store i8 0, i8* %25
ret void
}

LLVM IR (-O2):
@__const.rand_str.charset = [63 x i8] c"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
@RAND_MAX = global double 0.000000e+00
define void @rand_str(i8* nocapture writeonly %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %18, label %4
4: ; preds = %2, %4
%5 = phi i8* [ %16, %4 ], [ %0, %2 ]
%6 = phi i64 [ %7, %4 ], [ %1, %2 ]
%7 = add i64 %6, -1
%8 = tail call i64 (...) @rand()
%9 = sitofp i64 %8 to double
%10 = load double, double* @RAND_MAX
%11 = fdiv double %9, %10
%12 = fmul double %11, 6.200000e+01
%13 = fptoui double %12 to i64
%14 = getelementptr inbounds [63 x i8], [63 x i8]* @__const.rand_str.charset, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %15, i8* %5
%17 = icmp eq i64 %7, 0
br i1 %17, label %18, label %4
18: ; preds = %4, %2
%19 = phi i8* [ %0, %2 ], [ %16, %4 ]
store i8 0, i8* %19
ret void
}