#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*/ EVP_PKEY;

/* Variables and functions */
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int EVP_PKEY_id(int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_AutoPrivateKey(int /*<<< orphan*/ *,
                                       unsigned char const **, size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int test_d2i_AutoPrivateKey(const unsigned char *input, size_t input_len,
                            int expected_id) {
  int ret = 0;
  const unsigned char *p;
  EVP_PKEY *pkey = NULL;
  p = input;
  pkey = d2i_AutoPrivateKey(NULL, &p, input_len);
  if (pkey == NULL || p != input + input_len) {
    fprintf(stderr, "d2i_AutoPrivateKey failed\n");
    goto done;
  }
  if (EVP_PKEY_id(pkey) != expected_id) {
    fprintf(stderr, "Did not decode expected type\n");
    goto done;
  }
  ret = 1;
done:
  if (!ret) {
    ERR_print_errors_fp(stderr);
  }
  if (pkey != NULL) {
    EVP_PKEY_free(pkey);
  }
  return ret;
}


x86 assembly (clang, -O0):
<test_d2i_AutoPrivateKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1050 <d2i_AutoPrivateKey@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11af <test_d2i_AutoPrivateKey+0x5f>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
add    -0x10(%rbp),%rcx
cmp    %rcx,%rax
je     11c9 <test_d2i_AutoPrivateKey+0x79>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11fc <test_d2i_AutoPrivateKey+0xac>
mov    -0x28(%rbp),%rdi
call   1060 <EVP_PKEY_id@plt>
cmp    -0x14(%rbp),%eax
je     11f5 <test_d2i_AutoPrivateKey+0xa5>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11fc <test_d2i_AutoPrivateKey+0xac>
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1214 <test_d2i_AutoPrivateKey+0xc4>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1070 <ERR_print_errors_fp@plt>
cmpq   $0x0,-0x28(%rbp)
je     1228 <test_d2i_AutoPrivateKey+0xd8>
mov    -0x28(%rbp),%rdi
call   1030 <EVP_PKEY_free@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_d2i_AutoPrivateKey>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rdi,0x8(%rsp)
xor    %r14d,%r14d
lea    0x8(%rsp),%rsi
xor    %edi,%edi
mov    %rbp,%rdx
call   1050 <d2i_AutoPrivateKey@plt>
mov    %rax,%r12
test   %rax,%rax
je     11c0 <test_d2i_AutoPrivateKey+0x70>
add    %rbp,%rbx
cmp    %rbx,0x8(%rsp)
jne    11c0 <test_d2i_AutoPrivateKey+0x70>
mov    %r12,%rdi
call   1060 <EVP_PKEY_id@plt>
mov    $0x1,%ebx
cmp    %r15d,%eax
je     11e6 <test_d2i_AutoPrivateKey+0x96>
mov    0x2e39(%rip),%rbx
mov    (%rbx),%edi
lea    0xe6b(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
call   1070 <ERR_print_errors_fp@plt>
xor    %ebx,%ebx
jmp    11e6 <test_d2i_AutoPrivateKey+0x96>
mov    0x2e19(%rip),%rbx
mov    (%rbx),%edi
lea    0xe30(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
call   1070 <ERR_print_errors_fp@plt>
mov    $0x0,%ebx
test   %r12,%r12
je     11f1 <test_d2i_AutoPrivateKey+0xa1>
mov    %r12,%rdi
call   1030 <EVP_PKEY_free@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"d2i_AutoPrivateKey failed\0A\00"
@.str.1 = [30 x i8] c"Did not decode expected type\0A\00"
define i32 @test_d2i_AutoPrivateKey(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32* null, i32** %9
%10 = load i8*, i8** %4
store i8* %10, i8** %8
%11 = load i64, i64* %5
%12 = call i32* @d2i_AutoPrivateKey(i32* null, i8** %8, i64 %11)
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = icmp eq i32* %13, null
br i1 %14, label %21, label %15
15: ; preds = %3
%16 = load i8*, i8** %8
%17 = load i8*, i8** %4
%18 = load i64, i64* %5
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = icmp ne i8* %16, %19
br i1 %20, label %21, label %24
21: ; preds = %15, %3
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %33
24: ; preds = %15
%25 = load i32*, i32** %9
%26 = call i32 @EVP_PKEY_id(i32* %25)
%27 = load i32, i32* %6
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %33
32: ; preds = %24
store i32 1, i32* %7
br label %33
33: ; preds = %32, %29, %21
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 @ERR_print_errors_fp(i32 %37)
br label %39
39: ; preds = %36, %33
%40 = load i32*, i32** %9
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32*, i32** %9
%44 = call i32 @EVP_PKEY_free(i32* %43)
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %7
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"d2i_AutoPrivateKey failed\0A\00"
@.str.1 = [30 x i8] c"Did not decode expected type\0A\00"
define i32 @test_d2i_AutoPrivateKey(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i8* %0, i8** %4
%6 = call i32* @d2i_AutoPrivateKey(i32* null, i8** %4, i64 %1)
%7 = icmp ne i32* %6, null
%8 = load i8*, i8** %4
%9 = getelementptr inbounds i8, i8* %0, i64 %1
%10 = icmp eq i8* %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = call i32 @EVP_PKEY_id(i32* %6)
%14 = icmp eq i32 %13, %2
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = call i32 @ERR_print_errors_fp(i32 %18)
br label %26
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 @ERR_print_errors_fp(i32 %23)
%25 = icmp eq i32* %6, null
br i1 %25, label %29, label %26
26: ; preds = %12, %15, %20
%27 = phi i32 [ 0, %20 ], [ 0, %15 ], [ 1, %12 ]
%28 = call i32 @EVP_PKEY_free(i32* %6)
br label %29
29: ; preds = %26, %20
%30 = phi i32 [ %27, %26 ], [ 0, %20 ]
ret i32 %30
}