#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*/ SSL_CTX;

/* Variables and functions */
int /*<<< orphan*/ SSL_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_CTX_load_verify_locations(int /*<<< orphan*/ *,
                                                 char const *,
                                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ *SSL_CTX_new(int /*<<< orphan*/);
int /*<<< orphan*/ SSL_CTX_set_mode(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ SSL_CTX_set_verify(int /*<<< orphan*/ *, int,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_CTX_use_PrivateKey_file(int /*<<< orphan*/ *,
                                               char const *,
                                               int /*<<< orphan*/);
int /*<<< orphan*/ SSL_CTX_use_certificate_chain_file(int /*<<< orphan*/ *,
                                                      char const *);
int /*<<< orphan*/ SSL_FILETYPE_PEM;
int /*<<< orphan*/ SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER;
int SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
int SSL_VERIFY_PEER;
int /*<<< orphan*/ SSLv23_client_method();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
SSL_CTX *setup_ctx(const char *ca_file, const char *cert_file,
                   const char *key_file, const char *cipher) {
  SSL_CTX *ctx;
  (void)cipher;
  ctx = SSL_CTX_new(SSLv23_client_method());
  if (NULL == ctx)
    goto out;
  SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
                     NULL);
  if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) {
    fprintf(stderr, "%s: err loading ca file\n", ca_file);
    goto out_free;
  }
  if (cert_file && key_file) {
    if (!SSL_CTX_use_certificate_chain_file(ctx, cert_file)) {
      fprintf(stderr, "%s: err loading cert file\n", cert_file);
      goto out_free;
    }
    if (!SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM)) {
      fprintf(stderr, "%s: err loading key file\n", key_file);
      goto out_free;
    }
  }
  if (cipher != NULL) {
    fprintf(stderr, "Krypton doesn't support setting cipher spec\n");
    goto out_free;
  }
  goto out;
out_free:
  SSL_CTX_free(ctx);
  ctx = NULL;
out:
  return ctx;
}


x86 assembly (clang, -O0):
<setup_ctx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x0,%al
call   1030 <SSLv23_client_method@plt>
mov    %eax,%edi
call   1070 <SSL_CTX_new@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11cb <setup_ctx+0x3b>
jmp    1303 <setup_ctx+0x173>
mov    -0x28(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   10b0 <SSL_CTX_set_mode@plt>
mov    -0x28(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <SSL_CTX_set_verify@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <SSL_CTX_load_verify_locations@plt>
cmp    $0x0,%eax
jne    1236 <setup_ctx+0xa6>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12f2 <setup_ctx+0x162>
cmpq   $0x0,-0x10(%rbp)
je     12c6 <setup_ctx+0x136>
cmpq   $0x0,-0x18(%rbp)
je     12c6 <setup_ctx+0x136>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <SSL_CTX_use_certificate_chain_file@plt>
cmp    $0x0,%eax
jne    1282 <setup_ctx+0xf2>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12f2 <setup_ctx+0x162>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d57(%rip),%rax
mov    (%rax),%edx
call   1090 <SSL_CTX_use_PrivateKey_file@plt>
cmp    $0x0,%eax
jne    12c1 <setup_ctx+0x131>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12f2 <setup_ctx+0x162>
jmp    12c6 <setup_ctx+0x136>
cmpq   $0x0,-0x20(%rbp)
je     12ed <setup_ctx+0x15d>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12f2 <setup_ctx+0x162>
jmp    1303 <setup_ctx+0x173>
mov    -0x28(%rbp),%rdi
call   10a0 <SSL_CTX_free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_ctx>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
xor    %eax,%eax
call   1030 <SSLv23_client_method@plt>
mov    %eax,%edi
call   1070 <SSL_CTX_new@plt>
test   %rax,%rax
je     1298 <setup_ctx+0x108>
mov    %rax,%rbx
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <SSL_CTX_set_mode@plt>
mov    0x2df1(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <SSL_CTX_set_verify@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
xor    %edx,%edx
call   1080 <SSL_CTX_load_verify_locations@plt>
test   %eax,%eax
je     124c <setup_ctx+0xbc>
test   %r12,%r12
je     122e <setup_ctx+0x9e>
test   %r14,%r14
je     122e <setup_ctx+0x9e>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <SSL_CTX_use_certificate_chain_file@plt>
test   %eax,%eax
je     1261 <setup_ctx+0xd1>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <SSL_CTX_use_PrivateKey_file@plt>
test   %eax,%eax
je     1276 <setup_ctx+0xe6>
test   %r15,%r15
je     129a <setup_ctx+0x10a>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1290 <setup_ctx+0x100>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    %r13,%rdx
jmp    1289 <setup_ctx+0xf9>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
mov    %r12,%rdx
jmp    1289 <setup_ctx+0xf9>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <SSL_CTX_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER = global i32 0
@SSL_VERIFY_PEER = global i32 0
@SSL_VERIFY_FAIL_IF_NO_PEER_CERT = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"%s: err loading ca file\0A\00"
@.str.1 = [27 x i8] c"%s: err loading cert file\0A\00"
@SSL_FILETYPE_PEM = global i32 0
@.str.2 = [26 x i8] c"%s: err loading key file\0A\00"
@.str.3 = [45 x i8] c"Krypton doesn't support setting cipher spec\0A\00"
define i32* @setup_ctx(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%10 = load i8*, i8** %8
%11 = call i32 (...) @SSLv23_client_method()
%12 = call i32* @SSL_CTX_new(i32 %11)
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = icmp eq i32* null, %13
br i1 %14, label %15, label %16
15: ; preds = %4
br label %69
16: ; preds = %4
%17 = load i32*, i32** %9
%18 = load i32, i32* @SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
%19 = call i32 @SSL_CTX_set_mode(i32* %17, i32 %18)
%20 = load i32*, i32** %9
%21 = load i32, i32* @SSL_VERIFY_PEER
%22 = load i32, i32* @SSL_VERIFY_FAIL_IF_NO_PEER_CERT
%23 = or i32 %21, %22
%24 = call i32 @SSL_CTX_set_verify(i32* %20, i32 %23, i32* null)
%25 = load i32*, i32** %9
%26 = load i8*, i8** %5
%27 = call i32 @SSL_CTX_load_verify_locations(i32* %25, i8* %26, i32* null)
%28 = icmp ne i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %5
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %31)
br label %66
33: ; preds = %16
%34 = load i8*, i8** %6
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %59
36: ; preds = %33
%37 = load i8*, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %59
39: ; preds = %36
%40 = load i32*, i32** %9
%41 = load i8*, i8** %6
%42 = call i32 @SSL_CTX_use_certificate_chain_file(i32* %40, i8* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %6
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %46)
br label %66
48: ; preds = %39
%49 = load i32*, i32** %9
%50 = load i8*, i8** %7
%51 = load i32, i32* @SSL_FILETYPE_PEM
%52 = call i32 @SSL_CTX_use_PrivateKey_file(i32* %49, i8* %50, i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = load i8*, i8** %7
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %56)
br label %66
58: ; preds = %48
br label %59
59: ; preds = %58, %36, %33
%60 = load i8*, i8** %8
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %66
65: ; preds = %59
br label %69
66: ; preds = %62, %54, %44, %29
%67 = load i32*, i32** %9
%68 = call i32 @SSL_CTX_free(i32* %67)
store i32* null, i32** %9
br label %69
69: ; preds = %66, %65, %15
%70 = load i32*, i32** %9
ret i32* %70
}

LLVM IR (-O2):
@SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER = global i32 0
@SSL_VERIFY_PEER = global i32 0
@SSL_VERIFY_FAIL_IF_NO_PEER_CERT = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"%s: err loading ca file\0A\00"
@.str.1 = [27 x i8] c"%s: err loading cert file\0A\00"
@SSL_FILETYPE_PEM = global i32 0
@.str.2 = [26 x i8] c"%s: err loading key file\0A\00"
@.str.3 = [45 x i8] c"Krypton doesn't support setting cipher spec\0A\00"
define i32* @setup_ctx(i8* %0, i8* %1, i8* %2, i8* readnone %3) {
%5 = tail call i32 (...) @SSLv23_client_method()
%6 = tail call i32* @SSL_CTX_new(i32 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %44, label %8
8: ; preds = %4
%9 = load i32, i32* @SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
%10 = tail call i32 @SSL_CTX_set_mode(i32* %6, i32 %9)
%11 = load i32, i32* @SSL_VERIFY_PEER
%12 = load i32, i32* @SSL_VERIFY_FAIL_IF_NO_PEER_CERT
%13 = or i32 %12, %11
%14 = tail call i32 @SSL_CTX_set_verify(i32* %6, i32 %13, i32* null)
%15 = tail call i32 @SSL_CTX_load_verify_locations(i32* %6, i8* %0, i32* null)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %42
20: ; preds = %8
%21 = icmp ne i8* %1, null
%22 = icmp ne i8* %2, null
%23 = and i1 %21, %22
br i1 %23, label %24, label %37
24: ; preds = %20
%25 = tail call i32 @SSL_CTX_use_certificate_chain_file(i32* %6, i8* %1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %42
30: ; preds = %24
%31 = load i32, i32* @SSL_FILETYPE_PEM
%32 = tail call i32 @SSL_CTX_use_PrivateKey_file(i32* %6, i8* %2, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %42
37: ; preds = %30, %20
%38 = icmp eq i8* %3, null
br i1 %38, label %44, label %39
39: ; preds = %37
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %39, %34, %27, %17
%43 = tail call i32 @SSL_CTX_free(i32* %6)
br label %44
44: ; preds = %37, %4, %42
%45 = phi i32* [ null, %4 ], [ null, %42 ], [ %6, %37 ]
ret i32* %45
}