#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(size_t length) {
  static char charset[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
  char *randomString = NULL;
  int n;
  if (length) {
    randomString = malloc(sizeof(char) * (length + 1));
    if (randomString) {
      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    %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,%r14
add    $0x1,%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     118d <randstring+0x6d>
mov    %rax,%r12
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,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <randstring+0x30>
movb   $0x0,(%r12,%r14,1)
jmp    1190 <randstring+0x70>
xor    %r12d,%r12d
mov    %r12,%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 %41
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 i8* @malloc(i32 %12)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %40
16: ; preds = %8
store i32 0, i32* %4
br label %17
17: ; preds = %33, %16
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = load i64, i64* %2
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = call i32 (...) @rand()
%24 = srem i32 %23, 69
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [70 x i8], [70 x i8]* @randstring.charset, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %22
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %17
36: ; preds = %17
%37 = load i8*, i8** %3
%38 = load i64, i64* %2
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %36, %8
br label %41
41: ; preds = %40, %1
%42 = load i8*, i8** %3
ret i8* %42
}

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