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

/* Variables and functions */
char *malloc(int);
int rand();
char *randstring(int length) {
  static char charset[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  char *randomString;
  if (length > 0) {
    randomString = malloc(length + 1);
    if (randomString != NULL) {
      int n;
      for (n = 0; n < length; n++) {
        int key = rand() % (int)(sizeof(charset) - 1);
        randomString[n] = charset[key];
      }
      randomString[length] = '\0';
    }
  }
  return randomString;
}


x86 assembly (clang, -O0):
<randstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    11ac <randstring+0x8c>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a7 <randstring+0x87>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    119b <randstring+0x7b>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x3e,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
movslq -0x18(%rbp),%rcx
lea    0x2ec1(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1156 <randstring+0x36>
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ac <randstring+0x8c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randstring>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %edi,%edi
jle    119c <randstring+0x7c>
mov    %edi,%r14d
lea    0x1(%r14),%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     119e <randstring+0x7e>
mov    %rax,%r15
mov    %r14d,%r13d
lea    0xeb5(%rip),%r12
xor    %ebx,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0xffffffff84210843,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x5,%ecx
add    %edx,%ecx
mov    %ecx,%edx
shl    $0x6,%edx
mov    %ecx,%esi
sub    %edx,%esi
add    %ecx,%esi
add    %eax,%esi
movslq %esi,%rax
movzbl (%rax,%r12,1),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1150 <randstring+0x30>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
jmp    11a1 <randstring+0x81>
jmp    11a1 <randstring+0x81>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@randstring.charset = internal global [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
define i8* @randstring(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %39
8: ; preds = %1
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 1
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %38
14: ; preds = %8
store i32 0, i32* %4
br label %15
15: ; preds = %30, %14
%16 = load i32, i32* %4
%17 = load i32, i32* %2
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %33
19: ; preds = %15
%20 = call i32 (...) @rand()
%21 = srem i32 %20, 62
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [63 x i8], [63 x i8]* @randstring.charset, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
br label %30
30: ; preds = %19
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %15
33: ; preds = %15
%34 = load i8*, i8** %3
%35 = load i32, i32* %2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %33, %8
br label %39
39: ; preds = %38, %1
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
@randstring.charset = internal unnamed_addr constant [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
define noalias i8* @randstring(i32 %0) {
%2 = icmp sgt i32 %0, 0
br i1 %2, label %3, label %22
3: ; preds = %1
%4 = add nuw nsw i32 %0, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %22, label %7
7: ; preds = %3
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %17, %9 ]
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, 62
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [63 x i8], [63 x i8]* @randstring.charset, 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 nsw i64 %10, 1
%18 = icmp eq i64 %17, %8
br i1 %18, label %19, label %9
19: ; preds = %9
%20 = sext i32 %0 to i64
%21 = getelementptr inbounds i8, i8* %5, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %3, %19, %1
%23 = phi i8* [ %5, %19 ], [ null, %3 ], [ undef, %1 ]
ret i8* %23
}