#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 u_int16_t;
typedef scalar_t__ u_char;
struct sshbuf {
  int dummy;
};
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ *BN_bin2bn(scalar_t__ const *, size_t, int /*<<< orphan*/ *);
scalar_t__ sshbuf_consume(struct sshbuf *, int);
int sshbuf_len(struct sshbuf *);
scalar_t__ *sshbuf_ptr(struct sshbuf *);
int sshbuf_get_bignum1(struct sshbuf *buf, BIGNUM *v) {
  const u_char *d = sshbuf_ptr(buf);
  u_int16_t len_bits;
  size_t len_bytes;
  if (sshbuf_len(buf) < 2)
    return -3;
  len_bits = (((u_int16_t)(((const u_char *)(d))[0]) << 8) |
              (u_int16_t)(((const u_char *)(d))[1]));
  len_bytes = (len_bits + 7) >> 3;
  if (len_bytes > (16384 / 8))
    return -7;
  if (sshbuf_len(buf) < 2 + len_bytes)
    return -3;
  if (v != NULL && BN_bin2bn(d + 2, len_bytes, v) == NULL)
    return -2;
  if (sshbuf_consume(buf, 2 + len_bytes) != 0) {
    ;
    ;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sshbuf_get_bignum1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sshbuf_ptr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <sshbuf_len@plt>
cmp    $0x2,%eax
jge    117b <sshbuf_get_bignum1+0x3b>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1249 <sshbuf_get_bignum1+0x109>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
shl    $0x8,%eax
mov    -0x20(%rbp),%rcx
mov    0x8(%rcx),%rcx
or     %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x7,%eax
sar    $0x3,%eax
cltq
mov    %rax,-0x30(%rbp)
cmpq   $0x800,-0x30(%rbp)
jbe    11bb <sshbuf_get_bignum1+0x7b>
movl   $0xfffffff9,-0x4(%rbp)
jmp    1249 <sshbuf_get_bignum1+0x109>
mov    -0x10(%rbp),%rdi
call   1030 <sshbuf_len@plt>
cltq
mov    -0x30(%rbp),%rcx
add    $0x2,%rcx
cmp    %rcx,%rax
jae    11e3 <sshbuf_get_bignum1+0xa3>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1249 <sshbuf_get_bignum1+0x109>
cmpq   $0x0,-0x18(%rbp)
je     1219 <sshbuf_get_bignum1+0xd9>
mov    -0x20(%rbp),%rdi
add    $0x10,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <BN_bin2bn@plt>
cmp    $0x0,%rax
jne    1219 <sshbuf_get_bignum1+0xd9>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1249 <sshbuf_get_bignum1+0x109>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %eax,%esi
call   1050 <sshbuf_consume@plt>
cmp    $0x0,%rax
je     1242 <sshbuf_get_bignum1+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <sshbuf_get_bignum1+0x109>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sshbuf_get_bignum1>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1060 <sshbuf_ptr@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <sshbuf_len@plt>
mov    %eax,%ecx
mov    $0xfffffffd,%eax
cmp    $0x2,%ecx
jl     11d7 <sshbuf_get_bignum1+0x97>
mov    (%r15),%ecx
shl    $0x8,%ecx
or     0x8(%r15),%ecx
add    $0x7,%ecx
mov    $0xfffffff9,%eax
cmp    $0x4007,%ecx
ja     11d7 <sshbuf_get_bignum1+0x97>
sar    $0x3,%ecx
movslq %ecx,%r13
mov    %rbx,%rdi
call   1030 <sshbuf_len@plt>
movslq %eax,%rcx
lea    0x2(%r13),%r12
mov    $0xfffffffd,%eax
cmp    %rcx,%r12
ja     11d7 <sshbuf_get_bignum1+0x97>
test   %r14,%r14
je     11c0 <sshbuf_get_bignum1+0x80>
add    $0x10,%r15
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1040 <BN_bin2bn@plt>
test   %rax,%rax
je     11d2 <sshbuf_get_bignum1+0x92>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <sshbuf_consume@plt>
neg    %rax
sbb    %eax,%eax
jmp    11d7 <sshbuf_get_bignum1+0x97>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_bignum1(%struct.sshbuf* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.sshbuf*
%5 = alloca i32*
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i64
store %struct.sshbuf* %0, %struct.sshbuf** %4
store i32* %1, i32** %5
%9 = load %struct.sshbuf*, %struct.sshbuf** %4
%10 = call i64* @sshbuf_ptr(%struct.sshbuf* %9)
store i64* %10, i64** %6
%11 = load %struct.sshbuf*, %struct.sshbuf** %4
%12 = call i32 @sshbuf_len(%struct.sshbuf* %11)
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -3, i32* %3
br label %61
15: ; preds = %2
%16 = load i64*, i64** %6
%17 = getelementptr inbounds i64, i64* %16, i64 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = shl i32 %19, 8
%21 = load i64*, i64** %6
%22 = getelementptr inbounds i64, i64* %21, i64 1
%23 = load i64, i64* %22
%24 = trunc i64 %23 to i32
%25 = or i32 %20, %24
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 7
%28 = ashr i32 %27, 3
%29 = sext i32 %28 to i64
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = icmp ugt i64 %30, 2048
br i1 %31, label %32, label %33
32: ; preds = %15
store i32 -7, i32* %3
br label %61
33: ; preds = %15
%34 = load %struct.sshbuf*, %struct.sshbuf** %4
%35 = call i32 @sshbuf_len(%struct.sshbuf* %34)
%36 = sext i32 %35 to i64
%37 = load i64, i64* %8
%38 = add i64 2, %37
%39 = icmp ult i64 %36, %38
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 -3, i32* %3
br label %61
41: ; preds = %33
%42 = load i32*, i32** %5
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = load i64*, i64** %6
%46 = getelementptr inbounds i64, i64* %45, i64 2
%47 = load i64, i64* %8
%48 = load i32*, i32** %5
%49 = call i32* @BN_bin2bn(i64* %46, i64 %47, i32* %48)
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %52
51: ; preds = %44
store i32 -2, i32* %3
br label %61
52: ; preds = %44, %41
%53 = load %struct.sshbuf*, %struct.sshbuf** %4
%54 = load i64, i64* %8
%55 = add i64 2, %54
%56 = trunc i64 %55 to i32
%57 = call i64 @sshbuf_consume(%struct.sshbuf* %53, i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %52
store i32 -1, i32* %3
br label %61
60: ; preds = %52
store i32 0, i32* %3
br label %61
61: ; preds = %60, %59, %51, %40, %32, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sshbuf = type { i32 }
define i32 @sshbuf_get_bignum1(%struct.sshbuf* %0, i32* %1) {
%3 = tail call i64* @sshbuf_ptr(%struct.sshbuf* %0)
%4 = tail call i32 @sshbuf_len(%struct.sshbuf* %0)
%5 = icmp slt i32 %4, 2
br i1 %5, label %34, label %6
6: ; preds = %2
%7 = load i64, i64* %3
%8 = trunc i64 %7 to i32
%9 = shl i32 %8, 8
%10 = getelementptr inbounds i64, i64* %3, i64 1
%11 = load i64, i64* %10
%12 = trunc i64 %11 to i32
%13 = or i32 %9, %12
%14 = add nsw i32 %13, 7
%15 = ashr i32 %14, 3
%16 = sext i32 %15 to i64
%17 = icmp ugt i32 %14, 16391
br i1 %17, label %34, label %18
18: ; preds = %6
%19 = tail call i32 @sshbuf_len(%struct.sshbuf* %0)
%20 = sext i32 %19 to i64
%21 = add nsw i64 %16, 2
%22 = icmp ugt i64 %21, %20
br i1 %22, label %34, label %23
23: ; preds = %18
%24 = icmp eq i32* %1, null
br i1 %24, label %29, label %25
25: ; preds = %23
%26 = getelementptr inbounds i64, i64* %3, i64 2
%27 = tail call i32* @BN_bin2bn(i64* %26, i64 %16, i32* %1)
%28 = icmp eq i32* %27, null
br i1 %28, label %34, label %29
29: ; preds = %25, %23
%30 = trunc i64 %21 to i32
%31 = tail call i64 @sshbuf_consume(%struct.sshbuf* %0, i32 %30)
%32 = icmp ne i64 %31, 0
%33 = sext i1 %32 to i32
br label %34
34: ; preds = %29, %25, %18, %6, %2
%35 = phi i32 [ -3, %2 ], [ -7, %6 ], [ -3, %18 ], [ -2, %25 ], [ %33, %29 ]
ret i32 %35
}