#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 */
char *malloc(size_t);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
char *get_random_string(size_t n) {
  const char *charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  srand(time(NULL));
  char *s = malloc(n + 1);
  for (int i = 0; i < n; ++i)
    s[i] = charset[rand() % 52];
  s[n] = 0;
  return s;
}


x86 assembly (clang, -O0):
<get_random_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xead(%rip),%rax
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11c7 <get_random_string+0x87>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0x34,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117f <get_random_string+0x3f>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_random_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
lea    0x1(%r14),%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %r14,%r14
je     11b6 <get_random_string+0x76>
lea    0xe8f(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0x4ec4ec4f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x24,%rcx
add    %edx,%ecx
imul   $0x34,%ecx,%ecx
sub    %ecx,%eax
cltq
movzbl (%rax,%r15,1),%eax
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1180 <get_random_string+0x40>
movb   $0x0,(%r12,%r14,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [53 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\00"
define i8* @get_random_string(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i64 %0, i64* %2
store i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8** %3
%6 = call i32 @time(i32* null)
%7 = call i32 @srand(i32 %6)
%8 = load i64, i64* %2
%9 = add i64 %8, 1
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %4
store i32 0, i32* %5
br label %11
11: ; preds = %27, %1
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = load i64, i64* %2
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 (...) @rand()
%19 = srem i32 %18, 52
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, 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 = %16
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %11
30: ; preds = %11
%31 = load i8*, i8** %4
%32 = load i64, i64* %2
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %4
ret i8* %34
}

LLVM IR (-O2):
@.str = [53 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\00"
define noalias i8* @get_random_string(i64 %0) {
%2 = tail call i32 @time(i32* null)
%3 = tail call i32 @srand(i32 %2)
%4 = add i64 %0, 1
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i64 %0, 0
br i1 %6, label %7, label %9
7: ; preds = %9, %1
%8 = getelementptr inbounds i8, i8* %5, i64 %0
store i8 0, i8* %8
ret i8* %5
9: ; preds = %1, %9
%10 = phi i64 [ %17, %9 ], [ 0, %1 ]
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, 52
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [53 x i8], [53 x i8]* @.str, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = getelementptr inbounds i8, i8* %5, i64 %10
store i8 %15, i8* %16
%17 = add nuw i64 %10, 1
%18 = icmp eq i64 %17, %0
br i1 %18, label %7, label %9
}