void password_by_index(unsigned long long index, char *password,
                       const char *charset, unsigned int charset_length) {
  unsigned int i = 0;
  while (1) {
    password[i++] = charset[index % charset_length];
    index /= charset_length;
    if (index == 0L)
      break;
    index--;
  }
  password[i] = '\0';
}


x86 assembly (clang, -O0):
<password_by_index>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    -0x28(%rbp),%rax
mov    (%rax,%rdx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    116b <password_by_index+0x6b>
jmp    117c <password_by_index+0x7c>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    111a <password_by_index+0x1a>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
pop    %rbp
ret

x86 assembly (clang, -O2):
<password_by_index>:
mov    %rdx,%r8
mov    %ecx,%r9d
mov    %rdi,%rax
shr    $0x20,%rax
je     1119 <password_by_index+0x19>
mov    %rdi,%rax
xor    %edx,%edx
div    %r9
jmp    1120 <password_by_index+0x20>
mov    %edi,%eax
xor    %edx,%edx
div    %r9d
mov    (%r8,%rdx,1),%al
mov    %al,(%rsi)
mov    $0x1,%eax
cmp    %rdi,%r9
ja     1191 <password_by_index+0x91>
mov    $0x1,%ecx
jmp    115a <password_by_index+0x5a>
nopw   0x0(%rax,%rax,1)
mov    %rdi,%rax
xor    %edx,%edx
div    %r9
movzbl (%r8,%rdx,1),%eax
mov    %ecx,%edx
add    $0x1,%ecx
mov    %al,(%rsi,%rdx,1)
cmp    %r9,%rdi
jb     118f <password_by_index+0x8f>
mov    %rdi,%rax
shr    $0x20,%rax
je     1170 <password_by_index+0x70>
mov    %rdi,%rax
xor    %edx,%edx
div    %r9
mov    %rax,%rdi
jmp    1179 <password_by_index+0x79>
mov    %edi,%eax
xor    %edx,%edx
div    %r9d
mov    %eax,%edi
add    $0xffffffffffffffff,%rdi
mov    %rdi,%rax
shr    $0x20,%rax
jne    1140 <password_by_index+0x40>
mov    %edi,%eax
xor    %edx,%edx
div    %r9d
jmp    1148 <password_by_index+0x48>
mov    %ecx,%eax
movb   $0x0,(%rsi,%rax,1)
ret

LLVM IR (-O0):
define void @password_by_index(i64 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %4, %30
%11 = load i8*, i8** %7
%12 = load i64, i64* %5
%13 = load i32, i32* %8
%14 = zext i32 %13 to i64
%15 = urem i64 %12, %14
%16 = getelementptr inbounds i8, i8* %11, i64 %15
%17 = load i8, i8* %16
%18 = load i8*, i8** %6
%19 = load i32, i32* %9
%20 = add i32 %19, 1
store i32 %20, i32* %9
%21 = zext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
store i8 %17, i8* %22
%23 = load i32, i32* %8
%24 = zext i32 %23 to i64
%25 = load i64, i64* %5
%26 = udiv i64 %25, %24
store i64 %26, i64* %5
%27 = load i64, i64* %5
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %10
br label %33
30: ; preds = %10
%31 = load i64, i64* %5
%32 = add i64 %31, -1
store i64 %32, i64* %5
br label %10
33: ; preds = %29
%34 = load i8*, i8** %6
%35 = load i32, i32* %9
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 0, i8* %37
ret void
}

LLVM IR (-O2):
define void @password_by_index(i64 %0, i8* nocapture writeonly %1, i8* nocapture %2, i32 %3) {
%5 = zext i32 %3 to i64
%6 = urem i64 %0, %5
%7 = getelementptr inbounds i8, i8* %2, i64 %6
%8 = load i8, i8* %7
store i8 %8, i8* %1
%9 = icmp ugt i64 %5, %0
br i1 %9, label %24, label %10
10: ; preds = %4, %10
%11 = phi i32 [ %18, %10 ], [ 1, %4 ]
%12 = phi i64 [ %14, %10 ], [ %0, %4 ]
%13 = udiv i64 %12, %5
%14 = add i64 %13, -1
%15 = urem i64 %14, %5
%16 = getelementptr inbounds i8, i8* %2, i64 %15
%17 = load i8, i8* %16
%18 = add i32 %11, 1
%19 = zext i32 %11 to i64
%20 = getelementptr inbounds i8, i8* %1, i64 %19
store i8 %17, i8* %20
%21 = icmp ult i64 %14, %5
br i1 %21, label %22, label %10
22: ; preds = %10
%23 = zext i32 %18 to i64
br label %24
24: ; preds = %22, %4
%25 = phi i64 [ 1, %4 ], [ %23, %22 ]
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 0, i8* %26
ret void
}