#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 */
scalar_t__ malloc(int);
int rand();
char *randstring(size_t length) {
  static char charset[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
  char *randomString = NULL;
  if (length) {
    randomString = (char *)malloc(sizeof(char) * (length + 1));
    if (randomString) {
      for (int 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    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11c0 <randstring+0xa0>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bb <randstring+0x9b>
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11af <randstring+0x8f>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x45,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
movslq -0x18(%rbp),%rcx
lea    0x2ead(%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    1168 <randstring+0x48>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11c0 <randstring+0xa0>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randstring>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     118d <randstring+0x6d>
mov    %rdi,%r12
add    $0x1,%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     118d <randstring+0x6d>
mov    %rax,%r14
lea    0xeb9(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x76b981db,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x45,%ecx,%ecx
sub    %ecx,%eax
cltq
movzbl (%rax,%r15,1),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1150 <randstring+0x30>
movb   $0x0,(%r14,%r12,1)
jmp    1190 <randstring+0x70>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@randstring.charset = internal unnamed_addr constant [70 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!\00"
define i8* @randstring(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %21, label %3
3: ; preds = %1
%4 = trunc i64 %0 to i32
%5 = add i32 %4, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %21, label %11
9: ; preds = %11
%10 = getelementptr inbounds i8, i8* %7, i64 %0
store i8 0, i8* %10
br label %21
11: ; preds = %3, %11
%12 = phi i64 [ %19, %11 ], [ 0, %3 ]
%13 = tail call i32 (...) @rand()
%14 = srem i32 %13, 69
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [70 x i8], [70 x i8]* @randstring.charset, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = getelementptr inbounds i8, i8* %7, i64 %12
store i8 %17, i8* %18
%19 = add nuw i64 %12, 1
%20 = icmp eq i64 %19, %0
br i1 %20, label %9, label %11
21: ; preds = %3, %9, %1
%22 = phi i8* [ %7, %9 ], [ null, %3 ], [ null, %1 ]
ret i8* %22
}