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

/* Variables and functions */
char *calloc(int, int);
int rand();
int strlen(char *);
char *generate_random_string(char *alphabet, int length) {
  int alphabet_size = strlen(alphabet);
  char *string = calloc(length + 1, sizeof(char));
  for (int i = 0; i < length; i++) {
    string[i] = alphabet[rand() % alphabet_size];
  }
  return string;
}


x86 assembly (clang, -O0):
<generate_random_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a8 <generate_random_string+0x78>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
cltd
idivl  -0x10(%rbp)
mov    -0x28(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <generate_random_string+0x36>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_random_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r12d
lea    0x1(%r15),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r13
test   %r15d,%r15d
jle    1191 <generate_random_string+0x61>
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <rand@plt>
cltd
idiv   %r12d
movslq %edx,%rax
movzbl (%r14,%rax,1),%eax
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <generate_random_string+0x40>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @generate_random_string(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
%10 = load i32, i32* %4
%11 = add nsw i32 %10, 1
%12 = call i8* @calloc(i32 %11, i32 1)
store i8* %12, i8** %6
store i32 0, i32* %7
br label %13
13: ; preds = %29, %2
%14 = load i32, i32* %7
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i32 (...) @rand()
%20 = load i32, i32* %5
%21 = srem i32 %19, %20
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %18, i64 %22
%24 = load i8, i8* %23
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 %24, i8* %28
br label %29
29: ; preds = %17
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %13
32: ; preds = %13
%33 = load i8*, i8** %6
ret i8* %33
}

LLVM IR (-O2):
define noalias i8* @generate_random_string(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %1, 1
%5 = tail call i8* @calloc(i32 %4, i32 1)
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = zext i32 %1 to i64
br label %10
9: ; preds = %10, %2
ret i8* %5
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %18, %10 ]
%12 = tail call i32 (...) @rand()
%13 = srem i32 %12, %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = getelementptr inbounds i8, i8* %5, i64 %11
store i8 %16, i8* %17
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %8
br i1 %19, label %9, label %10
}