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

/* Variables and functions */
int /*<<< orphan*/ BN_bn2bin(int /*<<< orphan*/ const *, unsigned char *);
int BN_num_bytes(int /*<<< orphan*/ const *);
scalar_t__ alloca(int);
int /*<<< orphan*/ bzero(unsigned char *, int);
int send(int, void const *, int, int /*<<< orphan*/);
int send_bn(int socket, const BIGNUM *x) {
  int size = BN_num_bytes(x), ret = 0;
  if (size <= 0 || size >= 256)
    return 0;
  unsigned char *tmp = (unsigned char *)alloca(size + 1);
  *tmp = (unsigned char)size;
  if (!BN_bn2bin(x, tmp + 1))
    goto err;
  ssize_t written = 0;
  do {
    ssize_t wr =
        send(socket, (const void *)(tmp + written), size + 1 - written, 0);
    if (wr < 0)
      goto err;
    written += wr;
  } while (written < size + 1);
  ret = 1;
err:
  bzero(tmp, size + 1);
  return ret;
}


x86 assembly (clang, -O0):
<send_bn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <BN_num_bytes@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1189 <send_bn+0x39>
cmpl   $0x100,-0x14(%rbp)
jl     1195 <send_bn+0x45>
movl   $0x0,-0x4(%rbp)
jmp    123b <send_bn+0xeb>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1040 <alloca@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1060 <BN_bn2bin@plt>
cmp    $0x0,%eax
jne    11ce <send_bn+0x7e>
jmp    1226 <send_bn+0xd6>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
sub    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1205 <send_bn+0xb5>
jmp    1226 <send_bn+0xd6>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jl     11d5 <send_bn+0x85>
movl   $0x1,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1070 <bzero@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_bn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <BN_num_bytes@plt>
mov    %eax,%ebx
lea    -0x100(%rbx),%eax
xor    %ebp,%ebp
cmp    $0xffffff01,%eax
jb     11d4 <send_bn+0x84>
lea    0x1(%rbx),%r15d
mov    %r15d,%edi
call   1040 <alloca@plt>
mov    %rax,%r12
mov    %bl,(%rax)
lea    0x1(%rax),%rsi
mov    %r13,%rdi
call   1060 <BN_bn2bin@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11c9 <send_bn+0x79>
nopl   (%rax)
movslq %ebp,%rax
lea    (%r12,%rax,1),%rsi
mov    %r15d,%edx
sub    %eax,%edx
mov    %r14d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     11c7 <send_bn+0x77>
add    %eax,%ebp
cmp    %ebx,%ebp
jle    11a0 <send_bn+0x50>
mov    $0x1,%ebp
jmp    11c9 <send_bn+0x79>
xor    %ebp,%ebp
mov    %r12,%rdi
mov    %r15d,%esi
call   1070 <bzero@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send_bn(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%11 = load i32*, i32** %5
%12 = call i32 @BN_num_bytes(i32* %11)
store i32 %12, i32* %6
store i32 0, i32* %7
%13 = load i32, i32* %6
%14 = icmp sle i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp sge i32 %16, 256
br i1 %17, label %18, label %19
18: ; preds = %15, %2
store i32 0, i32* %3
br label %64
19: ; preds = %15
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
%22 = call i64 @alloca(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %8
%24 = load i32, i32* %6
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %8
store i8 %25, i8* %26
%27 = load i32*, i32** %5
%28 = load i8*, i8** %8
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = call i32 @BN_bn2bin(i32* %27, i8* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %19
br label %58
33: ; preds = %19
store i32 0, i32* %9
br label %34
34: ; preds = %52, %33
%35 = load i32, i32* %4
%36 = load i8*, i8** %8
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
%42 = load i32, i32* %9
%43 = sub nsw i32 %41, %42
%44 = call i32 @send(i32 %35, i8* %39, i32 %43, i32 0)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %34
br label %58
48: ; preds = %34
%49 = load i32, i32* %10
%50 = load i32, i32* %9
%51 = add nsw i32 %50, %49
store i32 %51, i32* %9
br label %52
52: ; preds = %48
%53 = load i32, i32* %9
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
%56 = icmp slt i32 %53, %55
br i1 %56, label %34, label %57
57: ; preds = %52
store i32 1, i32* %7
br label %58
58: ; preds = %57, %47, %32
%59 = load i8*, i8** %8
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
%62 = call i32 @bzero(i8* %59, i32 %61)
%63 = load i32, i32* %7
store i32 %63, i32* %3
br label %64
64: ; preds = %58, %18
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
define i32 @send_bn(i32 %0, i32* %1) {
%3 = tail call i32 @BN_num_bytes(i32* %1)
%4 = add i32 %3, -256
%5 = icmp ult i32 %4, -255
br i1 %5, label %27, label %6
6: ; preds = %2
%7 = add nuw nsw i32 %3, 1
%8 = tail call i64 @alloca(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = trunc i32 %3 to i8
store i8 %10, i8* %9
%11 = getelementptr inbounds i8, i8* %9, i64 1
%12 = tail call i32 @BN_bn2bin(i32* %1, i8* %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %6, %22
%15 = phi i32 [ %21, %22 ], [ 0, %6 ]
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %9, i64 %16
%18 = sub nsw i32 %7, %15
%19 = tail call i32 @send(i32 %0, i8* %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
%21 = add nsw i32 %19, %15
br i1 %20, label %24, label %22
22: ; preds = %14
%23 = icmp sgt i32 %21, %3
br i1 %23, label %24, label %14
24: ; preds = %14, %22, %6
%25 = phi i32 [ 0, %6 ], [ 1, %22 ], [ 0, %14 ]
%26 = tail call i32 @bzero(i8* %9, i32 %7)
br label %27
27: ; preds = %24, %2
%28 = phi i32 [ 0, %2 ], [ %25, %24 ]
ret i32 %28
}