#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 */
unsigned int BN_bn2mpi(int /*<<< orphan*/ *, unsigned char *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, unsigned int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, unsigned int);
scalar_t__ reverse(unsigned char *, unsigned int);
int make_big_endian_get(BIGNUM *num, unsigned char **buff) {
  unsigned char *lbuff = NULL;
  unsigned int num_size = BN_bn2mpi(num, NULL);
  if (num_size <= 4)
    return 0;
  lbuff = (unsigned char *)malloc(num_size);
  if (lbuff == NULL)
    return 0;
  memset(lbuff, 0, num_size);
  BN_bn2mpi(num, &lbuff[0]);
  memcpy(lbuff, (lbuff + 4), (num_size - 4));
  if (reverse(lbuff, (num_size - 4)) == 0) {
    free(lbuff);
    return 0;
  }
  *buff = lbuff;
  return (num_size - 4);
}


x86 assembly (clang, -O0):
<make_big_endian_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <BN_bn2mpi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
ja     119e <make_big_endian_get+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    1235 <make_big_endian_get+0xd5>
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c1 <make_big_endian_get+0x61>
movl   $0x0,-0x4(%rbp)
jmp    1235 <make_big_endian_get+0xd5>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <BN_bn2mpi@plt>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    -0x24(%rbp),%edx
sub    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
sub    $0x4,%esi
call   1030 <reverse@plt>
cmp    $0x0,%rax
jne    1221 <make_big_endian_get+0xc1>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1235 <make_big_endian_get+0xd5>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_big_endian_get>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
xor    %r15d,%r15d
xor    %esi,%esi
call   1080 <BN_bn2mpi@plt>
cmp    $0x5,%eax
jb     11db <make_big_endian_get+0x7b>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11db <make_big_endian_get+0x7b>
mov    %rax,%rbx
xor    %r15d,%r15d
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <BN_bn2mpi@plt>
mov    %rbx,%rsi
add    $0x4,%rsi
add    $0xfffffffc,%ebp
mov    %rbx,%rdi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <reverse@plt>
test   %rax,%rax
je     11d3 <make_big_endian_get+0x73>
mov    %rbx,(%r14)
mov    %ebp,%r15d
jmp    11db <make_big_endian_get+0x7b>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @make_big_endian_get(i32* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i8* null, i8** %6
%8 = load i32*, i32** %4
%9 = call i32 @BN_bn2mpi(i32* %8, i8* null)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ule i32 %10, 4
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %47
13: ; preds = %2
%14 = load i32, i32* %7
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 0, i32* %3
br label %47
20: ; preds = %13
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @memset(i8* %21, i32 0, i32 %22)
%24 = load i32*, i32** %4
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = call i32 @BN_bn2mpi(i32* %24, i8* %26)
%28 = load i8*, i8** %6
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i64 4
%31 = load i32, i32* %7
%32 = sub i32 %31, 4
%33 = call i32 @memcpy(i8* %28, i8* %30, i32 %32)
%34 = load i8*, i8** %6
%35 = load i32, i32* %7
%36 = sub i32 %35, 4
%37 = call i64 @reverse(i8* %34, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %20
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
store i32 0, i32* %3
br label %47
42: ; preds = %20
%43 = load i8*, i8** %6
%44 = load i8**, i8*** %5
store i8* %43, i8** %44
%45 = load i32, i32* %7
%46 = sub i32 %45, 4
store i32 %46, i32* %3
br label %47
47: ; preds = %42, %39, %19, %12
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
define i32 @make_big_endian_get(i32* %0, i8** nocapture writeonly %1) {
%3 = tail call i32 @BN_bn2mpi(i32* %0, i8* null)
%4 = icmp ult i32 %3, 5
br i1 %4, label %20, label %5
5: ; preds = %2
%6 = tail call i64 @malloc(i32 %3)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %20, label %9
9: ; preds = %5
%10 = tail call i32 @memset(i8* %7, i32 0, i32 %3)
%11 = tail call i32 @BN_bn2mpi(i32* %0, i8* %7)
%12 = getelementptr inbounds i8, i8* %7, i64 4
%13 = add i32 %3, -4
%14 = tail call i32 @memcpy(i8* %7, i8* %12, i32 %13)
%15 = tail call i64 @reverse(i8* %7, i32 %13)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = tail call i32 @free(i8* %7)
br label %20
19: ; preds = %9
store i8* %7, i8** %1
br label %20
20: ; preds = %5, %2, %19, %17
%21 = phi i32 [ 0, %17 ], [ %13, %19 ], [ 0, %2 ], [ 0, %5 ]
ret i32 %21
}