#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*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ X509_STORE_CTX;
typedef int /*<<< orphan*/ X509_STORE;
typedef int /*<<< orphan*/ X509;

/* Variables and functions */
scalar_t__ EVP_add_digest(int /*<<< orphan*/);
int /*<<< orphan*/ EVP_md5();
int /*<<< orphan*/ EVP_sha1();
int /*<<< orphan*/ EVP_sha256();
int /*<<< orphan*/ X509_STORE_CTX_cleanup(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_STORE_CTX_free(int /*<<< orphan*/ *);
scalar_t__ X509_STORE_CTX_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_STORE_CTX_new();
scalar_t__ X509_STORE_add_cert(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_STORE_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_STORE_new();
int /*<<< orphan*/ X509_STORE_set_flags(int /*<<< orphan*/ *, int);
int X509_V_FLAG_NO_CHECK_TIME;
int X509_V_FLAG_PARTIAL_CHAIN;
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int X509_verify_cert(int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_X509(int /*<<< orphan*/ *, int /*<<< orphan*/ const **,
                             int /*<<< orphan*/ const);
int is_immediate_ca(const uint8_t *cert, const uint32_t cert_size,
                    const uint8_t *ca_cert, const uint32_t ca_cert_size) {
  X509 *X509cert = NULL;
  X509 *X509ca = NULL;
  X509_STORE *cert_store = NULL;
  X509_STORE_CTX *cert_ctx = NULL;
  int ret = 0;
  if (cert == NULL || ca_cert == NULL)
    return 0;
  if (EVP_add_digest(EVP_md5()) == 0)
    return 0;
  if (EVP_add_digest(EVP_sha1()) == 0)
    return 0;
  if (EVP_add_digest(EVP_sha256()) == 0)
    return 0;
  X509cert = d2i_X509(NULL, &cert, cert_size);
  if (X509cert == NULL)
    return 0;
  X509ca = d2i_X509(NULL, &ca_cert, ca_cert_size);
  if (X509ca == NULL)
    goto err;
  cert_store = X509_STORE_new();
  if (cert_store == NULL)
    goto err;
  if (X509_STORE_add_cert(cert_store, X509ca) == 0)
    goto err;
  X509_STORE_set_flags(cert_store,
                       X509_V_FLAG_PARTIAL_CHAIN | X509_V_FLAG_NO_CHECK_TIME);
  cert_ctx = X509_STORE_CTX_new();
  if (cert_ctx == NULL)
    goto err;
  if (X509_STORE_CTX_init(cert_ctx, cert_store, X509cert, NULL) == 0)
    goto err;
  ret = X509_verify_cert(cert_ctx);
  if (ret < 0)
    ret = 0;
  X509_STORE_CTX_cleanup(cert_ctx);
err:
  if (X509cert)
    X509_free(X509cert);
  if (X509ca)
    X509_free(X509ca);
  if (cert_store)
    X509_STORE_free(cert_store);
  if (cert_store)
    X509_STORE_CTX_free(cert_ctx);
  return ret;
}


x86 assembly (clang, -O0):
<is_immediate_ca>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1243 <is_immediate_ca+0x53>
cmpq   $0x0,-0x20(%rbp)
jne    124f <is_immediate_ca+0x5f>
movl   $0x0,-0x4(%rbp)
jmp    1417 <is_immediate_ca+0x227>
mov    $0x0,%al
call   1040 <EVP_md5@plt>
mov    %eax,%edi
call   1090 <EVP_add_digest@plt>
cmp    $0x0,%rax
jne    1273 <is_immediate_ca+0x83>
movl   $0x0,-0x4(%rbp)
jmp    1417 <is_immediate_ca+0x227>
mov    $0x0,%al
call   1060 <EVP_sha1@plt>
mov    %eax,%edi
call   1090 <EVP_add_digest@plt>
cmp    $0x0,%rax
jne    1297 <is_immediate_ca+0xa7>
movl   $0x0,-0x4(%rbp)
jmp    1417 <is_immediate_ca+0x227>
mov    $0x0,%al
call   1110 <EVP_sha256@plt>
mov    %eax,%edi
call   1090 <EVP_add_digest@plt>
cmp    $0x0,%rax
jne    12bb <is_immediate_ca+0xcb>
movl   $0x0,-0x4(%rbp)
jmp    1417 <is_immediate_ca+0x227>
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x10(%rbp),%rsi
call   1100 <d2i_X509@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12e6 <is_immediate_ca+0xf6>
movl   $0x0,-0x4(%rbp)
jmp    1417 <is_immediate_ca+0x227>
mov    -0x24(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1100 <d2i_X509@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    130a <is_immediate_ca+0x11a>
jmp    13c1 <is_immediate_ca+0x1d1>
mov    $0x0,%al
call   10e0 <X509_STORE_new@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1325 <is_immediate_ca+0x135>
jmp    13c1 <is_immediate_ca+0x1d1>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10c0 <X509_STORE_add_cert@plt>
cmp    $0x0,%rax
jne    1341 <is_immediate_ca+0x151>
jmp    13c1 <is_immediate_ca+0x1d1>
mov    -0x40(%rbp),%rdi
mov    0x2c84(%rip),%rax
mov    (%rax),%esi
mov    0x2c8b(%rip),%rax
or     (%rax),%esi
call   10d0 <X509_STORE_set_flags@plt>
mov    $0x0,%al
call   1070 <X509_STORE_CTX_new@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1377 <is_immediate_ca+0x187>
jmp    13c1 <is_immediate_ca+0x1d1>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <X509_STORE_CTX_init@plt>
cmp    $0x0,%rax
jne    139b <is_immediate_ca+0x1ab>
jmp    13c1 <is_immediate_ca+0x1d1>
mov    -0x48(%rbp),%rdi
call   10a0 <X509_verify_cert@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    13b8 <is_immediate_ca+0x1c8>
movl   $0x0,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <X509_STORE_CTX_cleanup@plt>
cmpq   $0x0,-0x30(%rbp)
je     13d5 <is_immediate_ca+0x1e5>
mov    -0x30(%rbp),%rdi
call   10f0 <X509_free@plt>
cmpq   $0x0,-0x38(%rbp)
je     13e9 <is_immediate_ca+0x1f9>
mov    -0x38(%rbp),%rdi
call   10f0 <X509_free@plt>
cmpq   $0x0,-0x40(%rbp)
je     13fd <is_immediate_ca+0x20d>
mov    -0x40(%rbp),%rdi
call   10b0 <X509_STORE_free@plt>
cmpq   $0x0,-0x40(%rbp)
je     1411 <is_immediate_ca+0x221>
mov    -0x48(%rbp),%rdi
call   1080 <X509_STORE_CTX_free@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_immediate_ca>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,0x8(%rsp)
mov    %rdx,(%rsp)
xor    %ebx,%ebx
test   %rdi,%rdi
je     134f <is_immediate_ca+0x15f>
test   %rdx,%rdx
je     134f <is_immediate_ca+0x15f>
mov    %ecx,%r15d
mov    %esi,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <EVP_md5@plt>
mov    %eax,%edi
call   1090 <EVP_add_digest@plt>
test   %rax,%rax
je     134f <is_immediate_ca+0x15f>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <EVP_sha1@plt>
mov    %eax,%edi
call   1090 <EVP_add_digest@plt>
test   %rax,%rax
je     134f <is_immediate_ca+0x15f>
xor    %ebx,%ebx
xor    %eax,%eax
call   1110 <EVP_sha256@plt>
mov    %eax,%edi
call   1090 <EVP_add_digest@plt>
test   %rax,%rax
je     134f <is_immediate_ca+0x15f>
xor    %ebx,%ebx
lea    0x8(%rsp),%rsi
xor    %edi,%edi
mov    %ebp,%edx
call   1100 <d2i_X509@plt>
test   %rax,%rax
je     134f <is_immediate_ca+0x15f>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %rsp,%rsi
xor    %edi,%edi
mov    %r15d,%edx
call   1100 <d2i_X509@plt>
test   %rax,%rax
je     1316 <is_immediate_ca+0x126>
mov    %rax,%r15
xor    %eax,%eax
call   10e0 <X509_STORE_new@plt>
mov    %rax,%r12
test   %rax,%rax
je     1322 <is_immediate_ca+0x132>
mov    %r12,%rdi
mov    %r15,%rsi
call   10c0 <X509_STORE_add_cert@plt>
test   %rax,%rax
je     1326 <is_immediate_ca+0x136>
mov    0x2d0e(%rip),%rax
mov    0x2d17(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r12,%rdi
call   10d0 <X509_STORE_set_flags@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <X509_STORE_CTX_new@plt>
test   %rax,%rax
je     1322 <is_immediate_ca+0x132>
mov    %rax,%rbp
xor    %ebx,%ebx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1030 <X509_STORE_CTX_init@plt>
test   %rax,%rax
je     132a <is_immediate_ca+0x13a>
mov    %rbp,%rdi
call   10a0 <X509_verify_cert@plt>
xor    %ebx,%ebx
test   %eax,%eax
cmovns %eax,%ebx
mov    %rbp,%rdi
call   1050 <X509_STORE_CTX_cleanup@plt>
jmp    132a <is_immediate_ca+0x13a>
mov    %r14,%rdi
call   10f0 <X509_free@plt>
xor    %ebx,%ebx
jmp    134f <is_immediate_ca+0x15f>
xor    %ebp,%ebp
jmp    132a <is_immediate_ca+0x13a>
xor    %ebp,%ebp
xor    %ebx,%ebx
mov    %r14,%rdi
call   10f0 <X509_free@plt>
mov    %r15,%rdi
call   10f0 <X509_free@plt>
test   %r12,%r12
je     134f <is_immediate_ca+0x15f>
mov    %r12,%rdi
call   10b0 <X509_STORE_free@plt>
mov    %rbp,%rdi
call   1080 <X509_STORE_CTX_free@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@X509_V_FLAG_PARTIAL_CHAIN = global i32 0
@X509_V_FLAG_NO_CHECK_TIME = global i32 0
define i32 @is_immediate_ca(i32* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32* null, i32** %10
store i32* null, i32** %11
store i32* null, i32** %12
store i32* null, i32** %13
store i32 0, i32* %14
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %4
%18 = load i32*, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17, %4
store i32 0, i32* %5
br label %111
21: ; preds = %17
%22 = call i32 (...) @EVP_md5()
%23 = call i64 @EVP_add_digest(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %5
br label %111
26: ; preds = %21
%27 = call i32 (...) @EVP_sha1()
%28 = call i64 @EVP_add_digest(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 0, i32* %5
br label %111
31: ; preds = %26
%32 = call i32 (...) @EVP_sha256()
%33 = call i64 @EVP_add_digest(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 0, i32* %5
br label %111
36: ; preds = %31
%37 = load i32, i32* %7
%38 = call i32* @d2i_X509(i32* null, i32** %6, i32 %37)
store i32* %38, i32** %10
%39 = load i32*, i32** %10
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 0, i32* %5
br label %111
42: ; preds = %36
%43 = load i32, i32* %9
%44 = call i32* @d2i_X509(i32* null, i32** %8, i32 %43)
store i32* %44, i32** %11
%45 = load i32*, i32** %11
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %48
47: ; preds = %42
br label %85
48: ; preds = %42
%49 = call i32* (...) @X509_STORE_new()
store i32* %49, i32** %12
%50 = load i32*, i32** %12
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %53
52: ; preds = %48
br label %85
53: ; preds = %48
%54 = load i32*, i32** %12
%55 = load i32*, i32** %11
%56 = call i64 @X509_STORE_add_cert(i32* %54, i32* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %53
br label %85
59: ; preds = %53
%60 = load i32*, i32** %12
%61 = load i32, i32* @X509_V_FLAG_PARTIAL_CHAIN
%62 = load i32, i32* @X509_V_FLAG_NO_CHECK_TIME
%63 = or i32 %61, %62
%64 = call i32 @X509_STORE_set_flags(i32* %60, i32 %63)
%65 = call i32* (...) @X509_STORE_CTX_new()
store i32* %65, i32** %13
%66 = load i32*, i32** %13
%67 = icmp eq i32* %66, null
br i1 %67, label %68, label %69
68: ; preds = %59
br label %85
69: ; preds = %59
%70 = load i32*, i32** %13
%71 = load i32*, i32** %12
%72 = load i32*, i32** %10
%73 = call i64 @X509_STORE_CTX_init(i32* %70, i32* %71, i32* %72, i32* null)
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %69
br label %85
76: ; preds = %69
%77 = load i32*, i32** %13
%78 = call i32 @X509_verify_cert(i32* %77)
store i32 %78, i32* %14
%79 = load i32, i32* %14
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %76
store i32 0, i32* %14
br label %82
82: ; preds = %81, %76
%83 = load i32*, i32** %13
%84 = call i32 @X509_STORE_CTX_cleanup(i32* %83)
br label %85
85: ; preds = %82, %75, %68, %58, %52, %47
%86 = load i32*, i32** %10
%87 = icmp ne i32* %86, null
br i1 %87, label %88, label %91
88: ; preds = %85
%89 = load i32*, i32** %10
%90 = call i32 @X509_free(i32* %89)
br label %91
91: ; preds = %88, %85
%92 = load i32*, i32** %11
%93 = icmp ne i32* %92, null
br i1 %93, label %94, label %97
94: ; preds = %91
%95 = load i32*, i32** %11
%96 = call i32 @X509_free(i32* %95)
br label %97
97: ; preds = %94, %91
%98 = load i32*, i32** %12
%99 = icmp ne i32* %98, null
br i1 %99, label %100, label %103
100: ; preds = %97
%101 = load i32*, i32** %12
%102 = call i32 @X509_STORE_free(i32* %101)
br label %103
103: ; preds = %100, %97
%104 = load i32*, i32** %12
%105 = icmp ne i32* %104, null
br i1 %105, label %106, label %109
106: ; preds = %103
%107 = load i32*, i32** %13
%108 = call i32 @X509_STORE_CTX_free(i32* %107)
br label %109
109: ; preds = %106, %103
%110 = load i32, i32* %14
store i32 %110, i32* %5
br label %111
111: ; preds = %109, %41, %35, %30, %25, %20
%112 = load i32, i32* %5
ret i32 %112
}

LLVM IR (-O2):
@X509_V_FLAG_PARTIAL_CHAIN = global i32 0
@X509_V_FLAG_NO_CHECK_TIME = global i32 0
define i32 @is_immediate_ca(i32* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32*
store i32* %0, i32** %5
store i32* %2, i32** %6
%7 = icmp eq i32* %0, null
%8 = icmp eq i32* %2, null
%9 = or i1 %7, %8
br i1 %9, label %59, label %10
10: ; preds = %4
%11 = tail call i32 (...) @EVP_md5()
%12 = tail call i64 @EVP_add_digest(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %59, label %14
14: ; preds = %10
%15 = tail call i32 (...) @EVP_sha1()
%16 = tail call i64 @EVP_add_digest(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %59, label %18
18: ; preds = %14
%19 = tail call i32 (...) @EVP_sha256()
%20 = tail call i64 @EVP_add_digest(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %59, label %22
22: ; preds = %18
%23 = call i32* @d2i_X509(i32* null, i32** %5, i32 %1)
%24 = icmp eq i32* %23, null
br i1 %24, label %59, label %25
25: ; preds = %22
%26 = call i32* @d2i_X509(i32* null, i32** %6, i32 %3)
%27 = icmp eq i32* %26, null
br i1 %27, label %49, label %28
28: ; preds = %25
%29 = call i32* (...) @X509_STORE_new()
%30 = icmp eq i32* %29, null
br i1 %30, label %51, label %31
31: ; preds = %28
%32 = call i64 @X509_STORE_add_cert(i32* %29, i32* %26)
%33 = icmp eq i64 %32, 0
br i1 %33, label %51, label %34
34: ; preds = %31
%35 = load i32, i32* @X509_V_FLAG_PARTIAL_CHAIN
%36 = load i32, i32* @X509_V_FLAG_NO_CHECK_TIME
%37 = or i32 %36, %35
%38 = call i32 @X509_STORE_set_flags(i32* %29, i32 %37)
%39 = call i32* (...) @X509_STORE_CTX_new()
%40 = icmp eq i32* %39, null
br i1 %40, label %51, label %41
41: ; preds = %34
%42 = call i64 @X509_STORE_CTX_init(i32* %39, i32* %29, i32* %23, i32* null)
%43 = icmp eq i64 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %41
%45 = call i32 @X509_verify_cert(i32* %39)
%46 = icmp sgt i32 %45, 0
%47 = select i1 %46, i32 %45, i32 0
%48 = call i32 @X509_STORE_CTX_cleanup(i32* %39)
br label %51
49: ; preds = %25
%50 = call i32 @X509_free(i32* %23)
br label %59
51: ; preds = %41, %34, %31, %28, %44
%52 = phi i32* [ %39, %44 ], [ %39, %41 ], [ null, %34 ], [ null, %31 ], [ null, %28 ]
%53 = phi i32 [ %47, %44 ], [ 0, %41 ], [ 0, %34 ], [ 0, %31 ], [ 0, %28 ]
%54 = call i32 @X509_free(i32* %23)
%55 = call i32 @X509_free(i32* %26)
br i1 %30, label %59, label %56
56: ; preds = %51
%57 = call i32 @X509_STORE_free(i32* %29)
%58 = call i32 @X509_STORE_CTX_free(i32* %52)
br label %59
59: ; preds = %49, %56, %51, %22, %18, %14, %10, %4
%60 = phi i32 [ 0, %4 ], [ 0, %10 ], [ 0, %14 ], [ 0, %18 ], [ 0, %22 ], [ %53, %51 ], [ %53, %56 ], [ 0, %49 ]
ret i32 %60
}