#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 scalar_t__ point_conversion_form_t;
typedef int /*<<< orphan*/ EC_POINT;
typedef int /*<<< orphan*/ EC_GROUP;
typedef int /*<<< orphan*/ BN_CTX;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_end(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_CTX_start(int /*<<< orphan*/ *);
int BN_bn2bin(int /*<<< orphan*/ *, unsigned char *);
scalar_t__ BN_is_odd(int /*<<< orphan*/ *);
int BN_num_bytes(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_get_Jprojective_coordinates_GFp(
    int /*<<< orphan*/ const *, int /*<<< orphan*/ const *,
    int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
    int /*<<< orphan*/ *);
scalar_t__ POINT_CONVERSION_UNCOMPRESSED;
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, size_t);
size_t struct_EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
                                 point_conversion_form_t form,
                                 unsigned char *buf, size_t len, BN_CTX *ctx) {
  BIGNUM *X;
  BIGNUM *Y;
  int length;
  BN_CTX_start(ctx);
  X = BN_CTX_get(ctx);
  Y = BN_CTX_get(ctx);
  EC_POINT_get_Jprojective_coordinates_GFp(group, p, X, Y, NULL, ctx);
  BN_CTX_end(ctx);
  length = BN_num_bytes(X);
  memset(buf, 0, len);
  length = BN_bn2bin(X, buf + (33 - length));
  if (form == POINT_CONVERSION_UNCOMPRESSED) {
    length = BN_num_bytes(Y);
    length = BN_bn2bin(Y, buf + (65 - length));
    buf[0] = 0x04;
    length = 65;
  } else {
    if (BN_is_odd(Y)) {
      buf[0] = 0x03;
    } else {
      buf[0] = 0x02;
    }
    length = 33;
  }
  return (length);
}


x86 assembly (clang, -O0):
<struct_EC_POINT_point2oct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <BN_CTX_start@plt>
mov    -0x30(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%r9
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <EC_POINT_get_Jprojective_coordinates_GFp@plt>
mov    -0x30(%rbp),%rdi
call   1060 <BN_CTX_end@plt>
mov    -0x38(%rbp),%rdi
call   1080 <BN_num_bytes@plt>
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x21,%eax
sub    -0x44(%rbp),%eax
cltq
add    %rax,%rsi
call   1090 <BN_bn2bin@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    1272 <struct_EC_POINT_point2oct+0xf2>
mov    -0x40(%rbp),%rdi
call   1080 <BN_num_bytes@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x41,%eax
sub    -0x44(%rbp),%eax
cltq
add    %rax,%rsi
call   1090 <BN_bn2bin@plt>
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x4,(%rax)
movl   $0x41,-0x44(%rbp)
jmp    129f <struct_EC_POINT_point2oct+0x11f>
mov    -0x40(%rbp),%rdi
call   10a0 <BN_is_odd@plt>
cmp    $0x0,%rax
je     1291 <struct_EC_POINT_point2oct+0x111>
mov    -0x20(%rbp),%rax
movb   $0x3,(%rax)
jmp    1298 <struct_EC_POINT_point2oct+0x118>
mov    -0x20(%rbp),%rax
movb   $0x2,(%rax)
movl   $0x21,-0x44(%rbp)
movslq -0x44(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<struct_EC_POINT_point2oct>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%rbp
mov    %r8,0x8(%rsp)
mov    %rcx,%r14
mov    %rdx,0x10(%rsp)
mov    %rsi,%r15
mov    %rdi,%r12
mov    %r9,%rdi
call   1070 <BN_CTX_start@plt>
mov    %rbp,%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <BN_CTX_get@plt>
mov    %rax,%r13
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %r8d,%r8d
mov    %rbp,%r9
call   1030 <EC_POINT_get_Jprojective_coordinates_GFp@plt>
mov    %rbp,%rdi
call   1060 <BN_CTX_end@plt>
mov    %rbx,%rdi
call   1080 <BN_num_bytes@plt>
mov    %eax,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    0x8(%rsp),%rdx
call   1050 <memset@plt>
mov    $0x21,%eax
sub    %ebp,%eax
movslq %eax,%rsi
add    %r14,%rsi
mov    %rbx,%rdi
call   1090 <BN_bn2bin@plt>
mov    0x2dca(%rip),%rax
mov    0x10(%rsp),%rcx
cmp    %rcx,(%rax)
jne    1246 <struct_EC_POINT_point2oct+0xc6>
mov    %r13,%rdi
call   1080 <BN_num_bytes@plt>
mov    $0x41,%ecx
sub    %eax,%ecx
movslq %ecx,%rsi
add    %r14,%rsi
mov    %r13,%rdi
call   1090 <BN_bn2bin@plt>
mov    $0x41,%eax
mov    $0x4,%cl
jmp    125c <struct_EC_POINT_point2oct+0xdc>
mov    %r13,%rdi
call   10a0 <BN_is_odd@plt>
cmp    $0x1,%rax
mov    $0x3,%cl
sbb    $0x0,%cl
mov    $0x21,%eax
mov    %cl,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@POINT_CONVERSION_UNCOMPRESSED = global i64 0
define i64 @struct_EC_POINT_point2oct(i32* %0, i32* %1, i64 %2, i8* %3, i64 %4, i32* %5) {
%7 = tail call i32 @BN_CTX_start(i32* %5)
%8 = tail call i32* @BN_CTX_get(i32* %5)
%9 = tail call i32* @BN_CTX_get(i32* %5)
%10 = tail call i32 @EC_POINT_get_Jprojective_coordinates_GFp(i32* %0, i32* %1, i32* %8, i32* %9, i32* null, i32* %5)
%11 = tail call i32 @BN_CTX_end(i32* %5)
%12 = tail call i32 @BN_num_bytes(i32* %8)
%13 = tail call i32 @memset(i8* %3, i32 0, i64 %4)
%14 = sub nsw i32 33, %12
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %3, i64 %15
%17 = tail call i32 @BN_bn2bin(i32* %8, i8* %16)
%18 = load i64, i64* @POINT_CONVERSION_UNCOMPRESSED
%19 = icmp eq i64 %18, %2
br i1 %19, label %20, label %26
20: ; preds = %6
%21 = tail call i32 @BN_num_bytes(i32* %9)
%22 = sub nsw i32 65, %21
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %3, i64 %23
%25 = tail call i32 @BN_bn2bin(i32* %9, i8* %24)
br label %30
26: ; preds = %6
%27 = tail call i64 @BN_is_odd(i32* %9)
%28 = icmp eq i64 %27, 0
%29 = select i1 %28, i8 2, i8 3
br label %30
30: ; preds = %26, %20
%31 = phi i8 [ %29, %26 ], [ 4, %20 ]
%32 = phi i64 [ 33, %26 ], [ 65, %20 ]
store i8 %31, i8* %3
ret i64 %32
}