#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*/ X509;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
int EVP_PKEY_cmp(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *);
int /*<<< orphan*/ X509_R_KEY_TYPE_MISMATCH;
int /*<<< orphan*/ X509_R_KEY_VALUES_MISMATCH;
int /*<<< orphan*/ X509_R_UNKNOWN_KEY_TYPE;
int /*<<< orphan*/ *X509_get0_pubkey(int /*<<< orphan*/ const *);
int /*<<< orphan*/ X509error(int /*<<< orphan*/);
int X509_check_private_key(const X509 *x, const EVP_PKEY *k) {
  const EVP_PKEY *xk;
  int ret;
  xk = X509_get0_pubkey(x);
  if (xk)
    ret = EVP_PKEY_cmp(xk, k);
  else
    ret = -2;
  switch (ret) {
  case 1:
    break;
  case 0:
    X509error(X509_R_KEY_VALUES_MISMATCH);
    break;
  case -1:
    X509error(X509_R_KEY_TYPE_MISMATCH);
    break;
  case -2:
    X509error(X509_R_UNKNOWN_KEY_TYPE);
  }
  if (ret > 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<X509_check_private_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <X509_get0_pubkey@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     116d <X509_check_private_key+0x3d>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <EVP_PKEY_cmp@plt>
mov    %eax,-0x24(%rbp)
jmp    1174 <X509_check_private_key+0x44>
movl   $0xfffffffe,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x3,%eax
ja     11d6 <X509_check_private_key+0xa6>
mov    -0x30(%rbp),%rax
lea    0xe6c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    11d6 <X509_check_private_key+0xa6>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1050 <X509error@plt>
jmp    11d6 <X509_check_private_key+0xa6>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1050 <X509error@plt>
jmp    11d6 <X509_check_private_key+0xa6>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1050 <X509error@plt>
cmpl   $0x0,-0x24(%rbp)
jle    11ec <X509_check_private_key+0xbc>
movl   $0x1,-0x4(%rbp)
jmp    11f3 <X509_check_private_key+0xc3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<X509_check_private_key>:
push   %rbx
mov    %rsi,%rbx
call   1030 <X509_get0_pubkey@plt>
test   %rax,%rax
je     1162 <X509_check_private_key+0x32>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <EVP_PKEY_cmp@plt>
mov    %eax,%ebx
add    $0x2,%eax
cmp    $0x2,%eax
ja     1175 <X509_check_private_key+0x45>
cltq
lea    0x2ca4(%rip),%rcx
mov    (%rcx,%rax,8),%rax
jmp    116e <X509_check_private_key+0x3e>
mov    $0xfffffffe,%ebx
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1050 <X509error@plt>
xor    %eax,%eax
test   %ebx,%ebx
setg   %al
pop    %rbx
ret

LLVM IR (-O0):
@X509_R_KEY_VALUES_MISMATCH = global i32 0
@X509_R_KEY_TYPE_MISMATCH = global i32 0
@X509_R_UNKNOWN_KEY_TYPE = global i32 0
define i32 @X509_check_private_key(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%8 = load i32*, i32** %4
%9 = call i32* @X509_get0_pubkey(i32* %8)
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32*, i32** %6
%14 = load i32*, i32** %5
%15 = call i32 @EVP_PKEY_cmp(i32* %13, i32* %14)
store i32 %15, i32* %7
br label %17
16: ; preds = %2
store i32 -2, i32* %7
br label %17
17: ; preds = %16, %12
%18 = load i32, i32* %7
switch i32 %18, label %29 [
i32 1, label %19
i32 0, label %20
i32 -1, label %23
i32 -2, label %26
]
19: ; preds = %17
br label %29
20: ; preds = %17
%21 = load i32, i32* @X509_R_KEY_VALUES_MISMATCH
%22 = call i32 @X509error(i32 %21)
br label %29
23: ; preds = %17
%24 = load i32, i32* @X509_R_KEY_TYPE_MISMATCH
%25 = call i32 @X509error(i32 %24)
br label %29
26: ; preds = %17
%27 = load i32, i32* @X509_R_UNKNOWN_KEY_TYPE
%28 = call i32 @X509error(i32 %27)
br label %29
29: ; preds = %26, %17, %23, %20, %19
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* %3
br label %34
33: ; preds = %29
store i32 0, i32* %3
br label %34
34: ; preds = %33, %32
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@X509_R_KEY_VALUES_MISMATCH = global i32 0
@X509_R_KEY_TYPE_MISMATCH = global i32 0
@X509_R_UNKNOWN_KEY_TYPE = global i32 0
@switch.table.X509_check_private_key = [3 x i32*] [i32* @X509_R_UNKNOWN_KEY_TYPE, i32* @X509_R_KEY_TYPE_MISMATCH, i32* @X509_R_KEY_VALUES_MISMATCH]
define i32 @X509_check_private_key(i32* %0, i32* %1) {
%3 = tail call i32* @X509_get0_pubkey(i32* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = tail call i32 @EVP_PKEY_cmp(i32* %3, i32* %1)
%7 = add i32 %6, 2
%8 = icmp ult i32 %7, 3
br i1 %8, label %9, label %18
9: ; preds = %5
%10 = sext i32 %7 to i64
%11 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.X509_check_private_key, i64 0, i64 %10
%12 = load i32*, i32** %11
br label %13
13: ; preds = %2, %9
%14 = phi i32* [ %12, %9 ], [ @X509_R_UNKNOWN_KEY_TYPE, %2 ]
%15 = phi i32 [ %6, %9 ], [ -2, %2 ]
%16 = load i32, i32* %14
%17 = tail call i32 @X509error(i32 %16)
br label %18
18: ; preds = %5, %13
%19 = phi i32 [ %6, %5 ], [ %15, %13 ]
%20 = icmp sgt i32 %19, 0
%21 = zext i1 %20 to i32
ret i32 %21
}