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

/* Variables and functions */
int /*<<< orphan*/ DH_GENERATOR_2;
int /*<<< orphan*/ DH_generate_parameters_ex(int /*<<< orphan*/ *, int,
                                             int /*<<< orphan*/,
                                             int /*<<< orphan*/);
int /*<<< orphan*/ *DH_new();
int /*<<< orphan*/ PEM_write_DHparams(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int generate_dhparam(int dh_bits) {
  DH *dh;
  char filename[128 + 1];
  FILE *out;
  sprintf(filename, "dh%d.pem", dh_bits);
  if ((out = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "Cannot create %s: aborting\n", filename);
    return -1;
  }
  dh = DH_new();
  printf("Generating DH parameters (%d bits) -- this can take long\n", dh_bits);
  DH_generate_parameters_ex(dh, dh_bits, DH_GENERATOR_2, 0);
  PEM_write_DHparams(out, dh);
  fclose(out);
  printf("done\n");
  return 0;
}


x86 assembly (clang, -O0):
<generate_dhparam>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe61(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0xa8(%rbp)
cmp    $0x0,%rax
jne    11f0 <generate_dhparam+0x70>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <generate_dhparam+0xd4>
mov    $0x0,%al
call   1070 <DH_new@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <DH_generate_parameters_ex@plt>
mov    -0xa8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <PEM_write_DHparams@plt>
mov    -0xa8(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_dhparam>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %edi,%ebp
lea    0xe5a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   10b0 <sprintf@plt>
lea    0xe4f(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     121d <generate_dhparam+0x8d>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <DH_new@plt>
mov    %rax,%r15
lea    0xe4a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %ebp,%esi
xor    %ecx,%ecx
call   1030 <DH_generate_parameters_ex@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <PEM_write_DHparams@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe4c(%rip),%rdi
call   1040 <puts@plt>
jmp    123b <generate_dhparam+0xab>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    %rsp,%rdx
call   1090 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"dh%d.pem\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Cannot create %s: aborting\0A\00"
@.str.3 = [58 x i8] c"Generating DH parameters (%d bits) -- this can take long\0A\00"
@DH_GENERATOR_2 = global i32 0
@.str.4 = [6 x i8] c"done\0A\00"
define i32 @generate_dhparam(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [129 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
%7 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %15)
store i32 -1, i32* %2
br label %31
17: ; preds = %1
%18 = call i32* (...) @DH_new()
store i32* %18, i32** %4
%19 = load i32, i32* %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32*, i32** %4
%22 = load i32, i32* %3
%23 = load i32, i32* @DH_GENERATOR_2
%24 = call i32 @DH_generate_parameters_ex(i32* %21, i32 %22, i32 %23, i32 0)
%25 = load i32*, i32** %6
%26 = load i32*, i32** %4
%27 = call i32 @PEM_write_DHparams(i32* %25, i32* %26)
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %31
31: ; preds = %17, %13
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [9 x i8] c"dh%d.pem\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Cannot create %s: aborting\0A\00"
@.str.3 = [58 x i8] c"Generating DH parameters (%d bits) -- this can take long\0A\00"
@DH_GENERATOR_2 = global i32 0
@str = [5 x i8] c"done\00"
define i32 @generate_dhparam(i32 %0) {
%2 = alloca [129 x i8]
%3 = getelementptr inbounds [129 x i8], [129 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %18
10: ; preds = %1
%11 = tail call i32* (...) @DH_new()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%13 = load i32, i32* @DH_GENERATOR_2
%14 = tail call i32 @DH_generate_parameters_ex(i32* %11, i32 %0, i32 %13, i32 0)
%15 = tail call i32 @PEM_write_DHparams(i32* %5, i32* %11)
%16 = tail call i32 @fclose(i32* %5)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %10, %7
%19 = phi i32 [ -1, %7 ], [ 0, %10 ]
ret i32 %19
}