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

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_file(char *, char *);
scalar_t__ DH_check(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ DH_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_bio_DHparams(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ SSL_CTX_set_tmp_dh(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ log_error(char *);
char *printf_dup(char *, char const *);
int load_dh(const char *workdir, SSL_CTX *ctx) {
  char *path = printf_dup("%s/keys/dh_params", workdir);
  BIO *f = BIO_new_file(path, "r");
  if (!f)
    goto err;
  DH *dh = PEM_read_bio_DHparams(f, NULL, NULL, NULL);
  if (!dh)
    goto err;
  int codes;
  if (DH_check(dh, &codes) == 0 || codes) {
    log_error("Loaded DH parameters look bad!");
    goto err;
  }
  SSL_CTX_set_tmp_dh(ctx, dh);
  DH_free(dh);
  BIO_free(f);
  free(path);
  return 0;
err:
  if (f)
    BIO_free(f);
  if (path)
    free(path);
  return -1;
}


x86 assembly (clang, -O0):
<load_dh>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1050 <printf_dup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   10b0 <BIO_new_file@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d8 <load_dh+0x48>
jmp    1265 <load_dh+0xd5>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1060 <PEM_read_bio_DHparams@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ff <load_dh+0x6f>
jmp    1265 <load_dh+0xd5>
mov    -0x30(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   10a0 <DH_check@plt>
cmp    $0x0,%rax
je     1220 <load_dh+0x90>
cmpl   $0x0,-0x34(%rbp)
je     1231 <load_dh+0xa1>
lea    0xded(%rip),%rdi
call   1040 <log_error@plt>
jmp    1265 <load_dh+0xd5>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <SSL_CTX_set_tmp_dh@plt>
mov    -0x30(%rbp),%rdi
call   1070 <DH_free@plt>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <load_dh+0x104>
cmpq   $0x0,-0x28(%rbp)
je     1279 <load_dh+0xe9>
mov    -0x28(%rbp),%rdi
call   1090 <BIO_free@plt>
cmpq   $0x0,-0x20(%rbp)
je     128d <load_dh+0xfd>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_dh>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rsi
lea    0xe5b(%rip),%rdi
call   1050 <printf_dup@plt>
mov    %rax,%r14
lea    0xe5e(%rip),%rsi
mov    %rax,%rdi
call   10b0 <BIO_new_file@plt>
test   %rax,%rax
je     122e <load_dh+0x9e>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <PEM_read_bio_DHparams@plt>
test   %rax,%rax
je     1226 <load_dh+0x96>
mov    %rax,%r12
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
call   10a0 <DH_check@plt>
test   %rax,%rax
je     121a <load_dh+0x8a>
cmpl   $0x0,0x4(%rsp)
jne    121a <load_dh+0x8a>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <SSL_CTX_set_tmp_dh@plt>
mov    %r12,%rdi
call   1070 <DH_free@plt>
mov    %rbx,%rdi
call   1090 <BIO_free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1240 <load_dh+0xb0>
lea    0xdf3(%rip),%rdi
call   1040 <log_error@plt>
mov    %rbx,%rdi
call   1090 <BIO_free@plt>
mov    $0xffffffff,%ebx
test   %r14,%r14
je     1240 <load_dh+0xb0>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"%s/keys/dh_params\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Loaded DH parameters look bad!\00"
define i32 @load_dh(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = call i8* @printf_dup(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = call i32* @BIO_new_file(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
br label %42
17: ; preds = %2
%18 = load i32*, i32** %7
%19 = call i32* @PEM_read_bio_DHparams(i32* %18, i32* null, i32* null, i32* null)
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %17
br label %42
23: ; preds = %17
%24 = load i32*, i32** %8
%25 = call i64 @DH_check(i32* %24, i32* %9)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27, %23
%31 = call i32 @log_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %42
32: ; preds = %27
%33 = load i32*, i32** %5
%34 = load i32*, i32** %8
%35 = call i32 @SSL_CTX_set_tmp_dh(i32* %33, i32* %34)
%36 = load i32*, i32** %8
%37 = call i32 @DH_free(i32* %36)
%38 = load i32*, i32** %7
%39 = call i32 @BIO_free(i32* %38)
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
store i32 0, i32* %3
br label %55
42: ; preds = %30, %22, %16
%43 = load i32*, i32** %7
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32*, i32** %7
%47 = call i32 @BIO_free(i32* %46)
br label %48
48: ; preds = %45, %42
%49 = load i8*, i8** %6
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %51, %48
store i32 -1, i32* %3
br label %55
55: ; preds = %54, %32
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [18 x i8] c"%s/keys/dh_params\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Loaded DH parameters look bad!\00"
define i32 @load_dh(i8* %0, i32* %1) {
%3 = alloca i32
%4 = tail call i8* @printf_dup(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32* @BIO_new_file(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %25, label %7
7: ; preds = %2
%8 = tail call i32* @PEM_read_bio_DHparams(i32* %5, i32* null, i32* null, i32* null)
%9 = icmp eq i32* %8, null
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = call i64 @DH_check(i32* %8, i32* %3)
%12 = icmp eq i64 %11, 0
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 0
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @log_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %23
18: ; preds = %10
%19 = call i32 @SSL_CTX_set_tmp_dh(i32* %1, i32* %8)
%20 = call i32 @DH_free(i32* %8)
%21 = call i32 @BIO_free(i32* %5)
%22 = call i32 @free(i8* %4)
br label %29
23: ; preds = %16, %7
%24 = call i32 @BIO_free(i32* %5)
br label %25
25: ; preds = %2, %23
%26 = icmp eq i8* %4, null
br i1 %26, label %29, label %27
27: ; preds = %25
%28 = call i32 @free(i8* %4)
br label %29
29: ; preds = %25, %27, %18
%30 = phi i32 [ 0, %18 ], [ -1, %27 ], [ -1, %25 ]
ret i32 %30
}