#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*/ buf;

/* Variables and functions */
void *BF_crypt(char const *, char const *, char *, int, int);
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ clean(char **, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memcmp(char const *, char const *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int, int);
char *_crypt_blowfish_rn(__const char *key, __const char *setting, char *output,
                         int size) {
  __const char *test_key = "8b \xd0\xc1\xd2\xcf\xcc\xd8";
  __const char *test_2a =
      "$2a$00$abcdefghijklmnopqrstuui1D709vfamulimlGcq0qq3UvuUasvEa"
      "\0"
      "canary";
  __const char *test_2x =
      "$2x$00$abcdefghijklmnopqrstuuVUrPmXD6q/nVSSp7pNDhCR9071IfIRe"
      "\0"
      "canary";
  __const char *test_hash, *p;
  int ok;
  char buf[7 + 22 + 31 + 1 + 6 + 1];
  output = BF_crypt(key, setting, output, size, 16);
  test_hash = (setting[2] == 'x') ? test_2x : test_2a;
  memcpy(buf, test_hash, sizeof(buf));
  memset(buf, -1, sizeof(buf) - (6 + 1));
  p = BF_crypt(test_key, test_hash, buf, sizeof(buf) - 6, 1);
  ok = (p == buf && !memcmp(p, test_hash, sizeof(buf)));
  clean(&buf, sizeof(buf));
  if (ok)
    return output;
  errno = (EINVAL);
  return NULL;
}


x86 assembly (clang, -O0):
<_crypt_blowfish_rn>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    0xe8f(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe8e(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xec7(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x10,%r8d
call   1070 <BF_crypt@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl 0x2(%rax),%eax
cmp    $0x78,%eax
jne    11ca <_crypt_blowfish_rn+0x7a>
mov    -0x40(%rbp),%rax
mov    %rax,-0xa8(%rbp)
jmp    11d5 <_crypt_blowfish_rn+0x85>
mov    -0x38(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0x44,%edx
call   1050 <memcpy@plt>
lea    -0xa0(%rbp),%rdi
mov    $0xffffffff,%esi
mov    $0x3d,%edx
call   1030 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    -0xa0(%rbp),%rdx
mov    $0x3e,%ecx
mov    $0x1,%r8d
call   1070 <BF_crypt@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rcx
lea    -0xa0(%rbp),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0xa9(%rbp)
jne    126a <_crypt_blowfish_rn+0x11a>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0x44,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0xa9(%rbp)
mov    -0xa9(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x54(%rbp)
lea    -0xa0(%rbp),%rdi
mov    $0x44,%esi
call   1060 <clean@plt>
cmpl   $0x0,-0x54(%rbp)
je     12a0 <_crypt_blowfish_rn+0x150>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ba <_crypt_blowfish_rn+0x16a>
mov    0x2d31(%rip),%rax
mov    (%rax),%ecx
mov    0x2d30(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_crypt_blowfish_rn>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%rbx
mov    $0x10,%r8d
call   1070 <BF_crypt@plt>
mov    %rax,%r14
cmpb   $0x78,0x2(%rbx)
lea    0xed9(%rip),%rax
lea    0xe8e(%rip),%rbx
cmove  %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x44,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
mov    $0xffffffff,%esi
mov    $0x3d,%edx
call   1030 <memset@plt>
lea    0xe54(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    $0x3e,%ecx
mov    $0x1,%r8d
call   1070 <BF_crypt@plt>
cmp    %r15,%rax
je     11d6 <_crypt_blowfish_rn+0x86>
mov    %rsp,%rdi
mov    $0x44,%esi
call   1060 <clean@plt>
jmp    11fc <_crypt_blowfish_rn+0xac>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x44,%edx
call   1040 <memcmp@plt>
mov    %eax,%ebx
mov    %r15,%rdi
mov    $0x44,%esi
call   1060 <clean@plt>
test   %ebx,%ebx
je     1211 <_crypt_blowfish_rn+0xc1>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    0x2dd4(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"8b \D0\C1\D2\CF\CC\D8\00"
@.str.1 = [68 x i8] c"$2a$00$abcdefghijklmnopqrstuui1D709vfamulimlGcq0qq3UvuUasvEa\00canary\00"
@.str.2 = [68 x i8] c"$2x$00$abcdefghijklmnopqrstuuVUrPmXD6q/nVSSp7pNDhCR9071IfIRe\00canary\00"
@EINVAL = global i32 0
@errno = global i32 0
define i8* @_crypt_blowfish_rn(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca [68 x i8]
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %10
store i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i8** %11
store i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i8** %12
%17 = load i8*, i8** %6
%18 = load i8*, i8** %7
%19 = load i8*, i8** %8
%20 = load i32, i32* %9
%21 = call i8* @BF_crypt(i8* %17, i8* %18, i8* %19, i32 %20, i32 16)
store i8* %21, i8** %8
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 2
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 120
br i1 %26, label %27, label %29
27: ; preds = %4
%28 = load i8*, i8** %12
br label %31
29: ; preds = %4
%30 = load i8*, i8** %11
br label %31
31: ; preds = %29, %27
%32 = phi i8* [ %28, %27 ], [ %30, %29 ]
store i8* %32, i8** %13
%33 = getelementptr inbounds [68 x i8], [68 x i8]* %16, i64 0, i64 0
%34 = load i8*, i8** %13
%35 = call i32 @memcpy(i8* %33, i8* %34, i32 68)
%36 = getelementptr inbounds [68 x i8], [68 x i8]* %16, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 -1, i32 61)
%38 = load i8*, i8** %10
%39 = load i8*, i8** %13
%40 = getelementptr inbounds [68 x i8], [68 x i8]* %16, i64 0, i64 0
%41 = call i8* @BF_crypt(i8* %38, i8* %39, i8* %40, i32 62, i32 1)
store i8* %41, i8** %14
%42 = load i8*, i8** %14
%43 = getelementptr inbounds [68 x i8], [68 x i8]* %16, i64 0, i64 0
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %51
45: ; preds = %31
%46 = load i8*, i8** %14
%47 = load i8*, i8** %13
%48 = call i32 @memcmp(i8* %46, i8* %47, i32 68)
%49 = icmp ne i32 %48, 0
%50 = xor i1 %49, true
br label %51
51: ; preds = %45, %31
%52 = phi i1 [ false, %31 ], [ %50, %45 ]
%53 = zext i1 %52 to i32
store i32 %53, i32* %15
%54 = bitcast [68 x i8]* %16 to i8**
%55 = call i32 @clean(i8** %54, i32 68)
%56 = load i32, i32* %15
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = load i8*, i8** %8
store i8* %59, i8** %5
br label %62
60: ; preds = %51
%61 = load i32, i32* @EINVAL
store i32 %61, i32* @errno
store i8* null, i8** %5
br label %62
62: ; preds = %60, %58
%63 = load i8*, i8** %5
ret i8* %63
}

LLVM IR (-O2):
@.str = [10 x i8] c"8b \D0\C1\D2\CF\CC\D8\00"
@.str.1 = [68 x i8] c"$2a$00$abcdefghijklmnopqrstuui1D709vfamulimlGcq0qq3UvuUasvEa\00canary\00"
@.str.2 = [68 x i8] c"$2x$00$abcdefghijklmnopqrstuuVUrPmXD6q/nVSSp7pNDhCR9071IfIRe\00canary\00"
@EINVAL = global i32 0
@errno = global i32 0
define i8* @_crypt_blowfish_rn(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [68 x i8]
%6 = getelementptr inbounds [68 x i8], [68 x i8]* %5, i64 0, i64 0
%7 = tail call i8* @BF_crypt(i8* %0, i8* %1, i8* %2, i32 %3, i32 16)
%8 = getelementptr inbounds i8, i8* %1, i64 2
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 120
%11 = select i1 %10, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0)
%12 = call i32 @memcpy(i8* %6, i8* %11, i32 68)
%13 = call i32 @memset(i8* %6, i32 -1, i32 61)
%14 = call i8* @BF_crypt(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %6, i32 62, i32 1)
%15 = icmp eq i8* %14, %6
br i1 %15, label %16, label %21
16: ; preds = %4
%17 = call i32 @memcmp(i8* %6, i8* %11, i32 68)
%18 = icmp eq i32 %17, 0
%19 = bitcast [68 x i8]* %5 to i8**
%20 = call i32 @clean(i8** %19, i32 68)
br i1 %18, label %26, label %24
21: ; preds = %4
%22 = bitcast [68 x i8]* %5 to i8**
%23 = call i32 @clean(i8** %22, i32 68)
br label %24
24: ; preds = %21, %16
%25 = load i32, i32* @EINVAL
store i32 %25, i32* @errno
br label %26
26: ; preds = %16, %24
%27 = phi i8* [ null, %24 ], [ %7, %16 ]
ret i8* %27
}