#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*/ uint8_t;
typedef int /*<<< orphan*/ csalt;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ arc4random_buf(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ encode_base64(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
int bcrypt_initsalt(int log_rounds, uint8_t *salt, size_t saltbuflen) {
  uint8_t csalt[16];
  if (saltbuflen < (7 + (16 * 4 + 2) / 3 + 1)) {
    errno = EINVAL;
    return -1;
  }
  arc4random_buf(csalt, sizeof(csalt));
  if (log_rounds < 4)
    log_rounds = 4;
  else if (log_rounds > 31)
    log_rounds = 31;
  snprintf((char *)salt, saltbuflen, "$2b$%2.2u$", log_rounds);
  encode_base64((char *)salt + 7, csalt, sizeof(csalt));
  return 0;
}


x86 assembly (clang, -O0):
<bcrypt_initsalt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x1e,-0x18(%rbp)
jae    116c <bcrypt_initsalt+0x3c>
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
mov    0x2e82(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <bcrypt_initsalt+0xaa>
lea    -0x60(%rbp),%rdi
mov    $0x40,%esi
call   1030 <arc4random_buf@plt>
cmpl   $0x4,-0x8(%rbp)
jge    1190 <bcrypt_initsalt+0x60>
movl   $0x4,-0x8(%rbp)
jmp    11a6 <bcrypt_initsalt+0x76>
cmpl   $0x1f,-0x8(%rbp)
jle    11a1 <bcrypt_initsalt+0x71>
movl   $0x1f,-0x8(%rbp)
jmp    11a6 <bcrypt_initsalt+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%ecx
lea    0xe48(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1050 <encode_base64@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcrypt_initsalt>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
cmp    $0x1d,%rdx
ja     1159 <bcrypt_initsalt+0x29>
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    0x2e90(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    11af <bcrypt_initsalt+0x7f>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x40,%esi
call   1030 <arc4random_buf@plt>
cmp    $0x1f,%ebp
mov    $0x1f,%eax
cmovl  %ebp,%eax
cmp    $0x5,%eax
mov    $0x4,%ecx
cmovge %eax,%ecx
lea    0xe72(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <snprintf@plt>
add    $0x7,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x40,%edx
call   1050 <encode_base64@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"$2b$%2.2u$\00"
define i32 @bcrypt_initsalt(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca [16 x i32]
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
%9 = load i64, i64* %7
%10 = icmp ult i64 %9, 30
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i32 -1, i32* %4
br label %35
13: ; preds = %3
%14 = getelementptr inbounds [16 x i32], [16 x i32]* %8, i64 0, i64 0
%15 = call i32 @arc4random_buf(i32* %14, i32 64)
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 4
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 4, i32* %5
br label %24
19: ; preds = %13
%20 = load i32, i32* %5
%21 = icmp sgt i32 %20, 31
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 31, i32* %5
br label %23
23: ; preds = %22, %19
br label %24
24: ; preds = %23, %18
%25 = load i32*, i32** %6
%26 = bitcast i32* %25 to i8*
%27 = load i64, i64* %7
%28 = load i32, i32* %5
%29 = call i32 @snprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %6
%31 = bitcast i32* %30 to i8*
%32 = getelementptr inbounds i8, i8* %31, i64 7
%33 = getelementptr inbounds [16 x i32], [16 x i32]* %8, i64 0, i64 0
%34 = call i32 @encode_base64(i8* %32, i32* %33, i32 64)
store i32 0, i32* %4
br label %35
35: ; preds = %24, %11
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"$2b$%2.2u$\00"
define i32 @bcrypt_initsalt(i32 %0, i32* %1, i64 %2) {
%4 = alloca [16 x i32]
%5 = bitcast [16 x i32]* %4 to i8*
%6 = icmp ult i64 %2, 30
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32, i32* @EINVAL
store i32 %8, i32* @errno
br label %20
9: ; preds = %3
%10 = getelementptr inbounds [16 x i32], [16 x i32]* %4, i64 0, i64 0
%11 = call i32 @arc4random_buf(i32* %10, i32 64)
%12 = icmp slt i32 %0, 31
%13 = select i1 %12, i32 %0, i32 31
%14 = icmp sgt i32 %13, 4
%15 = select i1 %14, i32 %13, i32 4
%16 = bitcast i32* %1 to i8*
%17 = call i32 @snprintf(i8* %16, i64 %2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
%18 = getelementptr inbounds i8, i8* %16, i64 7
%19 = call i32 @encode_base64(i8* %18, i32* %10, i32 64)
br label %20
20: ; preds = %9, %7
%21 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %21
}