#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*/ alphanum;

/* Variables and functions */
int EINVAL;
int ENOMEM;
char *malloc(size_t const);
int rand();
int generate_random_string(char **dst, const size_t len) {
  static const char alphanum[] =
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  size_t i;
  char *s;
  if (!dst || len == 0)
    return EINVAL;
  if ((s = malloc(len)) == NULL)
    return ENOMEM;
  for (i = 0; i < len - 1; i++) {
    s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
  }
  s[len - 1] = '\0';
  *dst = s;
  return 0;
}


x86 assembly (clang, -O0):
<generate_random_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <generate_random_string+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1157 <generate_random_string+0x37>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <generate_random_string+0xd7>
mov    -0x18(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117f <generate_random_string+0x5f>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f7 <generate_random_string+0xd7>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    11d5 <generate_random_string+0xb5>
mov    $0x0,%al
call   1040 <rand@plt>
cltq
mov    $0x3e,%ecx
xor    %edx,%edx
div    %rcx
lea    0xe4a(%rip),%rax
mov    (%rax,%rdx,1),%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1187 <generate_random_string+0x67>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_random_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11b9 <generate_random_string+0x99>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11b9 <generate_random_string+0x99>
mov    %rdi,%r14
mov    %rbx,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     11c2 <generate_random_string+0xa2>
mov    %rax,%r15
add    $0xffffffffffffffff,%rbx
je     11ad <generate_random_string+0x8d>
movabs $0x8421084210842109,%r12
08 21 84
lea    0xe96(%rip),%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <rand@plt>
movslq %eax,%rcx
mov    %rcx,%rax
shr    %rax
mul    %r12
shr    $0x4,%rdx
mov    %rdx,%rax
shl    $0x6,%rax
mov    %rdx,%rsi
sub    %rax,%rsi
add    %rdx,%rsi
add    %rcx,%rsi
movzbl 0x0(%r13,%rsi,1),%eax
mov    %al,(%r15,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1170 <generate_random_string+0x50>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,(%r14)
xor    %eax,%eax
jmp    11cb <generate_random_string+0xab>
mov    0x2e18(%rip),%rax
jmp    11c9 <generate_random_string+0xa9>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@generate_random_string.alphanum = internal constant [63 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\00"
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @generate_random_string(i8** %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i8** %0, i8*** %4
store i64 %1, i64* %5
%8 = load i8**, i8*** %4
%9 = icmp ne i8** %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10, %2
%14 = load i32, i32* @EINVAL
store i32 %14, i32* %3
br label %46
15: ; preds = %10
%16 = load i64, i64* %5
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* @ENOMEM
store i32 %20, i32* %3
br label %46
21: ; preds = %15
store i64 0, i64* %6
br label %22
22: ; preds = %36, %21
%23 = load i64, i64* %6
%24 = load i64, i64* %5
%25 = sub i64 %24, 1
%26 = icmp ult i64 %23, %25
br i1 %26, label %27, label %39
27: ; preds = %22
%28 = call i32 (...) @rand()
%29 = sext i32 %28 to i64
%30 = urem i64 %29, 62
%31 = getelementptr inbounds [63 x i8], [63 x i8]* @generate_random_string.alphanum, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = load i8*, i8** %7
%34 = load i64, i64* %6
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 %32, i8* %35
br label %36
36: ; preds = %27
%37 = load i64, i64* %6
%38 = add i64 %37, 1
store i64 %38, i64* %6
br label %22
39: ; preds = %22
%40 = load i8*, i8** %7
%41 = load i64, i64* %5
%42 = sub i64 %41, 1
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %7
%45 = load i8**, i8*** %4
store i8* %44, i8** %45
store i32 0, i32* %3
br label %46
46: ; preds = %39, %19, %13
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@generate_random_string.alphanum = internal unnamed_addr constant [63 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\00"
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @generate_random_string(i8** writeonly %0, i64 %1) {
%3 = icmp eq i8** %0, null
%4 = icmp eq i64 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @EINVAL
br label %28
8: ; preds = %2
%9 = tail call i8* @malloc(i64 %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = add i64 %1, -1
%13 = icmp eq i64 %12, 0
br i1 %13, label %26, label %16
14: ; preds = %8
%15 = load i32, i32* @ENOMEM
br label %28
16: ; preds = %11, %16
%17 = phi i64 [ %24, %16 ], [ 0, %11 ]
%18 = tail call i32 (...) @rand()
%19 = sext i32 %18 to i64
%20 = urem i64 %19, 62
%21 = getelementptr inbounds [63 x i8], [63 x i8]* @generate_random_string.alphanum, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = getelementptr inbounds i8, i8* %9, i64 %17
store i8 %22, i8* %23
%24 = add nuw i64 %17, 1
%25 = icmp eq i64 %24, %12
br i1 %25, label %26, label %16
26: ; preds = %16, %11
%27 = getelementptr inbounds i8, i8* %9, i64 %12
store i8 0, i8* %27
store i8* %9, i8** %0
br label %28
28: ; preds = %26, %14, %6
%29 = phi i32 [ %7, %6 ], [ %15, %14 ], [ 0, %26 ]
ret i32 %29
}