#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 *malloc(int);
int rand();
char *generate_password(int length) {
  char *pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  char *password = malloc(sizeof(*password) * (length + 1));
  int i;
  for (i = 0; i < length; i++) {
    password[i] = pool[rand() % (sizeof((pool)) / sizeof((pool[0])))];
  }
  password[length] = '\0';
  return password;
}


x86 assembly (clang, -O0):
<generate_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    0xece(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1198 <generate_password+0x78>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
and    $0x7,%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    1154 <generate_password+0x34>
mov    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_password>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edi,%r14d
lea    0x1(%r14),%edi
call   1030 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    116c <generate_password+0x4c>
mov    %r14d,%r13d
lea    0xeb9(%rip),%r12
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <rand@plt>
and    $0x7,%eax
movzbl (%rax,%r12,1),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1150 <generate_password+0x30>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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