#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 uint8_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_bin2bn(int const *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_GROUP_get0_generator(int /*<<< orphan*/ const *);
int /*<<< orphan*/ EC_POINT_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_POINT_new(int /*<<< orphan*/ const *);
size_t EC_POINT_point2oct(int /*<<< orphan*/ const *, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_set_compressed_coordinates_GFp(
    int /*<<< orphan*/ const *, int /*<<< orphan*/ *, int /*<<< orphan*/ *, int,
    int /*<<< orphan*/ *);
int /*<<< orphan*/ POINT_CONVERSION_COMPRESSED;
EC_POINT *attempt(const EC_GROUP *grp, const uint8_t *digest, size_t dlen,
                  BN_CTX *ctx) {
  EC_POINT *p = NULL;
  BIGNUM *x = NULL;
  size_t glen;
  int y;
  if (grp == NULL || digest == NULL)
    return NULL;
  glen = EC_POINT_point2oct(grp, EC_GROUP_get0_generator(grp),
                            POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  if (glen-- == 0)
    return NULL;
  y = digest[dlen - 1] & 1;
  x = BN_bin2bn(digest, glen < dlen ? glen : dlen, NULL);
  if (x == NULL)
    return NULL;
  p = EC_POINT_new(grp);
  if (p == NULL)
    goto error;
  if (!EC_POINT_set_compressed_coordinates_GFp(grp, p, x, y, ctx)) {
    EC_POINT_free(p);
    p = NULL;
  }
error:
  BN_free(x);
  return p;
}


x86 assembly (clang, -O0):
<attempt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ae <attempt+0x3e>
cmpq   $0x0,-0x18(%rbp)
jne    11bb <attempt+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    12e7 <attempt+0x177>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <EC_GROUP_get0_generator@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%r9
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1060 <EC_POINT_point2oct@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x40(%rbp)
cmp    $0x0,%rax
jne    1215 <attempt+0xa5>
movq   $0x0,-0x8(%rbp)
jmp    12e7 <attempt+0x177>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,4),%eax
and    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    124d <attempt+0xdd>
mov    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1255 <attempt+0xe5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <BN_bin2bn@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1282 <attempt+0x112>
movq   $0x0,-0x8(%rbp)
jmp    12e7 <attempt+0x177>
mov    -0x10(%rbp),%rdi
call   1090 <EC_POINT_new@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    129f <attempt+0x12f>
jmp    12d6 <attempt+0x166>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x44(%rbp),%ecx
mov    -0x28(%rbp),%r8
call   1050 <EC_POINT_set_compressed_coordinates_GFp@plt>
cmp    $0x0,%eax
jne    12d1 <attempt+0x161>
mov    -0x30(%rbp),%rdi
call   1080 <EC_POINT_free@plt>
movq   $0x0,-0x30(%rbp)
jmp    12d6 <attempt+0x166>
mov    -0x38(%rbp),%rdi
call   1040 <BN_free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attempt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdi,%rdi
je     1226 <attempt+0xb6>
mov    %rsi,%r13
test   %rsi,%rsi
je     1226 <attempt+0xb6>
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rdi,%rbx
call   1070 <EC_GROUP_get0_generator@plt>
mov    0x2e40(%rip),%rcx
mov    (%rcx),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
mov    %eax,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r14,%r9
call   1060 <EC_POINT_point2oct@plt>
sub    $0x1,%rax
jb     1226 <attempt+0xb6>
mov    -0x4(%r13,%r12,4),%ebp
cmp    %r12,%rax
cmovb  %rax,%r12
xor    %r15d,%r15d
mov    %r13,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <BN_bin2bn@plt>
test   %rax,%rax
je     1226 <attempt+0xb6>
mov    %rax,%r12
mov    %rbx,%rdi
call   1090 <EC_POINT_new@plt>
test   %rax,%rax
je     121b <attempt+0xab>
mov    %rax,%r15
and    $0x1,%ebp
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
mov    %ebp,%ecx
mov    %r14,%r8
call   1050 <EC_POINT_set_compressed_coordinates_GFp@plt>
test   %eax,%eax
jne    121e <attempt+0xae>
mov    %r15,%rdi
call   1080 <EC_POINT_free@plt>
xor    %r15d,%r15d
mov    %r12,%rdi
call   1040 <BN_free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@POINT_CONVERSION_COMPRESSED = global i32 0
define i32* @attempt(i32* %0, i32* %1, i64 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32* null, i32** %10
store i32* null, i32** %11
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = load i32*, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %16, %4
store i32* null, i32** %5
br label %74
20: ; preds = %16
%21 = load i32*, i32** %6
%22 = load i32*, i32** %6
%23 = call i32 @EC_GROUP_get0_generator(i32* %22)
%24 = load i32, i32* @POINT_CONVERSION_COMPRESSED
%25 = load i32*, i32** %9
%26 = call i64 @EC_POINT_point2oct(i32* %21, i32 %23, i32 %24, i32* null, i32 0, i32* %25)
store i64 %26, i64* %12
%27 = load i64, i64* %12
%28 = add i64 %27, -1
store i64 %28, i64* %12
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %31
30: ; preds = %20
store i32* null, i32** %5
br label %74
31: ; preds = %20
%32 = load i32*, i32** %7
%33 = load i64, i64* %8
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32, i32* %35
%37 = and i32 %36, 1
store i32 %37, i32* %13
%38 = load i32*, i32** %7
%39 = load i64, i64* %12
%40 = load i64, i64* %8
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %31
%43 = load i64, i64* %12
br label %46
44: ; preds = %31
%45 = load i64, i64* %8
br label %46
46: ; preds = %44, %42
%47 = phi i64 [ %43, %42 ], [ %45, %44 ]
%48 = call i32* @BN_bin2bn(i32* %38, i64 %47, i32* null)
store i32* %48, i32** %11
%49 = load i32*, i32** %11
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %52
51: ; preds = %46
store i32* null, i32** %5
br label %74
52: ; preds = %46
%53 = load i32*, i32** %6
%54 = call i32* @EC_POINT_new(i32* %53)
store i32* %54, i32** %10
%55 = load i32*, i32** %10
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %58
57: ; preds = %52
br label %70
58: ; preds = %52
%59 = load i32*, i32** %6
%60 = load i32*, i32** %10
%61 = load i32*, i32** %11
%62 = load i32, i32* %13
%63 = load i32*, i32** %9
%64 = call i32 @EC_POINT_set_compressed_coordinates_GFp(i32* %59, i32* %60, i32* %61, i32 %62, i32* %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %58
%67 = load i32*, i32** %10
%68 = call i32 @EC_POINT_free(i32* %67)
store i32* null, i32** %10
br label %69
69: ; preds = %66, %58
br label %70
70: ; preds = %69, %57
%71 = load i32*, i32** %11
%72 = call i32 @BN_free(i32* %71)
%73 = load i32*, i32** %10
store i32* %73, i32** %5
br label %74
74: ; preds = %70, %51, %30, %19
%75 = load i32*, i32** %5
ret i32* %75
}

LLVM IR (-O2):
@POINT_CONVERSION_COMPRESSED = global i32 0
define i32* @attempt(i32* %0, i32* %1, i64 %2, i32* %3) {
%5 = icmp eq i32* %0, null
%6 = icmp eq i32* %1, null
%7 = or i1 %5, %6
br i1 %7, label %34, label %8
8: ; preds = %4
%9 = tail call i32 @EC_GROUP_get0_generator(i32* %0)
%10 = load i32, i32* @POINT_CONVERSION_COMPRESSED
%11 = tail call i64 @EC_POINT_point2oct(i32* %0, i32 %9, i32 %10, i32* null, i32 0, i32* %3)
%12 = add i64 %11, -1
%13 = icmp eq i64 %11, 0
br i1 %13, label %34, label %14
14: ; preds = %8
%15 = add i64 %2, -1
%16 = getelementptr inbounds i32, i32* %1, i64 %15
%17 = load i32, i32* %16
%18 = and i32 %17, 1
%19 = icmp ult i64 %12, %2
%20 = select i1 %19, i64 %12, i64 %2
%21 = tail call i32* @BN_bin2bn(i32* %1, i64 %20, i32* null)
%22 = icmp eq i32* %21, null
br i1 %22, label %34, label %23
23: ; preds = %14
%24 = tail call i32* @EC_POINT_new(i32* %0)
%25 = icmp eq i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = tail call i32 @EC_POINT_set_compressed_coordinates_GFp(i32* %0, i32* %24, i32* %21, i32 %18, i32* %3)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @EC_POINT_free(i32* %24)
br label %31
31: ; preds = %26, %29, %23
%32 = phi i32* [ null, %23 ], [ %24, %26 ], [ null, %29 ]
%33 = tail call i32 @BN_free(i32* %21)
br label %34
34: ; preds = %14, %8, %4, %31
%35 = phi i32* [ %32, %31 ], [ null, %4 ], [ null, %8 ], [ null, %14 ]
ret i32* %35
}