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

/* Variables and functions */
int /*<<< orphan*/ *BN_bin2bn(int /*<<< orphan*/ *, int, int /*<<< orphan*/ *);
int be32toh(int);
int lootr_decode_mpi(uint8_t *buf, uint32_t buf_length, BIGNUM **res,
                     uint32_t *bytes_read) {
  BIGNUM *p;
  uint32_t length;
  if (buf_length < sizeof(uint32_t))
    return -2;
  length = be32toh(*(uint32_t *)buf);
  if (buf_length < sizeof(uint32_t) + length)
    return -2;
  p = BN_bin2bn(buf + 4, length, *res);
  if (!p)
    return -4;
  *res = p;
  *bytes_read = length + 4;
  return 0;
}


x86 assembly (clang, -O0):
<lootr_decode_mpi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jae    1151 <lootr_decode_mpi+0x31>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d0 <lootr_decode_mpi+0xb0>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <be32toh@plt>
mov    %eax,-0x34(%rbp)
movslq -0x14(%rbp),%rax
movslq -0x34(%rbp),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jae    1180 <lootr_decode_mpi+0x60>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d0 <lootr_decode_mpi+0xb0>
mov    -0x10(%rbp),%rdi
add    $0x10,%rdi
mov    -0x34(%rbp),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
call   1030 <BN_bin2bn@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b2 <lootr_decode_mpi+0x92>
movl   $0xfffffffc,-0x4(%rbp)
jmp    11d0 <lootr_decode_mpi+0xb0>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%ecx
add    $0x4,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lootr_decode_mpi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xfffffffe,%ebp
cmp    $0x4,%esi
jb     1180 <lootr_decode_mpi+0x60>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%rbx
movslq %esi,%r13
mov    (%rdi),%edi
call   1040 <be32toh@plt>
mov    %eax,%r12d
cltq
add    $0x4,%rax
cmp    %r13,%rax
ja     1180 <lootr_decode_mpi+0x60>
add    $0x10,%rbx
mov    (%r15),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <BN_bin2bn@plt>
test   %rax,%rax
je     117b <lootr_decode_mpi+0x5b>
mov    %rax,(%r15)
add    $0x4,%r12d
mov    %r12d,(%r14)
xor    %ebp,%ebp
jmp    1180 <lootr_decode_mpi+0x60>
mov    $0xfffffffc,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @lootr_decode_mpi(i32* %0, i32 %1, i32** nocapture %2, i32* nocapture writeonly %3) {
%5 = icmp ult i32 %1, 4
br i1 %5, label %20, label %6
6: ; preds = %4
%7 = sext i32 %1 to i64
%8 = load i32, i32* %0
%9 = tail call i32 @be32toh(i32 %8)
%10 = sext i32 %9 to i64
%11 = add nsw i64 %10, 4
%12 = icmp ugt i64 %11, %7
br i1 %12, label %20, label %13
13: ; preds = %6
%14 = getelementptr inbounds i32, i32* %0, i64 4
%15 = load i32*, i32** %2
%16 = tail call i32* @BN_bin2bn(i32* %14, i32 %9, i32* %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %13
store i32* %16, i32** %2
%19 = add nsw i32 %9, 4
store i32 %19, i32* %3
br label %20
20: ; preds = %13, %6, %4, %18
%21 = phi i32 [ 0, %18 ], [ -2, %4 ], [ -2, %6 ], [ -4, %13 ]
ret i32 %21
}