#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(size_t);
int rand();
char *mkrndstr(size_t length) {
  static char charset[] = "abcdefghijklmnopqrstuvwxyz";
  char *randomString;
  int n;
  if (length) {
    randomString = malloc(length + 1);
    if (randomString) {
      int l = (int)(sizeof(charset) - 1);
      int key;
      for (n = 0; n < length; n++) {
        key = rand() % l;
        randomString[n] = charset[key];
      }
      randomString[length] = '\0';
    }
  }
  return randomString;
}


x86 assembly (clang, -O0):
<mkrndstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11b5 <mkrndstr+0x95>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b0 <mkrndstr+0x90>
movl   $0x1a,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    11a4 <mkrndstr+0x84>
mov    $0x0,%al
call   1040 <rand@plt>
cltd
idivl  -0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movslq -0x1c(%rbp),%rcx
lea    0x2eb8(%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    1161 <mkrndstr+0x41>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11b5 <mkrndstr+0x95>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkrndstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1192 <mkrndstr+0x72>
mov    %rdi,%r12
add    $0x1,%rdi
call   1030 <malloc@plt>
test   %rax,%rax
je     1194 <mkrndstr+0x74>
mov    %rax,%r14
lea    0xeb8(%rip),%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x4ec4ec4f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rdx,%rdx,4),%edx
add    %ecx,%edx
sub    %edx,%eax
cltq
movzbl (%rax,%r15,1),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1150 <mkrndstr+0x30>
movb   $0x0,(%r14,%r12,1)
jmp    1197 <mkrndstr+0x77>
jmp    1197 <mkrndstr+0x77>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@mkrndstr.charset = internal global [27 x i8] c"abcdefghijklmnopqrstuvwxyz\00"
define i8* @mkrndstr(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %2
%7 = load i64, i64* %2
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %41
9: ; preds = %1
%10 = load i64, i64* %2
%11 = add i64 %10, 1
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %40
15: ; preds = %9
store i32 26, i32* %5
store i32 0, i32* %4
br label %16
16: ; preds = %33, %15
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = load i64, i64* %2
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %36
21: ; preds = %16
%22 = call i32 (...) @rand()
%23 = load i32, i32* %5
%24 = srem i32 %22, %23
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [27 x i8], [27 x i8]* @mkrndstr.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 = %21
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %16
36: ; preds = %16
%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, %9
br label %41
41: ; preds = %40, %1
%42 = load i8*, i8** %3
ret i8* %42
}

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