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

/* Variables and functions */
int /*<<< orphan*/ EC_KEY_check_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_generate_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_new();
int /*<<< orphan*/ EC_KEY_set_group(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *sm2_ec_group();
int GenECKey(EC_KEY *ec_key, int ctype) {
  if (!ec_key)
    ec_key = EC_KEY_new();
  if (!ec_key) {
    return -1;
  }
  EC_GROUP *ec_group = NULL;
  if (ctype == 0) {
    ec_group = sm2_ec_group();
  } else if (ctype == 1) {
    ec_group = sm2_ec_group();
  } else {
    return -2;
  }
  if (!ec_group) {
    return -3;
  }
  if (!EC_KEY_set_group(ec_key, ec_group)) {
    return -4;
  }
  if (!EC_KEY_generate_key(ec_key)) {
    return -5;
  }
  if (!EC_KEY_check_key(ec_key)) {
    return -6;
  }
  return 0;
}


x86 assembly (clang, -O0):
<GenECKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <GenECKey+0x25>
mov    $0x0,%al
call   1060 <EC_KEY_new@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118c <GenECKey+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <GenECKey+0x105>
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11ae <GenECKey+0x5e>
mov    $0x0,%al
call   1040 <sm2_ec_group@plt>
mov    %rax,-0x20(%rbp)
jmp    11d9 <GenECKey+0x89>
cmpl   $0x1,-0x14(%rbp)
jne    11c8 <GenECKey+0x78>
mov    $0x0,%al
call   1040 <sm2_ec_group@plt>
mov    %rax,-0x20(%rbp)
jmp    11d4 <GenECKey+0x84>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1255 <GenECKey+0x105>
jmp    11d9 <GenECKey+0x89>
cmpq   $0x0,-0x20(%rbp)
jne    11f0 <GenECKey+0xa0>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1255 <GenECKey+0x105>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <EC_KEY_set_group@plt>
cmp    $0x0,%eax
jne    1212 <GenECKey+0xc2>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1255 <GenECKey+0x105>
mov    -0x10(%rbp),%rdi
call   1050 <EC_KEY_generate_key@plt>
cmp    $0x0,%eax
jne    1230 <GenECKey+0xe0>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1255 <GenECKey+0x105>
mov    -0x10(%rbp),%rdi
call   1030 <EC_KEY_check_key@plt>
cmp    $0x0,%eax
jne    124e <GenECKey+0xfe>
movl   $0xfffffffa,-0x4(%rbp)
jmp    1255 <GenECKey+0x105>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GenECKey>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
test   %rdi,%rdi
jne    116c <GenECKey+0x1c>
xor    %eax,%eax
call   1060 <EC_KEY_new@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ba <GenECKey+0x6a>
mov    $0xfffffffe,%eax
cmp    $0x1,%ebp
ja     11cd <GenECKey+0x7d>
xor    %eax,%eax
call   1040 <sm2_ec_group@plt>
test   %rax,%rax
je     11b3 <GenECKey+0x63>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <EC_KEY_set_group@plt>
test   %eax,%eax
je     11c1 <GenECKey+0x71>
mov    %rbx,%rdi
call   1050 <EC_KEY_generate_key@plt>
test   %eax,%eax
je     11c8 <GenECKey+0x78>
mov    %rbx,%rdi
call   1030 <EC_KEY_check_key@plt>
xor    %ecx,%ecx
test   %eax,%eax
mov    $0xfffffffa,%eax
cmovne %ecx,%eax
jmp    11cd <GenECKey+0x7d>
mov    $0xfffffffd,%eax
jmp    11cd <GenECKey+0x7d>
mov    $0xffffffff,%eax
jmp    11cd <GenECKey+0x7d>
mov    $0xfffffffc,%eax
jmp    11cd <GenECKey+0x7d>
mov    $0xfffffffb,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @GenECKey(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32* (...) @EC_KEY_new()
store i32* %10, i32** %4
br label %11
11: ; preds = %9, %2
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11
store i32 -1, i32* %3
br label %48
15: ; preds = %11
store i32* null, i32** %6
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32* (...) @sm2_ec_group()
store i32* %19, i32** %6
br label %27
20: ; preds = %15
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32* (...) @sm2_ec_group()
store i32* %24, i32** %6
br label %26
25: ; preds = %20
store i32 -2, i32* %3
br label %48
26: ; preds = %23
br label %27
27: ; preds = %26, %18
%28 = load i32*, i32** %6
%29 = icmp ne i32* %28, null
br i1 %29, label %31, label %30
30: ; preds = %27
store i32 -3, i32* %3
br label %48
31: ; preds = %27
%32 = load i32*, i32** %4
%33 = load i32*, i32** %6
%34 = call i32 @EC_KEY_set_group(i32* %32, i32* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %31
store i32 -4, i32* %3
br label %48
37: ; preds = %31
%38 = load i32*, i32** %4
%39 = call i32 @EC_KEY_generate_key(i32* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %37
store i32 -5, i32* %3
br label %48
42: ; preds = %37
%43 = load i32*, i32** %4
%44 = call i32 @EC_KEY_check_key(i32* %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %42
store i32 -6, i32* %3
br label %48
47: ; preds = %42
store i32 0, i32* %3
br label %48
48: ; preds = %47, %46, %41, %36, %30, %25, %14
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
define i32 @GenECKey(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32* (...) @EC_KEY_new()
%6 = icmp eq i32* %5, null
br i1 %6, label %23, label %7
7: ; preds = %2, %4
%8 = phi i32* [ %5, %4 ], [ %0, %2 ]
%9 = icmp ult i32 %1, 2
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = tail call i32* (...) @sm2_ec_group()
%12 = icmp eq i32* %11, null
br i1 %12, label %23, label %13
13: ; preds = %10
%14 = tail call i32 @EC_KEY_set_group(i32* %8, i32* %11)
%15 = icmp eq i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %13
%17 = tail call i32 @EC_KEY_generate_key(i32* %8)
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = tail call i32 @EC_KEY_check_key(i32* %8)
%21 = icmp eq i32 %20, 0
%22 = select i1 %21, i32 -6, i32 0
br label %23
23: ; preds = %7, %10, %13, %16, %19, %4
%24 = phi i32 [ -1, %4 ], [ -2, %7 ], [ -3, %10 ], [ -4, %13 ], [ -5, %16 ], [ %22, %19 ]
ret i32 %24
}