#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 /*<<< orphan*/ alphabet;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void gensalt(char *salt, size_t len) {
  static const char alphabet[] = "abcdefghijklmopqrstuvwxyz"
                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                 "0123456789_/";
  FILE *fp;
  size_t i;
  fp = fopen("/dev/urandom", "rb");
  if (fp == NULL)
    errx(1, "failed to open random device: %s", strerror(errno));
  if (fread(salt, len, 1, fp) != 1)
    errx(1, "failed to gen random salt: %s", strerror(errno));
  for (i = 0; i < len; i++) {
    salt[i] =
        alphabet[salt[i] % ((sizeof(alphabet) / sizeof(alphabet[0])) - 1)];
    if (len == 2 && salt[i] == '_')
      salt[i] = '.';
  }
  salt[len] = '\0';
  fclose(fp);
}


x86 assembly (clang, -O0):
<gensalt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xed9(%rip),%rdi
lea    0xedf(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a3 <gensalt+0x53>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xeb2(%rip),%rsi
call   1050 <errx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11e3 <gensalt+0x93>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe93(%rip),%rsi
call   1050 <errx@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1267 <gensalt+0x117>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbq (%rax,%rcx,1),%rax
mov    $0x3f,%ecx
xor    %edx,%edx
div    %rcx
lea    0xde9(%rip),%rax
mov    (%rax,%rdx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
cmpq   $0x2,-0x10(%rbp)
jne    1251 <gensalt+0x101>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5f,%eax
jne    1251 <gensalt+0x101>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x2e,(%rax,%rcx,1)
jmp    1256 <gensalt+0x106>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11eb <gensalt+0x9b>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gensalt>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xedc(%rip),%rdi
lea    0xee2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1199 <gensalt+0x49>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xec3(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1050 <errx@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11d2 <gensalt+0x82>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xeab(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1050 <errx@plt>
test   %r14,%r14
je     12f0 <gensalt+0x1a0>
movabs $0x410410410410411,%r8
41 10 04
cmp    $0x1,%r14
jne    11f2 <gensalt+0xa2>
xor    %esi,%esi
jmp    129e <gensalt+0x14e>
mov    %r14,%r11
and    $0xfffffffffffffffe,%r11
xor    %esi,%esi
lea    0xdfe(%rip),%r9
mov    $0x2e,%r10d
nopl   0x0(%rax,%rax,1)
movsbq (%rbx,%rsi,1),%rcx
mov    %rcx,%rax
mul    %r8
mov    %rcx,%rax
sub    %rdx,%rax
shr    %rax
add    %rdx,%rax
shr    $0x5,%rax
mov    %rax,%rdx
shl    $0x6,%rdx
sub    %rdx,%rax
add    %rcx,%rax
cmp    $0x3d,%rax
movzbl (%r9,%rax,1),%edi
mov    %edi,%ebp
cmove  %r10d,%ebp
movsbq 0x1(%rbx,%rsi,1),%rcx
mov    %rcx,%rax
mul    %r8
mov    %rcx,%rax
sub    %rdx,%rax
shr    %rax
add    %rdx,%rax
shr    $0x5,%rax
mov    %rax,%rdx
shl    $0x6,%rdx
sub    %rdx,%rax
add    %rcx,%rax
cmp    $0x3d,%rax
movzbl (%r9,%rax,1),%eax
mov    %eax,%ecx
cmove  %r10d,%ecx
cmp    $0x2,%r14
cmovne %edi,%ebp
cmovne %eax,%ecx
mov    %bpl,(%rbx,%rsi,1)
mov    %cl,0x1(%rbx,%rsi,1)
add    $0x2,%rsi
cmp    %rsi,%r11
jne    1210 <gensalt+0xc0>
test   $0x1,%r14b
je     12f0 <gensalt+0x1a0>
mov    %r14,%rdi
movsbq (%rbx,%rsi,1),%rcx
mov    %rcx,%rax
mul    %r8
xor    $0x2,%rdi
mov    %rcx,%rax
sub    %rdx,%rax
shr    %rax
add    %rdx,%rax
shr    $0x5,%rax
mov    %rax,%rdx
shl    $0x6,%rdx
sub    %rdx,%rax
add    %rcx,%rax
lea    0xd26(%rip),%rcx
movzbl (%rcx,%rax,1),%ecx
xor    $0x3d,%rax
or     %rdi,%rax
mov    $0x2e,%eax
cmovne %ecx,%eax
mov    %al,(%rbx,%rsi,1)
movb   $0x0,(%rbx,%r14,1)
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@gensalt.alphabet = internal constant [64 x i8] c"abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/\00"
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [33 x i8] c"failed to open random device: %s\00"
@errno = global i32 0
@.str.3 = [30 x i8] c"failed to gen random salt: %s\00"
define void @gensalt(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 @errx(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %10, %2
%15 = load i8*, i8** %3
%16 = load i64, i64* %4
%17 = load i32*, i32** %5
%18 = call i32 @fread(i8* %15, i64 %16, i32 1, i32* %17)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %20, %14
store i64 0, i64* %6
br label %25
25: ; preds = %55, %24
%26 = load i64, i64* %6
%27 = load i64, i64* %4
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %58
29: ; preds = %25
%30 = load i8*, i8** %3
%31 = load i64, i64* %6
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i64
%35 = urem i64 %34, 63
%36 = getelementptr inbounds [64 x i8], [64 x i8]* @gensalt.alphabet, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %3
%39 = load i64, i64* %6
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 %37, i8* %40
%41 = load i64, i64* %4
%42 = icmp eq i64 %41, 2
br i1 %42, label %43, label %54
43: ; preds = %29
%44 = load i8*, i8** %3
%45 = load i64, i64* %6
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 95
br i1 %49, label %50, label %54
50: ; preds = %43
%51 = load i8*, i8** %3
%52 = load i64, i64* %6
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8 46, i8* %53
br label %54
54: ; preds = %50, %43, %29
br label %55
55: ; preds = %54
%56 = load i64, i64* %6
%57 = add i64 %56, 1
store i64 %57, i64* %6
br label %25
58: ; preds = %25
%59 = load i8*, i8** %3
%60 = load i64, i64* %4
%61 = getelementptr inbounds i8, i8* %59, i64 %60
store i8 0, i8* %61
%62 = load i32*, i32** %5
%63 = call i32 @fclose(i32* %62)
ret void
}

LLVM IR (-O2):
@gensalt.alphabet = internal unnamed_addr constant [64 x i8] c"abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/\00"
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [33 x i8] c"failed to open random device: %s\00"
@errno = global i32 0
@.str.3 = [30 x i8] c"failed to gen random salt: %s\00"
define void @gensalt(i8* nocapture %0, i64 %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %5, %2
%10 = tail call i32 @fread(i8* %0, i64 %1, i32 1, i32* %3)
%11 = icmp eq i32 %10, 1
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %9
%17 = icmp eq i64 %1, 0
br i1 %17, label %62, label %18
18: ; preds = %16
%19 = icmp eq i64 %1, 2
%20 = and i64 %1, 1
%21 = icmp eq i64 %1, 1
br i1 %21, label %49, label %22
22: ; preds = %18
%23 = and i64 %1, -2
br label %24
24: ; preds = %24, %22
%25 = phi i64 [ 0, %22 ], [ %46, %24 ]
%26 = phi i64 [ 0, %22 ], [ %47, %24 ]
%27 = getelementptr inbounds i8, i8* %0, i64 %25
%28 = load i8, i8* %27
%29 = sext i8 %28 to i64
%30 = urem i64 %29, 63
%31 = getelementptr inbounds [64 x i8], [64 x i8]* @gensalt.alphabet, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i64 %30, 61
%34 = select i1 %19, i1 %33, i1 false
%35 = select i1 %34, i8 46, i8 %32
store i8 %35, i8* %27
%36 = or i64 %25, 1
%37 = getelementptr inbounds i8, i8* %0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i64
%40 = urem i64 %39, 63
%41 = getelementptr inbounds [64 x i8], [64 x i8]* @gensalt.alphabet, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = icmp eq i64 %40, 61
%44 = select i1 %19, i1 %43, i1 false
%45 = select i1 %44, i8 46, i8 %42
store i8 %45, i8* %37
%46 = add nuw i64 %25, 2
%47 = add i64 %26, 2
%48 = icmp eq i64 %47, %23
br i1 %48, label %49, label %24
49: ; preds = %24, %18
%50 = phi i64 [ 0, %18 ], [ %46, %24 ]
%51 = icmp eq i64 %20, 0
br i1 %51, label %62, label %52
52: ; preds = %49
%53 = getelementptr inbounds i8, i8* %0, i64 %50
%54 = load i8, i8* %53
%55 = sext i8 %54 to i64
%56 = urem i64 %55, 63
%57 = getelementptr inbounds [64 x i8], [64 x i8]* @gensalt.alphabet, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = icmp eq i64 %56, 61
%60 = select i1 %19, i1 %59, i1 false
%61 = select i1 %60, i8 46, i8 %58
store i8 %61, i8* %53
br label %62
62: ; preds = %52, %49, %16
%63 = getelementptr inbounds i8, i8* %0, i64 %1
store i8 0, i8* %63
%64 = tail call i32 @fclose(i32* %3)
ret void
}