#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 */
size_t arc4random_uniform(int /*<<< orphan*/);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ strlen(char *);
char *random_string(int len) {
  int i;
  static char *template = "abcdefghijklmnopqrstuvwxyz0123456789_";
  char *str = (char *)calloc(len + 1, sizeof(char));
  for (i = 0; i < len; i++)
    str[i] = template[arc4random_uniform(strlen(template))];
  str[i] = 0;
  return str;
}


x86 assembly (clang, -O0):
<random_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a3 <random_string+0x73>
mov    0x2ecf(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ec4(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1030 <arc4random_uniform@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1156 <random_string+0x26>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x1(%rbx),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
test   %ebx,%ebx
jle    1183 <random_string+0x53>
mov    %ebx,%r15d
xor    %ebx,%ebx
lea    0xea6(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
call   1030 <arc4random_uniform@plt>
movzbl (%rax,%r12,1),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1160 <random_string+0x30>
jmp    1186 <random_string+0x56>
xor    %r15d,%r15d
movb   $0x0,(%r14,%r15,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@random_string.template = internal global i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i32 0, i32 0)
@.str = [38 x i8] c"abcdefghijklmnopqrstuvwxyz0123456789_\00"
define i8* @random_string(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = add nsw i32 %5, 1
%7 = call i64 @calloc(i32 %6, i32 1)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %4
store i32 0, i32* %3
br label %9
9: ; preds = %24, %1
%10 = load i32, i32* %3
%11 = load i32, i32* %2
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %27
13: ; preds = %9
%14 = load i8*, i8** @random_string.template
%15 = load i8*, i8** @random_string.template
%16 = call i32 @strlen(i8* %15)
%17 = call i64 @arc4random_uniform(i32 %16)
%18 = getelementptr inbounds i8, i8* %14, i64 %17
%19 = load i8, i8* %18
%20 = load i8*, i8** %4
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 %19, i8* %23
br label %24
24: ; preds = %13
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %9
27: ; preds = %9
%28 = load i8*, i8** %4
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
@.str = [38 x i8] c"abcdefghijklmnopqrstuvwxyz0123456789_\00"
define i8* @random_string(i32 %0) {
%2 = add nsw i32 %0, 1
%3 = tail call i64 @calloc(i32 %2, i32 1)
%4 = inttoptr i64 %3 to i8*
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %19
6: ; preds = %1
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %15, %8 ]
%10 = tail call i32 @strlen(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%11 = tail call i64 @arc4random_uniform(i32 %10)
%12 = getelementptr inbounds [38 x i8], [38 x i8]* @.str, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %4, i64 %9
store i8 %13, i8* %14
%15 = add nuw nsw i64 %9, 1
%16 = icmp eq i64 %15, %7
br i1 %16, label %17, label %8
17: ; preds = %8
%18 = zext i32 %0 to i64
br label %19
19: ; preds = %1, %17
%20 = phi i64 [ %18, %17 ], [ 0, %1 ]
%21 = getelementptr inbounds i8, i8* %4, i64 %20
store i8 0, i8* %21
ret i8* %4
}