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

/* Variables and functions */
int /*<<< orphan*/ BN_bin2bn(unsigned char const *, int, int /*<<< orphan*/ *);
int BN_num_bits(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_rshift(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ ECDSAerror(int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_BN_LIB;
int ecdsa_prepare_digest(const unsigned char *dgst, int dgst_len, BIGNUM *order,
                         BIGNUM *ret) {
  int dgst_bits, order_bits;
  if (!BN_bin2bn(dgst, dgst_len, ret)) {
    ECDSAerror(ERR_R_BN_LIB);
    return 0;
  }
  dgst_bits = 8 * dgst_len;
  order_bits = BN_num_bits(order);
  if (dgst_bits > order_bits) {
    if (!BN_rshift(ret, ret, dgst_bits - order_bits)) {
      ECDSAerror(ERR_R_BN_LIB);
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<ecdsa_prepare_digest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1040 <BN_bin2bn@plt>
cmp    $0x0,%eax
jne    118a <ecdsa_prepare_digest+0x4a>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1050 <ECDSAerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ed <ecdsa_prepare_digest+0xad>
mov    -0x14(%rbp),%eax
shl    $0x3,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <BN_num_bits@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jle    11e6 <ecdsa_prepare_digest+0xa6>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
sub    -0x30(%rbp),%edx
call   1060 <BN_rshift@plt>
cmp    $0x0,%eax
jne    11e1 <ecdsa_prepare_digest+0xa1>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1050 <ECDSAerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ed <ecdsa_prepare_digest+0xad>
jmp    11e6 <ecdsa_prepare_digest+0xa6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ecdsa_prepare_digest>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rcx,%rdx
call   1040 <BN_bin2bn@plt>
test   %eax,%eax
je     117e <ecdsa_prepare_digest+0x3e>
shl    $0x3,%ebp
mov    %r14,%rdi
call   1030 <BN_num_bits@plt>
mov    $0x1,%r14d
sub    %eax,%ebp
jle    118f <ecdsa_prepare_digest+0x4f>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <BN_rshift@plt>
test   %eax,%eax
jne    118f <ecdsa_prepare_digest+0x4f>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1050 <ECDSAerror@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@ERR_R_BN_LIB = global i32 0
define i32 @ecdsa_prepare_digest(i8* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = load i32*, i32** %9
%15 = call i32 @BN_bin2bn(i8* %12, i32 %13, i32* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %4
%18 = load i32, i32* @ERR_R_BN_LIB
%19 = call i32 @ECDSAerror(i32 %18)
store i32 0, i32* %5
br label %41
20: ; preds = %4
%21 = load i32, i32* %7
%22 = mul nsw i32 8, %21
store i32 %22, i32* %10
%23 = load i32*, i32** %8
%24 = call i32 @BN_num_bits(i32* %23)
store i32 %24, i32* %11
%25 = load i32, i32* %10
%26 = load i32, i32* %11
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %40
28: ; preds = %20
%29 = load i32*, i32** %9
%30 = load i32*, i32** %9
%31 = load i32, i32* %10
%32 = load i32, i32* %11
%33 = sub nsw i32 %31, %32
%34 = call i32 @BN_rshift(i32* %29, i32* %30, i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %28
%37 = load i32, i32* @ERR_R_BN_LIB
%38 = call i32 @ECDSAerror(i32 %37)
store i32 0, i32* %5
br label %41
39: ; preds = %28
br label %40
40: ; preds = %39, %20
store i32 1, i32* %5
br label %41
41: ; preds = %40, %36, %17
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@ERR_R_BN_LIB = global i32 0
define i32 @ecdsa_prepare_digest(i8* %0, i32 %1, i32* %2, i32* %3) {
%5 = tail call i32 @BN_bin2bn(i8* %0, i32 %1, i32* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %4
%8 = shl nsw i32 %1, 3
%9 = tail call i32 @BN_num_bits(i32* %2)
%10 = icmp sgt i32 %8, %9
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = sub nsw i32 %8, %9
%13 = tail call i32 @BN_rshift(i32* %3, i32* %3, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11, %4
%16 = load i32, i32* @ERR_R_BN_LIB
%17 = tail call i32 @ECDSAerror(i32 %16)
br label %18
18: ; preds = %15, %7, %11
%19 = phi i32 [ 1, %11 ], [ 1, %7 ], [ 0, %15 ]
ret i32 %19
}