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

/* Variables and functions */
scalar_t__ BN_bn2bin(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ BN_num_bytes(int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
scalar_t__ htobe32(scalar_t__);
int lootr_encode_mpi(BIGNUM *mpi, uint8_t *buf, uint32_t buf_length,
                     uint32_t *bytes_written) {
  uint32_t length = BN_num_bytes(mpi);
  uint32_t needed = length + sizeof(uint32_t);
  if (!buf || buf_length < needed)
    return needed;
  *(uint32_t *)buf = htobe32(length);
  *bytes_written = BN_bn2bin(mpi, buf + sizeof(uint32_t));
  assert(*bytes_written == length);
  *bytes_written += sizeof(uint32_t);
  return 0;
}


x86 assembly (clang, -O0):
<lootr_encode_mpi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BN_num_bytes@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118a <lootr_encode_mpi+0x4a>
mov    -0x20(%rbp),%rax
cmp    -0x38(%rbp),%rax
jge    1196 <lootr_encode_mpi+0x56>
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11f1 <lootr_encode_mpi+0xb1>
mov    -0x30(%rbp),%rdi
call   1040 <htobe32@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x20,%rsi
call   1060 <BN_bn2bin@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmp    -0x30(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
add    $0x8,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lootr_encode_mpi>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rdi,%r12
call   1050 <BN_num_bytes@plt>
mov    %rax,%r15
add    $0x8,%rax
test   %rbx,%rbx
je     119c <lootr_encode_mpi+0x5c>
cmp    %r13,%rax
jg     119c <lootr_encode_mpi+0x5c>
mov    %r15,%rdi
call   1040 <htobe32@plt>
mov    %rax,(%rbx)
add    $0x20,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <BN_bn2bin@plt>
mov    %rax,(%r14)
xor    %edi,%edi
cmp    %r15,%rax
sete   %dil
call   1030 <assert@plt>
addq   $0x8,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @lootr_encode_mpi(i32* %0, i32* %1, i64 %2, i64* nocapture %3) {
%5 = tail call i64 @BN_num_bytes(i32* %0)
%6 = add i64 %5, 8
%7 = icmp eq i32* %1, null
%8 = icmp sgt i64 %6, %2
%9 = select i1 %7, i1 true, i1 %8
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = trunc i64 %6 to i32
br label %22
12: ; preds = %4
%13 = tail call i64 @htobe32(i64 %5)
%14 = bitcast i32* %1 to i64*
store i64 %13, i64* %14
%15 = getelementptr inbounds i32, i32* %1, i64 8
%16 = tail call i64 @BN_bn2bin(i32* %0, i32* %15)
store i64 %16, i64* %3
%17 = icmp eq i64 %16, %5
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = load i64, i64* %3
%21 = add i64 %20, 8
store i64 %21, i64* %3
br label %22
22: ; preds = %12, %10
%23 = phi i32 [ %11, %10 ], [ 0, %12 ]
ret i32 %23
}