#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_bn2bin(int /*<<< orphan*/ *, unsigned char *);
int BN_num_bytes(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int store_bignum(BIGNUM *bn, unsigned char *buf, int len) {
  int bytes = BN_num_bytes(bn);
  if (bytes > len)
    return 0;
  memset(buf, 0, len);
  BN_bn2bin(bn, buf + len - bytes);
  return 1;
}


x86 assembly (clang, -O0):
<store_bignum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <BN_num_bytes@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1167 <store_bignum+0x37>
movl   $0x0,-0x4(%rbp)
jmp    119c <store_bignum+0x6c>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
movslq -0x20(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
call   1050 <BN_bn2bin@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<store_bignum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <BN_num_bytes@plt>
mov    %eax,%ebx
xor    %eax,%eax
cmp    %ebp,%ebx
jg     1171 <store_bignum+0x41>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <memset@plt>
movslq %ebp,%rsi
add    %r14,%rsi
movslq %ebx,%rax
sub    %rax,%rsi
mov    %r15,%rdi
call   1050 <BN_bn2bin@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @store_bignum(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32*, i32** %5
%10 = call i32 @BN_num_bytes(i32* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 0, i32* %4
br label %29
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = call i32 @memset(i8* %16, i32 0, i32 %17)
%19 = load i32*, i32** %5
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = sub i64 0, %25
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = call i32 @BN_bn2bin(i32* %19, i8* %27)
store i32 1, i32* %4
br label %29
29: ; preds = %15, %14
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
define i32 @store_bignum(i32* %0, i8* %1, i32 %2) {
%4 = tail call i32 @BN_num_bytes(i32* %0)
%5 = icmp sgt i32 %4, %2
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%8 = sext i32 %2 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sext i32 %4 to i64
%11 = sub nsw i64 0, %10
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = tail call i32 @BN_bn2bin(i32* %0, i8* %12)
br label %14
14: ; preds = %3, %6
%15 = phi i32 [ 1, %6 ], [ 0, %3 ]
ret i32 %15
}