#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ X509_NAME;
typedef int /*<<< orphan*/ X509;
struct TYPE_3__ {
  scalar_t__ *data;
};
typedef int /*<<< orphan*/ SSL;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef TYPE_1__ ASN1_INTEGER;

/* Variables and functions */
int /*<<< orphan*/ EVP_sha1();
int /*<<< orphan*/ MBSTRING_ASC;
int /*<<< orphan*/ SSL_Error(char *);
int /*<<< orphan*/ *SSL_get_peer_certificate(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_NAME_add_entry_by_txt(int /*<<< orphan*/ *, char *,
                                              int /*<<< orphan*/, char *, int,
                                              int, int /*<<< orphan*/);
int /*<<< orphan*/ *X509_NAME_new();
int /*<<< orphan*/ *X509_dup(int /*<<< orphan*/ *);
TYPE_1__ *X509_get_serialNumber(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_get_version(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_set_issuer_name(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_set_version(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ X509_sign(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             int /*<<< orphan*/);
X509 *create_fake_certificate(SSL *ssl_to_server, EVP_PKEY *key) {
  unsigned char buffer[128] = {0};
  int length = 0, loc;
  X509 *server_x509 = SSL_get_peer_certificate(ssl_to_server);
  X509 *fake_x509 = X509_dup(server_x509);
  if (server_x509 == NULL) {
    SSL_Error("Fail to get the certificate from server!");
  }
  X509_set_version(fake_x509, X509_get_version(server_x509));
  ASN1_INTEGER *a = X509_get_serialNumber(fake_x509);
  a->data[0] = a->data[0] + 1;
  X509_NAME *issuer = X509_NAME_new();
  X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC, "Thawte SGC CA", -1,
                             -1, 0);
  X509_NAME_add_entry_by_txt(issuer, "O", MBSTRING_ASC,
                             "Thawte Consulting (Pty) Ltd.", -1, -1, 0);
  X509_NAME_add_entry_by_txt(issuer, "OU", MBSTRING_ASC, "Thawte SGC CA", -1,
                             -1, 0);
  X509_set_issuer_name(fake_x509, issuer);
  X509_sign(fake_x509, key, EVP_sha1());
  return fake_x509;
}


x86 assembly (clang, -O0):
<create_fake_certificate>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
movl   $0x0,-0x94(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <SSL_get_peer_certificate@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rdi
call   10e0 <X509_dup@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jne    122d <create_fake_certificate+0x6d>
lea    0xdd8(%rip),%rdi
call   1030 <SSL_Error@plt>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0xa0(%rbp),%rdi
call   1060 <X509_get_version@plt>
mov    -0xd0(%rbp),%rdi
mov    %eax,%esi
call   1040 <X509_set_version@plt>
mov    -0xa8(%rbp),%rdi
call   1090 <X509_get_serialNumber@plt>
mov    %rax,-0xb0(%rbp)
mov    -0xb0(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    -0xb0(%rbp),%rax
mov    (%rax),%rax
mov    %rcx,(%rax)
mov    $0x0,%al
call   10a0 <X509_NAME_new@plt>
mov    %rax,-0xb8(%rbp)
mov    -0xb8(%rbp),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%edx
lea    0xd7e(%rip),%rsi
lea    0xd7a(%rip),%rcx
mov    $0xffffffff,%r9d
xor    %eax,%eax
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   10b0 <X509_NAME_add_entry_by_txt@plt>
mov    -0xb8(%rbp),%rdi
mov    0x2d11(%rip),%rax
mov    (%rax),%edx
lea    0xd5a(%rip),%rsi
lea    0xd55(%rip),%rcx
mov    $0xffffffff,%r9d
xor    %eax,%eax
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   10b0 <X509_NAME_add_entry_by_txt@plt>
mov    -0xb8(%rbp),%rdi
mov    0x2cdc(%rip),%rax
mov    (%rax),%edx
lea    0xd44(%rip),%rsi
lea    0xd10(%rip),%rcx
mov    $0xffffffff,%r9d
xor    %eax,%eax
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   10b0 <X509_NAME_add_entry_by_txt@plt>
mov    -0xa8(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
call   1080 <X509_set_issuer_name@plt>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    $0x0,%al
call   1050 <EVP_sha1@plt>
mov    -0xc8(%rbp),%rdi
mov    -0xc0(%rbp),%rsi
mov    %eax,%edx
call   10d0 <X509_sign@plt>
mov    -0xa8(%rbp),%rax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_fake_certificate>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
call   10b0 <SSL_get_peer_certificate@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10d0 <X509_dup@plt>
mov    %rax,%r15
test   %rbx,%rbx
jne    11e4 <create_fake_certificate+0x34>
lea    0xe21(%rip),%rdi
call   1030 <SSL_Error@plt>
mov    %rbx,%rdi
call   1060 <X509_get_version@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <X509_set_version@plt>
mov    %r15,%rdi
call   1080 <X509_get_serialNumber@plt>
mov    (%rax),%rax
addq   $0x1,(%rax)
xor    %eax,%eax
call   1090 <X509_NAME_new@plt>
mov    %rax,%rbx
mov    0x2dd2(%rip),%r13
mov    0x0(%r13),%edx
movl   $0x0,(%rsp)
lea    0xe01(%rip),%rsi
lea    0xdfd(%rip),%r12
mov    %rax,%rdi
mov    %r12,%rcx
mov    $0xffffffff,%r8d
mov    $0xffffffff,%r9d
call   10a0 <X509_NAME_add_entry_by_txt@plt>
mov    0x0(%r13),%edx
movl   $0x0,(%rsp)
lea    0xde2(%rip),%rsi
lea    0xddd(%rip),%rcx
mov    %rbx,%rdi
mov    $0xffffffff,%r8d
mov    $0xffffffff,%r9d
call   10a0 <X509_NAME_add_entry_by_txt@plt>
mov    0x0(%r13),%edx
movl   $0x0,(%rsp)
lea    0xdd4(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rcx
mov    $0xffffffff,%r8d
mov    $0xffffffff,%r9d
call   10a0 <X509_NAME_add_entry_by_txt@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <X509_set_issuer_name@plt>
xor    %eax,%eax
call   1050 <EVP_sha1@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   10c0 <X509_sign@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64* }
@.str = [41 x i8] c"Fail to get the certificate from server!\00"
@.str.1 = [3 x i8] c"CN\00"
@MBSTRING_ASC = global i32 0
@.str.2 = [14 x i8] c"Thawte SGC CA\00"
@.str.3 = [2 x i8] c"O\00"
@.str.4 = [29 x i8] c"Thawte Consulting (Pty) Ltd.\00"
@.str.5 = [3 x i8] c"OU\00"
define i32* @create_fake_certificate(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [128 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.TYPE_3__*
%11 = alloca i32*
store i32* %0, i32** %3
store i32* %1, i32** %4
%12 = bitcast [128 x i8]* %5 to i8*
store i32 0, i32* %6
%13 = load i32*, i32** %3
%14 = call i32* @SSL_get_peer_certificate(i32* %13)
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = call i32* @X509_dup(i32* %15)
store i32* %16, i32** %9
%17 = load i32*, i32** %8
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @SSL_Error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i32*, i32** %9
%23 = load i32*, i32** %8
%24 = call i32 @X509_get_version(i32* %23)
%25 = call i32 @X509_set_version(i32* %22, i32 %24)
%26 = load i32*, i32** %9
%27 = call %struct.TYPE_3__* @X509_get_serialNumber(i32* %26)
store %struct.TYPE_3__* %27, %struct.TYPE_3__** %10
%28 = load %struct.TYPE_3__*, %struct.TYPE_3__** %10
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %28, i32 0, i32 0
%30 = load i64*, i64** %29
%31 = getelementptr inbounds i64, i64* %30, i64 0
%32 = load i64, i64* %31
%33 = add nsw i64 %32, 1
%34 = load %struct.TYPE_3__*, %struct.TYPE_3__** %10
%35 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %34, i32 0, i32 0
%36 = load i64*, i64** %35
%37 = getelementptr inbounds i64, i64* %36, i64 0
store i64 %33, i64* %37
%38 = call i32* (...) @X509_NAME_new()
store i32* %38, i32** %11
%39 = load i32*, i32** %11
%40 = load i32, i32* @MBSTRING_ASC
%41 = call i32 @X509_NAME_add_entry_by_txt(i32* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%42 = load i32*, i32** %11
%43 = load i32, i32* @MBSTRING_ASC
%44 = call i32 @X509_NAME_add_entry_by_txt(i32* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%45 = load i32*, i32** %11
%46 = load i32, i32* @MBSTRING_ASC
%47 = call i32 @X509_NAME_add_entry_by_txt(i32* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %46, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%48 = load i32*, i32** %9
%49 = load i32*, i32** %11
%50 = call i32 @X509_set_issuer_name(i32* %48, i32* %49)
%51 = load i32*, i32** %9
%52 = load i32*, i32** %4
%53 = call i32 (...) @EVP_sha1()
%54 = call i32 @X509_sign(i32* %51, i32* %52, i32 %53)
%55 = load i32*, i32** %9
ret i32* %55
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64* }
@.str = [41 x i8] c"Fail to get the certificate from server!\00"
@.str.1 = [3 x i8] c"CN\00"
@MBSTRING_ASC = global i32 0
@.str.2 = [14 x i8] c"Thawte SGC CA\00"
@.str.3 = [2 x i8] c"O\00"
@.str.4 = [29 x i8] c"Thawte Consulting (Pty) Ltd.\00"
@.str.5 = [3 x i8] c"OU\00"
define i32* @create_fake_certificate(i32* %0, i32* %1) {
%3 = tail call i32* @SSL_get_peer_certificate(i32* %0)
%4 = tail call i32* @X509_dup(i32* %3)
%5 = icmp eq i32* %3, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @SSL_Error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = tail call i32 @X509_get_version(i32* %3)
%10 = tail call i32 @X509_set_version(i32* %4, i32 %9)
%11 = tail call %struct.TYPE_3__* @X509_get_serialNumber(i32* %4)
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %11, i64 0, i32 0
%13 = load i64*, i64** %12
%14 = load i64, i64* %13
%15 = add nsw i64 %14, 1
store i64 %15, i64* %13
%16 = tail call i32* (...) @X509_NAME_new()
%17 = load i32, i32* @MBSTRING_ASC
%18 = tail call i32 @X509_NAME_add_entry_by_txt(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%19 = load i32, i32* @MBSTRING_ASC
%20 = tail call i32 @X509_NAME_add_entry_by_txt(i32* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%21 = load i32, i32* @MBSTRING_ASC
%22 = tail call i32 @X509_NAME_add_entry_by_txt(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32 -1, i32 0)
%23 = tail call i32 @X509_set_issuer_name(i32* %4, i32* %16)
%24 = tail call i32 (...) @EVP_sha1()
%25 = tail call i32 @X509_sign(i32* %4, i32* %1, i32 %24)
ret i32* %4
}