#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 */
struct policy {
  int dummy;
};
typedef int /*<<< orphan*/ X509;

/* Variables and functions */
int /*<<< orphan*/ OPENSSL_free(char *);
char *X509_NAME_oneline(int /*<<< orphan*/, int /*<<< orphan*/ *,
                        int /*<<< orphan*/);
int /*<<< orphan*/ X509_get_subject_name(int /*<<< orphan*/ *);
int find_policy(struct policy **, int /*<<< orphan*/ *, int);
scalar_t__ isselfsigned(int /*<<< orphan*/ *);
int restriction_evaluate_policy(int /*<<< orphan*/ *, struct policy *);
int evaluate_cert(X509 *cert, struct policy **namespaces) {
  int result = 2;
  int policyindex = -1, currentindex = -1;
  if (isselfsigned(cert)) {
    char *subject = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
    OPENSSL_free(subject);
    return 0;
  }
  while ((policyindex = find_policy(namespaces, cert, currentindex)) != -1) {
    struct policy *policy = namespaces[policyindex];
    result = restriction_evaluate_policy(cert, policy);
    if (result != 2)
      break;
    currentindex = policyindex;
  }
  return result;
}


x86 assembly (clang, -O0):
<evaluate_cert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <isselfsigned@plt>
cmp    $0x0,%rax
je     11c7 <evaluate_cert+0x67>
mov    -0x10(%rbp),%rdi
call   1050 <X509_get_subject_name@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <X509_NAME_oneline@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <OPENSSL_free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1228 <evaluate_cert+0xc8>
jmp    11cc <evaluate_cert+0x6c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <find_policy@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     1222 <evaluate_cert+0xc2>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <restriction_evaluate_policy@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
je     1217 <evaluate_cert+0xb7>
jmp    1222 <evaluate_cert+0xc2>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11cc <evaluate_cert+0x6c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<evaluate_cert>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <isselfsigned@plt>
test   %rax,%rax
je     1196 <evaluate_cert+0x36>
mov    %r15,%rdi
call   1050 <X509_get_subject_name@plt>
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <X509_NAME_oneline@plt>
mov    %rax,%rdi
call   1080 <OPENSSL_free@plt>
jmp    11d0 <evaluate_cert+0x70>
mov    $0xffffffff,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <find_policy@plt>
cmp    $0xffffffff,%eax
je     11cb <evaluate_cert+0x6b>
mov    %eax,%ebp
cltq
mov    (%r14,%rax,8),%rsi
mov    %r15,%rdi
call   1070 <restriction_evaluate_policy@plt>
cmp    $0x2,%eax
je     11a0 <evaluate_cert+0x40>
mov    %eax,%ebx
jmp    11d0 <evaluate_cert+0x70>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.policy = type { i32 }
define i32 @evaluate_cert(i32* %0, %struct.policy** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.policy**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.policy*
store i32* %0, i32** %4
store %struct.policy** %1, %struct.policy*** %5
store i32 2, i32* %6
store i32 -1, i32* %7
store i32 -1, i32* %8
%11 = load i32*, i32** %4
%12 = call i64 @isselfsigned(i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32*, i32** %4
%16 = call i32 @X509_get_subject_name(i32* %15)
%17 = call i8* @X509_NAME_oneline(i32 %16, i32* null, i32 0)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = call i32 @OPENSSL_free(i8* %18)
store i32 0, i32* %3
br label %43
20: ; preds = %2
br label %21
21: ; preds = %39, %20
%22 = load %struct.policy**, %struct.policy*** %5
%23 = load i32*, i32** %4
%24 = load i32, i32* %8
%25 = call i32 @find_policy(%struct.policy** %22, i32* %23, i32 %24)
store i32 %25, i32* %7
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %41
27: ; preds = %21
%28 = load %struct.policy**, %struct.policy*** %5
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds %struct.policy*, %struct.policy** %28, i64 %30
%32 = load %struct.policy*, %struct.policy** %31
store %struct.policy* %32, %struct.policy** %10
%33 = load i32*, i32** %4
%34 = load %struct.policy*, %struct.policy** %10
%35 = call i32 @restriction_evaluate_policy(i32* %33, %struct.policy* %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 2
br i1 %37, label %38, label %39
38: ; preds = %27
br label %41
39: ; preds = %27
%40 = load i32, i32* %7
store i32 %40, i32* %8
br label %21
41: ; preds = %38, %21
%42 = load i32, i32* %6
store i32 %42, i32* %3
br label %43
43: ; preds = %41, %14
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.policy = type { i32 }
define i32 @evaluate_cert(i32* %0, %struct.policy** %1) {
%3 = tail call i64 @isselfsigned(i32* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = tail call i32 @X509_get_subject_name(i32* %0)
%7 = tail call i8* @X509_NAME_oneline(i32 %6, i32* null, i32 0)
%8 = tail call i32 @OPENSSL_free(i8* %7)
br label %19
9: ; preds = %2, %13
%10 = phi i32 [ %11, %13 ], [ -1, %2 ]
%11 = tail call i32 @find_policy(%struct.policy** %1, i32* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %19, label %13
13: ; preds = %9
%14 = sext i32 %11 to i64
%15 = getelementptr inbounds %struct.policy*, %struct.policy** %1, i64 %14
%16 = load %struct.policy*, %struct.policy** %15
%17 = tail call i32 @restriction_evaluate_policy(i32* %0, %struct.policy* %16)
%18 = icmp eq i32 %17, 2
br i1 %18, label %9, label %19
19: ; preds = %13, %9, %5
%20 = phi i32 [ 0, %5 ], [ 2, %9 ], [ %17, %13 ]
ret i32 %20
}