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

/* Variables and functions */
char *malloc(int);
int rand();
char *rand_string(int n) {
  char chars[] =
      "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789";
  char *result = malloc(sizeof(char) * (n - 1));
  for (int i = 0; i < n; i++) {
    result[i] = chars[rand() % (sizeof(chars) - 1)];
  }
  result[n] = 0;
  return result;
}


x86 assembly (clang, -O0):
<rand_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xeba(%rip),%rsi
mov    $0x3f,%edx
call   1030 <memcpy@plt>
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11aa <rand_string+0x7a>
mov    $0x0,%al
call   1050 <rand@plt>
cltq
mov    $0x3e,%ecx
xor    %edx,%edx
div    %rcx
mov    -0x50(%rbp,%rdx,1),%dl
mov    -0x58(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    116e <rand_string+0x3e>
mov    -0x58(%rbp),%rax
movslq -0x4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x58(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
lea    -0x1(%r14),%edi
call   1030 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    119d <rand_string+0x7d>
mov    %r14d,%ebx
movabs $0x8421084210842109,%r12
08 21 84
lea    0xead(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
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    1160 <rand_string+0x40>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.rand_string.chars = [63 x i8] c"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789\00"
define i8* @rand_string(i32 %0) {
%2 = alloca i32
%3 = alloca [63 x i8]
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = bitcast [63 x i8]* %3 to i8*
%7 = load i32, i32* %2
%8 = sub nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = mul i64 1, %9
%11 = trunc i64 %10 to i32
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %4
store i32 0, i32* %5
br label %13
13: ; preds = %27, %1
%14 = load i32, i32* %5
%15 = load i32, i32* %2
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %30
17: ; preds = %13
%18 = call i32 (...) @rand()
%19 = sext i32 %18 to i64
%20 = urem i64 %19, 62
%21 = getelementptr inbounds [63 x i8], [63 x i8]* %3, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
br label %27
27: ; preds = %17
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %13
30: ; preds = %13
%31 = load i8*, i8** %4
%32 = load i32, i32* %2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %4
ret i8* %35
}

LLVM IR (-O2):
@__const.rand_string.chars = [63 x i8] c"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789\00"
define noalias i8* @rand_string(i32 %0) {
%2 = add nsw i32 %0, -1
%3 = tail call i8* @malloc(i32 %2)
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = zext i32 %0 to i64
br label %10
7: ; preds = %10, %1
%8 = sext i32 %0 to i64
%9 = getelementptr inbounds i8, i8* %3, i64 %8
store i8 0, i8* %9
ret i8* %3
10: ; preds = %5, %10
%11 = phi i64 [ 0, %5 ], [ %18, %10 ]
%12 = tail call i32 (...) @rand()
%13 = sext i32 %12 to i64
%14 = urem i64 %13, 62
%15 = getelementptr inbounds [63 x i8], [63 x i8]* @__const.rand_string.chars, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = getelementptr inbounds i8, i8* %3, i64 %11
store i8 %16, i8* %17
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %6
br i1 %19, label %7, label %10
}