#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 uint8_t;

/* Variables and functions */
char *am_malloc(int);
int rand();
char *rand_str(uint8_t size) {
  static const char text[] =
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  int i, len = rand() % (size - 1);
  char *dst = am_malloc(len + 1);
  for (i = 0; i < len; ++i) {
    dst[i] = text[rand() % (sizeof text - 1)];
  }
  dst[i] = '\0';
  return dst;
}


x86 assembly (clang, -O0):
<rand_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1030 <am_malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1196 <rand_str+0x76>
mov    $0x0,%al
call   1040 <rand@plt>
cltq
mov    $0x34,%ecx
xor    %edx,%edx
div    %rcx
lea    0xe86(%rip),%rax
mov    (%rax,%rdx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1154 <rand_str+0x34>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edi,%ebx
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <rand@plt>
add    $0xffffffff,%ebx
cltd
idiv   %ebx
mov    %edx,%r14d
lea    0x1(%r14),%edi
call   1030 <am_malloc@plt>
mov    %rax,%r13
test   %r14d,%r14d
jle    119e <rand_str+0x7e>
mov    %r14d,%r15d
movabs $0x4ec4ec4ec4ec4ec5,%r14
ec c4 4e
lea    0xe9d(%rip),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1040 <rand@plt>
movslq %eax,%rcx
mov    %rcx,%rax
mul    %r14
shr    $0x4,%rdx
imul   $0x34,%rdx,%rax
sub    %rax,%rcx
movzbl (%r12,%rcx,1),%eax
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1170 <rand_str+0x50>
movb   $0x0,0x0(%r13,%r15,1)
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@rand_str.text = internal constant [53 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define i8* @rand_str(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
%6 = call i32 (...) @rand()
%7 = load i32, i32* %2
%8 = sub nsw i32 %7, 1
%9 = srem i32 %6, %8
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = add nsw i32 %10, 1
%12 = call i8* @am_malloc(i32 %11)
store i8* %12, i8** %5
store i32 0, i32* %3
br label %13
13: ; preds = %27, %1
%14 = load i32, i32* %3
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %30
17: ; preds = %13
%18 = call i32 (...) @rand()
%19 = sext i32 %18 to i64
%20 = urem i64 %19, 52
%21 = getelementptr inbounds [53 x i8], [53 x i8]* @rand_str.text, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %5
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
br label %27
27: ; preds = %17
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
br label %13
30: ; preds = %13
%31 = load i8*, i8** %5
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %5
ret i8* %35
}

LLVM IR (-O2):
@rand_str.text = internal unnamed_addr constant [53 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define i8* @rand_str(i32 %0) {
%2 = tail call i32 (...) @rand()
%3 = add nsw i32 %0, -1
%4 = srem i32 %2, %3
%5 = add nsw i32 %4, 1
%6 = tail call i8* @am_malloc(i32 %5)
%7 = icmp sgt i32 %4, 0
br i1 %7, label %8, label %22
8: ; preds = %1
%9 = zext i32 %4 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %18, %10 ]
%12 = tail call i32 (...) @rand()
%13 = sext i32 %12 to i64
%14 = urem i64 %13, 52
%15 = getelementptr inbounds [53 x i8], [53 x i8]* @rand_str.text, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = getelementptr inbounds i8, i8* %6, i64 %11
store i8 %16, i8* %17
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %9
br i1 %19, label %20, label %10
20: ; preds = %10
%21 = zext i32 %4 to i64
br label %22
22: ; preds = %1, %20
%23 = phi i64 [ %21, %20 ], [ 0, %1 ]
%24 = getelementptr inbounds i8, i8* %6, i64 %23
store i8 0, i8* %24
ret i8* %6
}