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

/* Variables and functions */
int /*<<< orphan*/ BN_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_CTX_new();
int /*<<< orphan*/ BN_hex2bn(int /*<<< orphan*/ **, char *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ EC_POINT_is_on_curve(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_POINT_new(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_set_compressed_coordinates_GFp(
    int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
    int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ OPENSSL_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sm2_ec_group();
int Pub_is_on_curve(char *pubx) {
  BN_CTX *ctx = BN_CTX_new();
  EC_GROUP *group = sm2_ec_group();
  EC_POINT *P = EC_POINT_new(group);
  BIGNUM *x = BN_new();
  if (!x)
    return 2;
  if (!BN_hex2bn(&x, pubx))
    return 1;
  if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
    return 1;
  if (!EC_POINT_is_on_curve(group, P, ctx))
    return 1;
  BN_CTX_free(ctx);
  OPENSSL_free(group);
  return 0;
}


x86 assembly (clang, -O0):
<Pub_is_on_curve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <BN_CTX_new@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <sm2_ec_group@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10b0 <EC_POINT_new@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <BN_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e1 <Pub_is_on_curve+0x51>
movl   $0x2,-0x4(%rbp)
jmp    126e <Pub_is_on_curve+0xde>
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1030 <BN_hex2bn@plt>
cmp    $0x0,%eax
jne    1203 <Pub_is_on_curve+0x73>
movl   $0x1,-0x4(%rbp)
jmp    126e <Pub_is_on_curve+0xde>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%r8
xor    %ecx,%ecx
call   1070 <EC_POINT_set_compressed_coordinates_GFp@plt>
cmp    $0x0,%eax
jne    122f <Pub_is_on_curve+0x9f>
movl   $0x1,-0x4(%rbp)
jmp    126e <Pub_is_on_curve+0xde>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1090 <EC_POINT_is_on_curve@plt>
cmp    $0x0,%eax
jne    1255 <Pub_is_on_curve+0xc5>
movl   $0x1,-0x4(%rbp)
jmp    126e <Pub_is_on_curve+0xde>
mov    -0x18(%rbp),%rdi
call   1060 <BN_CTX_free@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <OPENSSL_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Pub_is_on_curve>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
xor    %eax,%eax
call   1080 <BN_CTX_new@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <sm2_ec_group@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <EC_POINT_new@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <BN_new@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     121b <Pub_is_on_curve+0x8b>
mov    %rsp,%rdi
mov    %rbp,%rsi
call   1030 <BN_hex2bn@plt>
mov    $0x1,%ebp
test   %eax,%eax
je     1220 <Pub_is_on_curve+0x90>
mov    (%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %ecx,%ecx
mov    %r14,%r8
call   1070 <EC_POINT_set_compressed_coordinates_GFp@plt>
test   %eax,%eax
je     1220 <Pub_is_on_curve+0x90>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1090 <EC_POINT_is_on_curve@plt>
test   %eax,%eax
je     1220 <Pub_is_on_curve+0x90>
mov    %r14,%rdi
call   1060 <BN_CTX_free@plt>
mov    %rbx,%rdi
call   10a0 <OPENSSL_free@plt>
xor    %ebp,%ebp
jmp    1220 <Pub_is_on_curve+0x90>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @Pub_is_on_curve(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
store i8* %0, i8** %3
%8 = call i32* (...) @BN_CTX_new()
store i32* %8, i32** %4
%9 = call i32* (...) @sm2_ec_group()
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = call i32* @EC_POINT_new(i32* %10)
store i32* %11, i32** %6
%12 = call i32* (...) @BN_new()
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i32 2, i32* %2
br label %41
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @BN_hex2bn(i32** %7, i8* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %16
store i32 1, i32* %2
br label %41
21: ; preds = %16
%22 = load i32*, i32** %5
%23 = load i32*, i32** %6
%24 = load i32*, i32** %7
%25 = load i32*, i32** %4
%26 = call i32 @EC_POINT_set_compressed_coordinates_GFp(i32* %22, i32* %23, i32* %24, i32 0, i32* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %21
store i32 1, i32* %2
br label %41
29: ; preds = %21
%30 = load i32*, i32** %5
%31 = load i32*, i32** %6
%32 = load i32*, i32** %4
%33 = call i32 @EC_POINT_is_on_curve(i32* %30, i32* %31, i32* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %29
store i32 1, i32* %2
br label %41
36: ; preds = %29
%37 = load i32*, i32** %4
%38 = call i32 @BN_CTX_free(i32* %37)
%39 = load i32*, i32** %5
%40 = call i32 @OPENSSL_free(i32* %39)
store i32 0, i32* %2
br label %41
41: ; preds = %36, %35, %28, %20, %15
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
define i32 @Pub_is_on_curve(i8* %0) {
%2 = alloca i32*
%3 = tail call i32* (...) @BN_CTX_new()
%4 = tail call i32* (...) @sm2_ec_group()
%5 = tail call i32* @EC_POINT_new(i32* %4)
%6 = bitcast i32** %2 to i8*
%7 = tail call i32* (...) @BN_new()
store i32* %7, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %22, label %9
9: ; preds = %1
%10 = call i32 @BN_hex2bn(i32** %2, i8* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %22, label %12
12: ; preds = %9
%13 = load i32*, i32** %2
%14 = call i32 @EC_POINT_set_compressed_coordinates_GFp(i32* %4, i32* %5, i32* %13, i32 0, i32* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = call i32 @EC_POINT_is_on_curve(i32* %4, i32* %5, i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32 @BN_CTX_free(i32* %3)
%21 = call i32 @OPENSSL_free(i32* %4)
br label %22
22: ; preds = %16, %12, %9, %1, %19
%23 = phi i32 [ 0, %19 ], [ 2, %1 ], [ 1, %9 ], [ 1, %12 ], [ 1, %16 ]
ret i32 %23
}