#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*/ SHA256_CTX;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_bn2bin(int /*<<< orphan*/ const *, unsigned char *);
int BN_num_bytes(int /*<<< orphan*/ const *);
int SHA256_Update(int /*<<< orphan*/ *, void const *, int);
scalar_t__ alloca(int);
int /*<<< orphan*/ bzero(unsigned char *, int);
int hash_bn(SHA256_CTX *sha, const BIGNUM *x) {
  int size = BN_num_bytes(x), ret = 0;
  if (size <= 0 || size >= 256)
    return 0;
  unsigned char *tmp = (unsigned char *)alloca(size + 1);
  *tmp = (unsigned char)size;
  if (!BN_bn2bin(x, tmp + 1))
    goto err;
  ret = SHA256_Update(sha, (const void *)tmp, size + 1);
err:
  bzero(tmp, size + 1);
  return ret;
}


x86 assembly (clang, -O0):
<hash_bn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <BN_num_bytes@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    118a <hash_bn+0x3a>
cmpl   $0x100,-0x1c(%rbp)
jl     1196 <hash_bn+0x46>
movl   $0x0,-0x4(%rbp)
jmp    11fa <hash_bn+0xaa>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1030 <alloca@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1050 <BN_bn2bin@plt>
cmp    $0x0,%eax
jne    11cf <hash_bn+0x7f>
jmp    11e5 <hash_bn+0x95>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1070 <SHA256_Update@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
call   1060 <bzero@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_bn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <BN_num_bytes@plt>
mov    %eax,%ebx
lea    -0x100(%rbx),%eax
xor    %ebp,%ebp
cmp    $0xffffff01,%eax
jb     11b6 <hash_bn+0x66>
mov    %ebx,%ebp
add    $0x1,%ebx
mov    %ebx,%edi
call   1030 <alloca@plt>
mov    %rax,%r15
mov    %bpl,(%rax)
lea    0x1(%rax),%rsi
mov    %r12,%rdi
call   1050 <BN_bn2bin@plt>
test   %eax,%eax
je     11aa <hash_bn+0x5a>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <SHA256_Update@plt>
mov    %eax,%ebp
jmp    11ac <hash_bn+0x5c>
xor    %ebp,%ebp
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <bzero@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hash_bn(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32* %0, i32** %4
store i32* %1, i32** %5
%9 = load i32*, i32** %5
%10 = call i32 @BN_num_bytes(i32* %9)
store i32 %10, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* %6
%12 = icmp sle i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp sge i32 %14, 256
br i1 %15, label %16, label %17
16: ; preds = %13, %2
store i32 0, i32* %3
br label %43
17: ; preds = %13
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = call i64 @alloca(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %8
%22 = load i32, i32* %6
%23 = trunc i32 %22 to i8
%24 = load i8*, i8** %8
store i8 %23, i8* %24
%25 = load i32*, i32** %5
%26 = load i8*, i8** %8
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = call i32 @BN_bn2bin(i32* %25, i8* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %17
br label %37
31: ; preds = %17
%32 = load i32*, i32** %4
%33 = load i8*, i8** %8
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
%36 = call i32 @SHA256_Update(i32* %32, i8* %33, i32 %35)
store i32 %36, i32* %7
br label %37
37: ; preds = %31, %30
%38 = load i8*, i8** %8
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
%41 = call i32 @bzero(i8* %38, i32 %40)
%42 = load i32, i32* %7
store i32 %42, i32* %3
br label %43
43: ; preds = %37, %16
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
define i32 @hash_bn(i32* %0, i32* %1) {
%3 = tail call i32 @BN_num_bytes(i32* %1)
%4 = add i32 %3, -256
%5 = icmp ult i32 %4, -255
br i1 %5, label %19, label %6
6: ; preds = %2
%7 = add nuw nsw i32 %3, 1
%8 = tail call i64 @alloca(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = trunc i32 %3 to i8
store i8 %10, i8* %9
%11 = getelementptr inbounds i8, i8* %9, i64 1
%12 = tail call i32 @BN_bn2bin(i32* %1, i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %6
%15 = tail call i32 @SHA256_Update(i32* %0, i8* %9, i32 %7)
br label %16
16: ; preds = %6, %14
%17 = phi i32 [ %15, %14 ], [ 0, %6 ]
%18 = tail call i32 @bzero(i8* %9, i32 %7)
br label %19
19: ; preds = %2, %16
%20 = phi i32 [ %17, %16 ], [ 0, %2 ]
ret i32 %20
}