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

/* Variables and functions */
int /*<<< orphan*/ ASN1_INTEGER_set(int /*<<< orphan*/, int);
int /*<<< orphan*/ EVP_PKEY_assign_RSA(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_new();
int /*<<< orphan*/ EVP_sha1();
int /*<<< orphan*/ MBSTRING_ASC;
int /*<<< orphan*/ NID_basic_constraints;
int /*<<< orphan*/ NID_key_usage;
int /*<<< orphan*/ NID_subject_key_identifier;
int /*<<< orphan*/ RSA_F4;
int /*<<< orphan*/ *RSA_generate_key(int, int /*<<< orphan*/,
                                     int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_NAME_add_entry_by_txt(int /*<<< orphan*/ *, char *,
                                              int /*<<< orphan*/,
                                              unsigned char *, int, int,
                                              int /*<<< orphan*/);
int /*<<< orphan*/ X509_get_notAfter(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_get_notBefore(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_get_serialNumber(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_get_subject_name(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_gmtime_adj(int /*<<< orphan*/, long);
int /*<<< orphan*/ *X509_new();
int /*<<< orphan*/ X509_set_issuer_name(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_set_pubkey(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_set_version(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ X509_sign(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             int /*<<< orphan*/);
int /*<<< orphan*/ add_ext(int /*<<< orphan*/ *, int /*<<< orphan*/, char *);
int mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int years) {
  X509 *x;
  EVP_PKEY *pk;
  RSA *rsa;
  X509_NAME *name = NULL;
  if (*pkeyp == NULL) {
    if ((pk = EVP_PKEY_new()) == NULL) {
      return (0);
    }
  } else {
    pk = *pkeyp;
  }
  if (*x509p == NULL) {
    if ((x = X509_new()) == NULL) {
      goto err;
    }
  } else {
    x = *x509p;
  }
  rsa = RSA_generate_key(bits, RSA_F4, NULL, NULL);
  if (!EVP_PKEY_assign_RSA(pk, rsa)) {
    goto err;
  }
  X509_set_version(x, 2);
  ASN1_INTEGER_set(X509_get_serialNumber(x), serial);
  X509_gmtime_adj(X509_get_notBefore(x), 0);
  X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * 365 * years);
  X509_set_pubkey(x, pk);
  name = X509_get_subject_name(x);
  X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
                             (unsigned char *)"NVIDIA GameStream Client", -1,
                             -1, 0);
  X509_set_issuer_name(x, name);
  add_ext(x, NID_basic_constraints, "critical,CA:TRUE");
  add_ext(x, NID_key_usage, "critical,keyCertSign,cRLSign");
  add_ext(x, NID_subject_key_identifier, "hash");
  if (!X509_sign(x, pk, EVP_sha1())) {
    goto err;
  }
  *x509p = x;
  *pkeyp = pk;
  return (1);
err:
  return (0);
}


x86 assembly (clang, -O0):
<mkcert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1266 <mkcert+0x56>
mov    $0x0,%al
call   10f0 <EVP_PKEY_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1261 <mkcert+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1431 <mkcert+0x221>
jmp    1271 <mkcert+0x61>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    129e <mkcert+0x8e>
mov    $0x0,%al
call   1130 <X509_new@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1299 <mkcert+0x89>
jmp    142a <mkcert+0x21a>
jmp    12a9 <mkcert+0x99>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1120 <RSA_generate_key@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1050 <EVP_PKEY_assign_RSA@plt>
cmp    $0x0,%eax
jne    12e0 <mkcert+0xd0>
jmp    142a <mkcert+0x21a>
mov    -0x30(%rbp),%rdi
mov    $0x2,%esi
call   1060 <X509_set_version@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <X509_get_serialNumber@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%esi
call   10c0 <ASN1_INTEGER_set@plt>
mov    -0x30(%rbp),%rdi
call   10d0 <X509_get_notBefore@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <X509_gmtime_adj@plt>
mov    -0x30(%rbp),%rdi
call   1070 <X509_get_notAfter@plt>
mov    %eax,%edi
movslq -0x24(%rbp),%rax
imul   $0x1e13380,%rax,%rsi
call   1030 <X509_gmtime_adj@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1100 <X509_set_pubkey@plt>
mov    -0x30(%rbp),%rdi
call   1090 <X509_get_subject_name@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2c8b(%rip),%rax
mov    (%rax),%edx
lea    0xca2(%rip),%rsi
lea    0xc9e(%rip),%rcx
mov    $0xffffffff,%r9d
xor    %eax,%eax
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   10e0 <X509_NAME_add_entry_by_txt@plt>
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10a0 <X509_set_issuer_name@plt>
mov    -0x30(%rbp),%rdi
mov    0x2c3c(%rip),%rax
mov    (%rax),%esi
lea    0xc7f(%rip),%rdx
call   1040 <add_ext@plt>
mov    -0x30(%rbp),%rdi
mov    0x2c43(%rip),%rax
mov    (%rax),%esi
lea    0xc77(%rip),%rdx
call   1040 <add_ext@plt>
mov    -0x30(%rbp),%rdi
mov    0x2bf2(%rip),%rax
mov    (%rax),%esi
lea    0xc7b(%rip),%rdx
call   1040 <add_ext@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1080 <EVP_sha1@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1110 <X509_sign@plt>
cmp    $0x0,%eax
jne    1408 <mkcert+0x1f8>
jmp    142a <mkcert+0x21a>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1431 <mkcert+0x221>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkcert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rdi,0x8(%rsp)
mov    %rsi,0x10(%rsp)
mov    (%rsi),%r12
test   %r12,%r12
jne    124f <mkcert+0x3f>
xor    %r13d,%r13d
xor    %eax,%eax
call   10f0 <EVP_PKEY_new@plt>
mov    %rax,%r12
test   %rax,%rax
je     13b2 <mkcert+0x1a2>
mov    0x8(%rsp),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
jne    1272 <mkcert+0x62>
xor    %r13d,%r13d
xor    %eax,%eax
call   1130 <X509_new@plt>
mov    %rax,%rbx
test   %rax,%rax
je     13b2 <mkcert+0x1a2>
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
xor    %r13d,%r13d
mov    %r15d,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1120 <RSA_generate_key@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <EVP_PKEY_assign_RSA@plt>
test   %eax,%eax
je     13b2 <mkcert+0x1a2>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <X509_set_version@plt>
mov    %rbx,%rdi
call   10b0 <X509_get_serialNumber@plt>
mov    %eax,%edi
mov    %r14d,%esi
call   10c0 <ASN1_INTEGER_set@plt>
mov    %rbx,%rdi
call   10d0 <X509_get_notBefore@plt>
xor    %r13d,%r13d
mov    %eax,%edi
xor    %esi,%esi
call   1030 <X509_gmtime_adj@plt>
mov    %rbx,%rdi
call   1070 <X509_get_notAfter@plt>
movslq %ebp,%rcx
imul   $0x1e13380,%rcx,%rsi
mov    %eax,%edi
call   1030 <X509_gmtime_adj@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1100 <X509_set_pubkey@plt>
mov    %rbx,%rdi
call   1090 <X509_get_subject_name@plt>
mov    %rax,%rbp
mov    0x2cda(%rip),%rax
mov    (%rax),%edx
movl   $0x0,(%rsp)
lea    0xcea(%rip),%rsi
lea    0xce6(%rip),%rcx
mov    %rbp,%rdi
mov    $0xffffffff,%r8d
mov    $0xffffffff,%r9d
call   10e0 <X509_NAME_add_entry_by_txt@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <X509_set_issuer_name@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%esi
lea    0xcd0(%rip),%rdx
mov    %rbx,%rdi
call   1040 <add_ext@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%esi
lea    0xcc9(%rip),%rdx
mov    %rbx,%rdi
call   1040 <add_ext@plt>
mov    0x2c45(%rip),%rax
mov    (%rax),%esi
lea    0xcce(%rip),%rdx
mov    %rbx,%rdi
call   1040 <add_ext@plt>
xor    %eax,%eax
call   1080 <EVP_sha1@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1110 <X509_sign@plt>
test   %eax,%eax
je     13b2 <mkcert+0x1a2>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax)
mov    0x10(%rsp),%rax
mov    %r12,(%rax)
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RSA_F4 = global i32 0
@.str = [3 x i8] c"CN\00"
@MBSTRING_ASC = global i32 0
@.str.1 = [25 x i8] c"NVIDIA GameStream Client\00"
@NID_basic_constraints = global i32 0
@.str.2 = [17 x i8] c"critical,CA:TRUE\00"
@NID_key_usage = global i32 0
@.str.3 = [29 x i8] c"critical,keyCertSign,cRLSign\00"
@NID_subject_key_identifier = global i32 0
@.str.4 = [5 x i8] c"hash\00"
define i32 @mkcert(i32** %0, i32** %1, i32 %2, i32 %3, i32 %4) {
%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*
%15 = alloca i32*
store i32** %0, i32*** %7
store i32** %1, i32*** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32* null, i32** %15
%16 = load i32**, i32*** %8
%17 = load i32*, i32** %16
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %5
%20 = call i32* (...) @EVP_PKEY_new()
store i32* %20, i32** %13
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 0, i32* %6
br label %96
23: ; preds = %19
br label %27
24: ; preds = %5
%25 = load i32**, i32*** %8
%26 = load i32*, i32** %25
store i32* %26, i32** %13
br label %27
27: ; preds = %24, %23
%28 = load i32**, i32*** %7
%29 = load i32*, i32** %28
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = call i32* (...) @X509_new()
store i32* %32, i32** %12
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %31
br label %95
35: ; preds = %31
br label %39
36: ; preds = %27
%37 = load i32**, i32*** %7
%38 = load i32*, i32** %37
store i32* %38, i32** %12
br label %39
39: ; preds = %36, %35
%40 = load i32, i32* %9
%41 = load i32, i32* @RSA_F4
%42 = call i32* @RSA_generate_key(i32 %40, i32 %41, i32* null, i32* null)
store i32* %42, i32** %14
%43 = load i32*, i32** %13
%44 = load i32*, i32** %14
%45 = call i32 @EVP_PKEY_assign_RSA(i32* %43, i32* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %39
br label %95
48: ; preds = %39
%49 = load i32*, i32** %12
%50 = call i32 @X509_set_version(i32* %49, i32 2)
%51 = load i32*, i32** %12
%52 = call i32 @X509_get_serialNumber(i32* %51)
%53 = load i32, i32* %10
%54 = call i32 @ASN1_INTEGER_set(i32 %52, i32 %53)
%55 = load i32*, i32** %12
%56 = call i32 @X509_get_notBefore(i32* %55)
%57 = call i32 @X509_gmtime_adj(i32 %56, i64 0)
%58 = load i32*, i32** %12
%59 = call i32 @X509_get_notAfter(i32* %58)
%60 = load i32, i32* %11
%61 = sext i32 %60 to i64
%62 = mul nsw i64 31536000, %61
%63 = call i32 @X509_gmtime_adj(i32 %59, i64 %62)
%64 = load i32*, i32** %12
%65 = load i32*, i32** %13
%66 = call i32 @X509_set_pubkey(i32* %64, i32* %65)
%67 = load i32*, i32** %12
%68 = call i32* @X509_get_subject_name(i32* %67)
store i32* %68, i32** %15
%69 = load i32*, i32** %15
%70 = load i32, i32* @MBSTRING_ASC
%71 = call i32 @X509_NAME_add_entry_by_txt(i32* %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %70, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%72 = load i32*, i32** %12
%73 = load i32*, i32** %15
%74 = call i32 @X509_set_issuer_name(i32* %72, i32* %73)
%75 = load i32*, i32** %12
%76 = load i32, i32* @NID_basic_constraints
%77 = call i32 @add_ext(i32* %75, i32 %76, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%78 = load i32*, i32** %12
%79 = load i32, i32* @NID_key_usage
%80 = call i32 @add_ext(i32* %78, i32 %79, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%81 = load i32*, i32** %12
%82 = load i32, i32* @NID_subject_key_identifier
%83 = call i32 @add_ext(i32* %81, i32 %82, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%84 = load i32*, i32** %12
%85 = load i32*, i32** %13
%86 = call i32 (...) @EVP_sha1()
%87 = call i32 @X509_sign(i32* %84, i32* %85, i32 %86)
%88 = icmp ne i32 %87, 0
br i1 %88, label %90, label %89
89: ; preds = %48
br label %95
90: ; preds = %48
%91 = load i32*, i32** %12
%92 = load i32**, i32*** %7
store i32* %91, i32** %92
%93 = load i32*, i32** %13
%94 = load i32**, i32*** %8
store i32* %93, i32** %94
store i32 1, i32* %6
br label %96
95: ; preds = %89, %47, %34
store i32 0, i32* %6
br label %96
96: ; preds = %95, %90, %22
%97 = load i32, i32* %6
ret i32 %97
}

LLVM IR (-O2):
@RSA_F4 = global i32 0
@.str = [3 x i8] c"CN\00"
@MBSTRING_ASC = global i32 0
@.str.1 = [25 x i8] c"NVIDIA GameStream Client\00"
@NID_basic_constraints = global i32 0
@.str.2 = [17 x i8] c"critical,CA:TRUE\00"
@NID_key_usage = global i32 0
@.str.3 = [29 x i8] c"critical,keyCertSign,cRLSign\00"
@NID_subject_key_identifier = global i32 0
@.str.4 = [5 x i8] c"hash\00"
define i32 @mkcert(i32** nocapture %0, i32** nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = load i32*, i32** %1
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32* (...) @EVP_PKEY_new()
%10 = icmp eq i32* %9, null
br i1 %10, label %49, label %11
11: ; preds = %5, %8
%12 = phi i32* [ %9, %8 ], [ %6, %5 ]
%13 = load i32*, i32** %0
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32* (...) @X509_new()
%17 = icmp eq i32* %16, null
br i1 %17, label %49, label %18
18: ; preds = %11, %15
%19 = phi i32* [ %16, %15 ], [ %13, %11 ]
%20 = load i32, i32* @RSA_F4
%21 = tail call i32* @RSA_generate_key(i32 %2, i32 %20, i32* null, i32* null)
%22 = tail call i32 @EVP_PKEY_assign_RSA(i32* %12, i32* %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %49, label %24
24: ; preds = %18
%25 = tail call i32 @X509_set_version(i32* %19, i32 2)
%26 = tail call i32 @X509_get_serialNumber(i32* %19)
%27 = tail call i32 @ASN1_INTEGER_set(i32 %26, i32 %3)
%28 = tail call i32 @X509_get_notBefore(i32* %19)
%29 = tail call i32 @X509_gmtime_adj(i32 %28, i64 0)
%30 = tail call i32 @X509_get_notAfter(i32* %19)
%31 = sext i32 %4 to i64
%32 = mul nsw i64 %31, 31536000
%33 = tail call i32 @X509_gmtime_adj(i32 %30, i64 %32)
%34 = tail call i32 @X509_set_pubkey(i32* %19, i32* %12)
%35 = tail call i32* @X509_get_subject_name(i32* %19)
%36 = load i32, i32* @MBSTRING_ASC
%37 = tail call i32 @X509_NAME_add_entry_by_txt(i32* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%38 = tail call i32 @X509_set_issuer_name(i32* %19, i32* %35)
%39 = load i32, i32* @NID_basic_constraints
%40 = tail call i32 @add_ext(i32* %19, i32 %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @NID_key_usage
%42 = tail call i32 @add_ext(i32* %19, i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @NID_subject_key_identifier
%44 = tail call i32 @add_ext(i32* %19, i32 %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%45 = tail call i32 (...) @EVP_sha1()
%46 = tail call i32 @X509_sign(i32* %19, i32* %12, i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %24
store i32* %19, i32** %0
store i32* %12, i32** %1
br label %49
49: ; preds = %15, %18, %24, %8, %48
%50 = phi i32 [ 1, %48 ], [ 0, %8 ], [ 0, %24 ], [ 0, %18 ], [ 0, %15 ]
ret i32 %50
}