#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 */
int RAND_bytes(unsigned char *, int);
char *malloc(int);
int strlen(char *);
int generate_salt(char **salt) {
  int i = 0;
  int c = 0;
  char valid_chars[] = "abcdefghijklmnopqrstuvwxyz"
                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                       "0123456789"
                       "!@#$%^&*()-_=+|[]{};,.<>?/~";
  int valid_chars_len = strlen(valid_chars);
  unsigned char rand_bytes[32];
  *salt = malloc(32);
  if (!salt) {
    return 1;
  }
  if (RAND_bytes(rand_bytes, 32) != 1) {
    return 1;
  }
  for (i = 0; i < 32; i++) {
    c = rand_bytes[i] % valid_chars_len;
    (*salt)[i] = valid_chars[c];
  }
  return 0;
}


x86 assembly (clang, -O0):
<generate_salt>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe98(%rip),%rsi
mov    $0x5a,%edx
call   1050 <memcpy@plt>
lea    -0x80(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x84(%rbp)
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    11ac <generate_salt+0x6c>
movl   $0x1,-0x4(%rbp)
jmp    1224 <generate_salt+0xe4>
lea    -0xb0(%rbp),%rdi
mov    $0x20,%esi
call   1040 <RAND_bytes@plt>
cmp    $0x1,%eax
je     11d2 <generate_salt+0x92>
movl   $0x1,-0x4(%rbp)
jmp    1224 <generate_salt+0xe4>
movl   $0x0,-0x14(%rbp)
cmpl   $0x20,-0x14(%rbp)
jge    121d <generate_salt+0xdd>
movslq -0x14(%rbp),%rax
movzbl -0xb0(%rbp,%rax,1),%eax
ff
cltd
idivl  -0x84(%rbp)
mov    %edx,-0x18(%rbp)
movslq -0x18(%rbp),%rax
mov    -0x80(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d9 <generate_salt+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_salt>:
push   %rbp
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%rbx
movups 0xf07(%rip),%xmm0
movups %xmm0,0x4a(%rsp)
movaps 0xef1(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xed5(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xeb9(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe9d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe81(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,(%rbx)
lea    0x60(%rsp),%rdi
mov    $0x20,%esi
call   1040 <RAND_bytes@plt>
mov    $0x1,%ecx
cmp    $0x1,%eax
jne    11f1 <generate_salt+0xc1>
xor    %ecx,%ecx
xor    %esi,%esi
nopw   0x0(%rax,%rax,1)
movzbl 0x60(%rsp,%rsi,1),%eax
xor    %edx,%edx
idiv   %ebp
movzbl (%rsp,%rdx,1),%eax
mov    (%rbx),%rdx
mov    %al,(%rdx,%rsi,1)
movzbl 0x61(%rsp,%rsi,1),%eax
xor    %edx,%edx
idiv   %ebp
movzbl (%rsp,%rdx,1),%eax
mov    (%rbx),%rdx
mov    %al,0x1(%rdx,%rsi,1)
add    $0x2,%rsi
cmp    $0x20,%rsi
jne    11c0 <generate_salt+0x90>
mov    %ecx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.generate_salt.valid_chars = [90 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+|[]{};,.<>?/~\00"
define i32 @generate_salt(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca [90 x i8]
%7 = alloca i32
%8 = alloca [32 x i8]
store i8** %0, i8*** %3
store i32 0, i32* %4
store i32 0, i32* %5
%9 = bitcast [90 x i8]* %6 to i8*
%10 = getelementptr inbounds [90 x i8], [90 x i8]* %6, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %7
%12 = call i8* @malloc(i32 32)
%13 = load i8**, i8*** %3
store i8* %12, i8** %13
%14 = load i8**, i8*** %3
%15 = icmp ne i8** %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i32 1, i32* %2
br label %47
17: ; preds = %1
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%19 = call i32 @RAND_bytes(i8* %18, i32 32)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 1, i32* %2
br label %47
22: ; preds = %17
store i32 0, i32* %4
br label %23
23: ; preds = %43, %22
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 32
br i1 %25, label %26, label %46
26: ; preds = %23
%27 = load i32, i32* %4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = load i32, i32* %7
%33 = srem i32 %31, %32
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [90 x i8], [90 x i8]* %6, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = load i8**, i8*** %3
%39 = load i8*, i8** %38
%40 = load i32, i32* %4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 %37, i8* %42
br label %43
43: ; preds = %26
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %23
46: ; preds = %23
store i32 0, i32* %2
br label %47
47: ; preds = %46, %21, %16
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@__const.generate_salt.valid_chars = [90 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+|[]{};,.<>?/~\00"
define i32 @generate_salt(i8** nocapture %0) {
%2 = alloca [90 x i8]
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [90 x i8], [90 x i8]* %2, i64 0, i64 0
%5 = call i32 @strlen(i8* %4)
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = call dereferenceable_or_null(32) i8* @malloc(i32 32)
store i8* %7, i8** %0
%8 = call i32 @RAND_bytes(i8* %6, i32 32)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %33
10: ; preds = %1, %10
%11 = phi i64 [ %31, %10 ], [ 0, %1 ]
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = srem i32 %14, %5
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds [90 x i8], [90 x i8]* %2, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = load i8*, i8** %0
%20 = getelementptr inbounds i8, i8* %19, i64 %11
store i8 %18, i8* %20
%21 = or i64 %11, 1
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = srem i32 %24, %5
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds [90 x i8], [90 x i8]* %2, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %0
%30 = getelementptr inbounds i8, i8* %29, i64 %21
store i8 %28, i8* %30
%31 = add nuw nsw i64 %11, 2
%32 = icmp eq i64 %31, 32
br i1 %32, label %33, label %10
33: ; preds = %10, %1
%34 = phi i32 [ 1, %1 ], [ 0, %10 ]
ret i32 %34
}