#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;
typedef int /*<<< orphan*/ SSL_METHOD;
typedef int /*<<< orphan*/ SSL_CTX;
typedef int /*<<< orphan*/ SSL;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ *BIO_new_file(char const *, char *);
int /*<<< orphan*/ BIO_s_file();
int /*<<< orphan*/ ERR_load_BIO_strings();
int /*<<< orphan*/ ERR_load_crypto_strings();
int /*<<< orphan*/ OpenSSL_add_all_algorithms();
int /*<<< orphan*/ PEM_write_bio_X509(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SSL_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_CTX_new(int /*<<< orphan*/ const *);
int /*<<< orphan*/ SSL_CTX_set_options(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/);
int /*<<< orphan*/ SSL_OP_NO_SSLv2;
int /*<<< orphan*/ SSL_connect(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_get_peer_certificate(int /*<<< orphan*/ *);
scalar_t__ SSL_library_init();
int /*<<< orphan*/ SSL_load_error_strings();
int /*<<< orphan*/ *SSL_new(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_set_fd(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *SSLv23_client_method();
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int create_socket(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ raise(int /*<<< orphan*/);
int save_ssl_certificate_at_path(char dest_url[], const char *filename) {
  BIO *certbio = NULL;
  BIO *outbio = NULL;
  X509 *cert = NULL;
  const SSL_METHOD *method;
  SSL_CTX *ctx;
  SSL *ssl;
  int server = 0;
  OpenSSL_add_all_algorithms();
  ERR_load_BIO_strings();
  ERR_load_crypto_strings();
  SSL_load_error_strings();
  certbio = BIO_new(BIO_s_file());
  outbio = BIO_new_file(filename, "w");
  if (SSL_library_init() < 0) {
    raise(SIGINT);
  }
  method = SSLv23_client_method();
  if ((ctx = SSL_CTX_new(method)) == NULL) {
    raise(SIGINT);
  }
  SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
  ssl = SSL_new(ctx);
  server = create_socket(dest_url, outbio);
  SSL_set_fd(ssl, server);
  SSL_connect(ssl);
  cert = SSL_get_peer_certificate(ssl);
  if (cert == NULL)
    raise(SIGINT);
  PEM_write_bio_X509(outbio, cert);
  SSL_free(ssl);
  close(server);
  X509_free(cert);
  SSL_CTX_free(ctx);
  return (0);
}


x86 assembly (clang, -O0):
<save_ssl_certificate_at_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
mov    $0x0,%al
call   10b0 <OpenSSL_add_all_algorithms@plt>
mov    $0x0,%al
call   1170 <ERR_load_BIO_strings@plt>
mov    $0x0,%al
call   1150 <ERR_load_crypto_strings@plt>
mov    $0x0,%al
call   1140 <SSL_load_error_strings@plt>
mov    $0x0,%al
call   1160 <BIO_s_file@plt>
mov    %eax,%edi
call   10f0 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd38(%rip),%rsi
call   1180 <BIO_new_file@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1090 <SSL_library_init@plt>
cmp    $0x0,%rax
jge    12f0 <save_ssl_certificate_at_path+0x90>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
mov    $0x0,%al
call   1050 <SSLv23_client_method@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10d0 <SSL_CTX_new@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1320 <save_ssl_certificate_at_path+0xc0>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
mov    -0x38(%rbp),%rdi
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
call   1040 <SSL_CTX_set_options@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <SSL_new@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1100 <create_socket@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1070 <SSL_set_fd@plt>
mov    -0x40(%rbp),%rdi
call   1120 <SSL_connect@plt>
mov    -0x40(%rbp),%rdi
call   10e0 <SSL_get_peer_certificate@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    138a <save_ssl_certificate_at_path+0x12a>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10c0 <PEM_write_bio_X509@plt>
mov    -0x40(%rbp),%rdi
call   1060 <SSL_free@plt>
mov    -0x44(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%rdi
call   1110 <X509_free@plt>
mov    -0x38(%rbp),%rdi
call   1130 <SSL_CTX_free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_ssl_certificate_at_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   10b0 <OpenSSL_add_all_algorithms@plt>
xor    %eax,%eax
call   1170 <ERR_load_BIO_strings@plt>
xor    %eax,%eax
call   1150 <ERR_load_crypto_strings@plt>
xor    %eax,%eax
call   1140 <SSL_load_error_strings@plt>
xor    %eax,%eax
call   1160 <BIO_s_file@plt>
mov    %eax,%edi
call   10f0 <BIO_new@plt>
lea    0xd61(%rip),%rsi
mov    %rbx,%rdi
call   1180 <BIO_new_file@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1090 <SSL_library_init@plt>
test   %rax,%rax
jns    12c4 <save_ssl_certificate_at_path+0x64>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
xor    %eax,%eax
call   1050 <SSLv23_client_method@plt>
mov    %rax,%rdi
call   10d0 <SSL_CTX_new@plt>
mov    %rax,%r12
test   %rax,%rax
jne    12e9 <save_ssl_certificate_at_path+0x89>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1040 <SSL_CTX_set_options@plt>
mov    %r12,%rdi
call   10a0 <SSL_new@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
call   1100 <create_socket@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <SSL_set_fd@plt>
mov    %rbx,%rdi
call   1120 <SSL_connect@plt>
mov    %rbx,%rdi
call   10e0 <SSL_get_peer_certificate@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1343 <save_ssl_certificate_at_path+0xe3>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
call   1030 <raise@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   10c0 <PEM_write_bio_X509@plt>
mov    %rbx,%rdi
call   1060 <SSL_free@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %rbp,%rdi
call   1110 <X509_free@plt>
mov    %r12,%rdi
call   1130 <SSL_CTX_free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@SIGINT = global i32 0
@SSL_OP_NO_SSLv2 = global i32 0
define i32 @save_ssl_certificate_at_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32* null, i32** %5
store i32* null, i32** %6
store i32* null, i32** %7
store i32 0, i32* %11
%12 = call i32 (...) @OpenSSL_add_all_algorithms()
%13 = call i32 (...) @ERR_load_BIO_strings()
%14 = call i32 (...) @ERR_load_crypto_strings()
%15 = call i32 (...) @SSL_load_error_strings()
%16 = call i32 (...) @BIO_s_file()
%17 = call i32* @BIO_new(i32 %16)
store i32* %17, i32** %5
%18 = load i8*, i8** %4
%19 = call i32* @BIO_new_file(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %6
%20 = call i64 (...) @SSL_library_init()
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = load i32, i32* @SIGINT
%24 = call i32 @raise(i32 %23)
br label %25
25: ; preds = %22, %2
%26 = call i32* (...) @SSLv23_client_method()
store i32* %26, i32** %8
%27 = load i32*, i32** %8
%28 = call i32* @SSL_CTX_new(i32* %27)
store i32* %28, i32** %9
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* @SIGINT
%32 = call i32 @raise(i32 %31)
br label %33
33: ; preds = %30, %25
%34 = load i32*, i32** %9
%35 = load i32, i32* @SSL_OP_NO_SSLv2
%36 = call i32 @SSL_CTX_set_options(i32* %34, i32 %35)
%37 = load i32*, i32** %9
%38 = call i32* @SSL_new(i32* %37)
store i32* %38, i32** %10
%39 = load i8*, i8** %3
%40 = load i32*, i32** %6
%41 = call i32 @create_socket(i8* %39, i32* %40)
store i32 %41, i32* %11
%42 = load i32*, i32** %10
%43 = load i32, i32* %11
%44 = call i32 @SSL_set_fd(i32* %42, i32 %43)
%45 = load i32*, i32** %10
%46 = call i32 @SSL_connect(i32* %45)
%47 = load i32*, i32** %10
%48 = call i32* @SSL_get_peer_certificate(i32* %47)
store i32* %48, i32** %7
%49 = load i32*, i32** %7
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %33
%52 = load i32, i32* @SIGINT
%53 = call i32 @raise(i32 %52)
br label %54
54: ; preds = %51, %33
%55 = load i32*, i32** %6
%56 = load i32*, i32** %7
%57 = call i32 @PEM_write_bio_X509(i32* %55, i32* %56)
%58 = load i32*, i32** %10
%59 = call i32 @SSL_free(i32* %58)
%60 = load i32, i32* %11
%61 = call i32 @close(i32 %60)
%62 = load i32*, i32** %7
%63 = call i32 @X509_free(i32* %62)
%64 = load i32*, i32** %9
%65 = call i32 @SSL_CTX_free(i32* %64)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@SIGINT = global i32 0
@SSL_OP_NO_SSLv2 = global i32 0
define i32 @save_ssl_certificate_at_path(i8* %0, i8* %1) {
%3 = tail call i32 (...) @OpenSSL_add_all_algorithms()
%4 = tail call i32 (...) @ERR_load_BIO_strings()
%5 = tail call i32 (...) @ERR_load_crypto_strings()
%6 = tail call i32 (...) @SSL_load_error_strings()
%7 = tail call i32 (...) @BIO_s_file()
%8 = tail call i32* @BIO_new(i32 %7)
%9 = tail call i32* @BIO_new_file(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = tail call i64 (...) @SSL_library_init()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @SIGINT
%14 = tail call i32 @raise(i32 %13)
br label %15
15: ; preds = %12, %2
%16 = tail call i32* (...) @SSLv23_client_method()
%17 = tail call i32* @SSL_CTX_new(i32* %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @SIGINT
%21 = tail call i32 @raise(i32 %20)
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* @SSL_OP_NO_SSLv2
%24 = tail call i32 @SSL_CTX_set_options(i32* %17, i32 %23)
%25 = tail call i32* @SSL_new(i32* %17)
%26 = tail call i32 @create_socket(i8* %0, i32* %9)
%27 = tail call i32 @SSL_set_fd(i32* %25, i32 %26)
%28 = tail call i32 @SSL_connect(i32* %25)
%29 = tail call i32* @SSL_get_peer_certificate(i32* %25)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = load i32, i32* @SIGINT
%33 = tail call i32 @raise(i32 %32)
br label %34
34: ; preds = %31, %22
%35 = tail call i32 @PEM_write_bio_X509(i32* %9, i32* %29)
%36 = tail call i32 @SSL_free(i32* %25)
%37 = tail call i32 @close(i32 %26)
%38 = tail call i32 @X509_free(i32* %29)
%39 = tail call i32 @SSL_CTX_free(i32* %17)
ret i32 0
}