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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int rand();
int /*<<< orphan*/ stderr;
char *rnd_gen(unsigned int length) {
  const char charlist[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  int i = 0, key = 0;
  char *dest = NULL;
  if (length != 0) {
    dest = malloc(sizeof(char) * (length + 1));
    for (i = 0; i < length; i++) {
      key = rand() % (int)(sizeof(charlist) - 1);
      dest[i] = charlist[key];
    }
    dest[length] = '\0';
  } else {
    fprintf(stderr, " Data length must not be null \n");
  }
  return dest;
}


x86 assembly (clang, -O0):
<rnd_gen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xeaa(%rip),%rsi
mov    $0x3f,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11ed <rnd_gen+0xad>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x60(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11dd <rnd_gen+0x9d>
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0x3e,%ecx
cltd
idiv   %ecx
mov    %edx,-0x58(%rbp)
movslq -0x58(%rbp),%rax
mov    -0x50(%rbp,%rax,1),%dl
mov    -0x60(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    119e <rnd_gen+0x5e>
mov    -0x60(%rbp),%rax
mov    -0x4(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    1202 <rnd_gen+0xc2>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x60(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rnd_gen>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
je     11a9 <rnd_gen+0x79>
mov    %edi,%r14d
lea    0x1(%r14),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %r14d,%r12d
lea    0xeab(%rip),%r14
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <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,%r14,1),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1160 <rnd_gen+0x30>
movb   $0x0,(%r15,%r12,1)
jmp    11c1 <rnd_gen+0x91>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1030 <fprintf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.rnd_gen.charlist = [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
@stderr = global i32 0
@.str = [32 x i8] c" Data length must not be null \0A\00"
define i8* @rnd_gen(i32 %0) {
%2 = alloca i32
%3 = alloca [63 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
%7 = bitcast [63 x i8]* %3 to i8*
store i32 0, i32* %4
store i32 0, i32* %5
store i8* null, i8** %6
%8 = load i32, i32* %2
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %40
10: ; preds = %1
%11 = load i32, i32* %2
%12 = add i32 %11, 1
%13 = zext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %6
store i32 0, i32* %4
br label %17
17: ; preds = %32, %10
%18 = load i32, i32* %4
%19 = load i32, i32* %2
%20 = icmp ult i32 %18, %19
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = call i32 (...) @rand()
%23 = srem i32 %22, 62
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [63 x i8], [63 x i8]* %3, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = load i8*, i8** %6
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 %27, i8* %31
br label %32
32: ; preds = %21
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %4
br label %17
35: ; preds = %17
%36 = load i8*, i8** %6
%37 = load i32, i32* %2
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
br label %43
40: ; preds = %1
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %40, %35
%44 = load i8*, i8** %6
ret i8* %44
}

LLVM IR (-O2):
@__const.rnd_gen.charlist = [63 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\00"
@stderr = global i32 0
@.str = [32 x i8] c" Data length must not be null \0A\00"
define noalias i8* @rnd_gen(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %19, label %3
3: ; preds = %1
%4 = add i32 %0, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = zext i32 %0 to i64
br label %7
7: ; preds = %3, %7
%8 = phi i64 [ 0, %3 ], [ %15, %7 ]
%9 = tail call i32 (...) @rand()
%10 = srem i32 %9, 62
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [63 x i8], [63 x i8]* @__const.rnd_gen.charlist, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %5, i64 %8
store i8 %13, i8* %14
%15 = add nuw nsw i64 %8, 1
%16 = icmp eq i64 %15, %6
br i1 %16, label %17, label %7
17: ; preds = %7
%18 = getelementptr inbounds i8, i8* %5, i64 %6
store i8 0, i8* %18
br label %22
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %17
%23 = phi i8* [ %5, %17 ], [ null, %19 ]
ret i8* %23
}