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

/* Variables and functions */
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BN_new();
int /*<<< orphan*/ BN_rand(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ BN_to_ASN1_INTEGER(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ MBSTRING_ASC;
int /*<<< orphan*/ NID_ext_key_usage;
int /*<<< orphan*/ NID_key_usage;
int /*<<< orphan*/ NID_subject_alt_name;
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*/ const *);
int /*<<< orphan*/ add_coulton(int /*<<< orphan*/ *);
int /*<<< orphan*/ add_ext(int /*<<< orphan*/ *, int /*<<< orphan*/, char *);
int commonname_type(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
X509 *makeselfcert(EVP_PKEY *pkey, int days, char *commonname,
                   const EVP_MD *hash) {
  X509 *x;
  X509_NAME *name;
  char *cnField;
  char *eku;
  BIGNUM *bn;
  int type;
  char subjaltname[512];
  type = commonname_type(commonname);
  switch (type) {
  case 1:
    cnField = "CN";
    eku = "serverAuth";
    strcpy(subjaltname, "DNS:");
    break;
  case 2:
    cnField = "emailAddress";
    eku = "emailProtection";
    strcpy(subjaltname, "email:");
    break;
  default:
    return NULL;
  }
  x = X509_new();
  X509_set_version(x, 2);
  bn = BN_new();
  BN_rand(bn, 255, 1, 0);
  BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(x));
  BN_free(bn);
  X509_gmtime_adj(X509_get_notBefore(x), (long)60 * 60 * 24 * (-7));
  X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days);
  X509_set_pubkey(x, pkey);
  name = X509_get_subject_name(x);
  X509_NAME_add_entry_by_txt(name, cnField, MBSTRING_ASC,
                             (unsigned char *)commonname, -1, -1, 0);
  X509_set_issuer_name(x, name);
  add_ext(x, NID_key_usage,
          "critical,digitalSignature,keyEncipherment,keyAgreement");
  add_ext(x, NID_ext_key_usage, eku);
  if (strlen(commonname) < 500) {
    strcat(subjaltname, commonname);
    add_ext(x, NID_subject_alt_name, subjaltname);
  }
  add_coulton(x);
  X509_sign(x, pkey, hash);
  return x;
}


x86 assembly (clang, -O0):
<makeselfcert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <commonname_type@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x264(%rbp)
sub    $0x1,%eax
je     12a1 <makeselfcert+0x51>
jmp    128d <makeselfcert+0x3d>
mov    -0x264(%rbp),%eax
sub    $0x2,%eax
je     12cf <makeselfcert+0x7f>
jmp    12fd <makeselfcert+0xad>
lea    0xd58(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xd50(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    -0x260(%rbp),%rdi
lea    0xd49(%rip),%rsi
call   1060 <strcpy@plt>
jmp    130a <makeselfcert+0xba>
lea    0xd3d(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xd3f(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    -0x260(%rbp),%rdi
lea    0xd3d(%rip),%rsi
call   1060 <strcpy@plt>
jmp    130a <makeselfcert+0xba>
movq   $0x0,-0x8(%rbp)
jmp    1481 <makeselfcert+0x231>
mov    $0x0,%al
call   1170 <X509_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x2,%esi
call   10a0 <X509_set_version@plt>
mov    $0x0,%al
call   1090 <BN_new@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    $0xff,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <BN_rand@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x270(%rbp)
mov    -0x30(%rbp),%rdi
call   10f0 <X509_get_serialNumber@plt>
mov    -0x270(%rbp),%rdi
mov    %eax,%esi
call   1130 <BN_to_ASN1_INTEGER@plt>
mov    -0x50(%rbp),%rdi
call   1100 <BN_free@plt>
mov    -0x30(%rbp),%rdi
call   1110 <X509_get_notBefore@plt>
mov    %eax,%edi
mov    $0xfffffffffff6c580,%rsi
call   1030 <X509_gmtime_adj@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <X509_get_notAfter@plt>
mov    %eax,%edi
movslq -0x14(%rbp),%rax
imul   $0x15180,%rax,%rsi
call   1030 <X509_gmtime_adj@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1140 <X509_set_pubkey@plt>
mov    -0x30(%rbp),%rdi
call   10d0 <X509_get_subject_name@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    0x2c0f(%rip),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0xffffffff,%r9d
xor    %eax,%eax
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   1120 <X509_NAME_add_entry_by_txt@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10e0 <X509_set_issuer_name@plt>
mov    -0x30(%rbp),%rdi
mov    0x2bea(%rip),%rax
mov    (%rax),%esi
lea    0xc30(%rip),%rdx
call   1050 <add_ext@plt>
mov    -0x30(%rbp),%rdi
mov    0x2ba9(%rip),%rax
mov    (%rax),%esi
mov    -0x48(%rbp),%rdx
call   1050 <add_ext@plt>
mov    -0x20(%rbp),%rdi
call   10c0 <strlen@plt>
cmp    $0x1f4,%eax
jge    145f <makeselfcert+0x20f>
lea    -0x260(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1160 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    0x2ba7(%rip),%rax
mov    (%rax),%esi
lea    -0x260(%rbp),%rdx
call   1050 <add_ext@plt>
mov    -0x30(%rbp),%rdi
call   1070 <add_coulton@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1150 <X509_sign@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeselfcert>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rdx,%rdi
call   1080 <commonname_type@plt>
cmp    $0x1,%eax
je     12a9 <makeselfcert+0x59>
cmp    $0x2,%eax
jne    141d <makeselfcert+0x1cd>
mov    %rbx,0x18(%rsp)
lea    0xd84(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xd85(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xd89(%rip),%rsi
jmp    12cd <makeselfcert+0x7d>
mov    %rbx,0x18(%rsp)
lea    0xd4b(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xd42(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xd41(%rip),%rsi
lea    0x20(%rsp),%rdi
call   1060 <strcpy@plt>
xor    %eax,%eax
call   1170 <X509_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2,%esi
call   10a0 <X509_set_version@plt>
xor    %eax,%eax
call   1090 <BN_new@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0xff,%esi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <BN_rand@plt>
mov    %rbx,%rdi
call   10f0 <X509_get_serialNumber@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1130 <BN_to_ASN1_INTEGER@plt>
mov    %r13,%rdi
call   1100 <BN_free@plt>
mov    %rbx,%rdi
call   1110 <X509_get_notBefore@plt>
mov    %eax,%edi
mov    $0xfffffffffff6c580,%rsi
call   1030 <X509_gmtime_adj@plt>
mov    %rbx,%rdi
call   10b0 <X509_get_notAfter@plt>
movslq %r14d,%rcx
imul   $0x15180,%rcx,%rsi
mov    %eax,%edi
call   1030 <X509_gmtime_adj@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1140 <X509_set_pubkey@plt>
mov    %rbx,%rdi
call   10d0 <X509_get_subject_name@plt>
mov    %rax,%rbp
mov    0x2c66(%rip),%rax
mov    (%rax),%edx
movl   $0x0,(%rsp)
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
mov    %r12,%rcx
mov    $0xffffffff,%r8d
mov    $0xffffffff,%r9d
call   1120 <X509_NAME_add_entry_by_txt@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10e0 <X509_set_issuer_name@plt>
mov    0x2c3f(%rip),%rax
mov    (%rax),%esi
lea    0xc85(%rip),%rdx
mov    %rbx,%rdi
call   1050 <add_ext@plt>
mov    0x2bff(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    0x10(%rsp),%rdx
call   1050 <add_ext@plt>
mov    %r12,%rdi
call   10c0 <strlen@plt>
cmp    $0x1f3,%eax
jg     1403 <makeselfcert+0x1b3>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1160 <strcat@plt>
mov    0x2c02(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1050 <add_ext@plt>
mov    %rbx,%rdi
call   1070 <add_coulton@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    0x18(%rsp),%rdx
call   1150 <X509_sign@plt>
jmp    141f <makeselfcert+0x1cf>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"CN\00"
@.str.1 = [11 x i8] c"serverAuth\00"
@.str.2 = [5 x i8] c"DNS:\00"
@.str.3 = [13 x i8] c"emailAddress\00"
@.str.4 = [16 x i8] c"emailProtection\00"
@.str.5 = [7 x i8] c"email:\00"
@MBSTRING_ASC = global i32 0
@NID_key_usage = global i32 0
@.str.6 = [55 x i8] c"critical,digitalSignature,keyEncipherment,keyAgreement\00"
@NID_ext_key_usage = global i32 0
@NID_subject_alt_name = global i32 0
define i32* @makeselfcert(i32* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca [512 x i8]
store i32* %0, i32** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32* %3, i32** %9
%17 = load i8*, i8** %8
%18 = call i32 @commonname_type(i8* %17)
store i32 %18, i32* %15
%19 = load i32, i32* %15
switch i32 %19, label %26 [
i32 1, label %20
i32 2, label %23
]
20: ; preds = %4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %12
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %13
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %27
23: ; preds = %4
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %12
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8** %13
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %27
26: ; preds = %4
store i32* null, i32** %5
br label %88
27: ; preds = %23, %20
%28 = call i32* (...) @X509_new()
store i32* %28, i32** %10
%29 = load i32*, i32** %10
%30 = call i32 @X509_set_version(i32* %29, i32 2)
%31 = call i32* (...) @BN_new()
store i32* %31, i32** %14
%32 = load i32*, i32** %14
%33 = call i32 @BN_rand(i32* %32, i32 255, i32 1, i32 0)
%34 = load i32*, i32** %14
%35 = load i32*, i32** %10
%36 = call i32 @X509_get_serialNumber(i32* %35)
%37 = call i32 @BN_to_ASN1_INTEGER(i32* %34, i32 %36)
%38 = load i32*, i32** %14
%39 = call i32 @BN_free(i32* %38)
%40 = load i32*, i32** %10
%41 = call i32 @X509_get_notBefore(i32* %40)
%42 = call i32 @X509_gmtime_adj(i32 %41, i64 -604800)
%43 = load i32*, i32** %10
%44 = call i32 @X509_get_notAfter(i32* %43)
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = mul nsw i64 86400, %46
%48 = call i32 @X509_gmtime_adj(i32 %44, i64 %47)
%49 = load i32*, i32** %10
%50 = load i32*, i32** %6
%51 = call i32 @X509_set_pubkey(i32* %49, i32* %50)
%52 = load i32*, i32** %10
%53 = call i32* @X509_get_subject_name(i32* %52)
store i32* %53, i32** %11
%54 = load i32*, i32** %11
%55 = load i8*, i8** %12
%56 = load i32, i32* @MBSTRING_ASC
%57 = load i8*, i8** %8
%58 = call i32 @X509_NAME_add_entry_by_txt(i32* %54, i8* %55, i32 %56, i8* %57, i32 -1, i32 -1, i32 0)
%59 = load i32*, i32** %10
%60 = load i32*, i32** %11
%61 = call i32 @X509_set_issuer_name(i32* %59, i32* %60)
%62 = load i32*, i32** %10
%63 = load i32, i32* @NID_key_usage
%64 = call i32 @add_ext(i32* %62, i32 %63, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.6, i64 0, i64 0))
%65 = load i32*, i32** %10
%66 = load i32, i32* @NID_ext_key_usage
%67 = load i8*, i8** %13
%68 = call i32 @add_ext(i32* %65, i32 %66, i8* %67)
%69 = load i8*, i8** %8
%70 = call i32 @strlen(i8* %69)
%71 = icmp slt i32 %70, 500
br i1 %71, label %72, label %80
72: ; preds = %27
%73 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%74 = load i8*, i8** %8
%75 = call i32 @strcat(i8* %73, i8* %74)
%76 = load i32*, i32** %10
%77 = load i32, i32* @NID_subject_alt_name
%78 = getelementptr inbounds [512 x i8], [512 x i8]* %16, i64 0, i64 0
%79 = call i32 @add_ext(i32* %76, i32 %77, i8* %78)
br label %80
80: ; preds = %72, %27
%81 = load i32*, i32** %10
%82 = call i32 @add_coulton(i32* %81)
%83 = load i32*, i32** %10
%84 = load i32*, i32** %6
%85 = load i32*, i32** %9
%86 = call i32 @X509_sign(i32* %83, i32* %84, i32* %85)
%87 = load i32*, i32** %10
store i32* %87, i32** %5
br label %88
88: ; preds = %80, %26
%89 = load i32*, i32** %5
ret i32* %89
}

LLVM IR (-O2):
@.str = [3 x i8] c"CN\00"
@.str.1 = [11 x i8] c"serverAuth\00"
@.str.2 = [5 x i8] c"DNS:\00"
@.str.3 = [13 x i8] c"emailAddress\00"
@.str.4 = [16 x i8] c"emailProtection\00"
@.str.5 = [7 x i8] c"email:\00"
@MBSTRING_ASC = global i32 0
@NID_key_usage = global i32 0
@.str.6 = [55 x i8] c"critical,digitalSignature,keyEncipherment,keyAgreement\00"
@NID_ext_key_usage = global i32 0
@NID_subject_alt_name = global i32 0
define i32* @makeselfcert(i32* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @commonname_type(i8* %2)
switch i32 %7, label %45 [
i32 1, label %9
i32 2, label %8
]
8: ; preds = %4
br label %9
9: ; preds = %4, %8
%10 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %8 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %4 ]
%11 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), %8 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %4 ]
%12 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %8 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %4 ]
%13 = call i32 @strcpy(i8* %6, i8* %10)
%14 = call i32* (...) @X509_new()
%15 = call i32 @X509_set_version(i32* %14, i32 2)
%16 = call i32* (...) @BN_new()
%17 = call i32 @BN_rand(i32* %16, i32 255, i32 1, i32 0)
%18 = call i32 @X509_get_serialNumber(i32* %14)
%19 = call i32 @BN_to_ASN1_INTEGER(i32* %16, i32 %18)
%20 = call i32 @BN_free(i32* %16)
%21 = call i32 @X509_get_notBefore(i32* %14)
%22 = call i32 @X509_gmtime_adj(i32 %21, i64 -604800)
%23 = call i32 @X509_get_notAfter(i32* %14)
%24 = sext i32 %1 to i64
%25 = mul nsw i64 %24, 86400
%26 = call i32 @X509_gmtime_adj(i32 %23, i64 %25)
%27 = call i32 @X509_set_pubkey(i32* %14, i32* %0)
%28 = call i32* @X509_get_subject_name(i32* %14)
%29 = load i32, i32* @MBSTRING_ASC
%30 = call i32 @X509_NAME_add_entry_by_txt(i32* %28, i8* %12, i32 %29, i8* %2, i32 -1, i32 -1, i32 0)
%31 = call i32 @X509_set_issuer_name(i32* %14, i32* %28)
%32 = load i32, i32* @NID_key_usage
%33 = call i32 @add_ext(i32* %14, i32 %32, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.6, i64 0, i64 0))
%34 = load i32, i32* @NID_ext_key_usage
%35 = call i32 @add_ext(i32* %14, i32 %34, i8* %11)
%36 = call i32 @strlen(i8* %2)
%37 = icmp slt i32 %36, 500
br i1 %37, label %38, label %42
38: ; preds = %9
%39 = call i32 @strcat(i8* %6, i8* %2)
%40 = load i32, i32* @NID_subject_alt_name
%41 = call i32 @add_ext(i32* %14, i32 %40, i8* %6)
br label %42
42: ; preds = %38, %9
%43 = call i32 @add_coulton(i32* %14)
%44 = call i32 @X509_sign(i32* %14, i32* %0, i32* %3)
br label %45
45: ; preds = %4, %42
%46 = phi i32* [ %14, %42 ], [ null, %4 ]
ret i32* %46
}