#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;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ stderr;
void output_der(unsigned char *buffer, int offs, size_t size, long *count) {
  char filename[15];
  sprintf(filename, "privkey-%02li.der", *count);
  FILE *fp = fopen(filename, "wb");
  if (fp == NULL) {
    perror("fopen()");
    fprintf(stderr, "Failed to open %s.\n", filename);
    exit(-1);
  } else {
    fwrite(&buffer[offs], 1, size, fp);
    printf("Wrote key to file %s.\n", filename);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<output_der>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x2f(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xe57(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x2f(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11fc <output_der+0x7c>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    -0x2f(%rbp),%rdx
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
lea    -0x2f(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<output_der>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
mov    (%rcx),%rdx
lea    0xe64(%rip),%rsi
lea    0x9(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sprintf@plt>
lea    0xe62(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11fd <output_der+0x7d>
mov    %rax,%rbx
movslq %ebp,%rax
add    %rax,%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
lea    0xe51(%rip),%rdi
lea    0x9(%rsp),%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
lea    0x9(%rsp),%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"privkey-%02li.der\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [8 x i8] c"fopen()\00"
@stderr = global i32 0
@.str.3 = [20 x i8] c"Failed to open %s.\0A\00"
@.str.4 = [23 x i8] c"Wrote key to file %s.\0A\00"
define void @output_der(i8* %0, i32 %1, i64 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64*
%9 = alloca [15 x i8]
%10 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i64* %3, i64** %8
%11 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%12 = load i64*, i64** %8
%13 = load i64, i64* %12
%14 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %13)
%15 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %10
%17 = load i32*, i32** %10
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %25
19: ; preds = %4
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %4
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i64, i64* %7
%31 = load i32*, i32** %10
%32 = call i32 @fwrite(i8* %29, i32 1, i64 %30, i32* %31)
%33 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%34 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %25
%36 = load i32*, i32** %10
%37 = call i32 @fclose(i32* %36)
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"privkey-%02li.der\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [8 x i8] c"fopen()\00"
@stderr = global i32 0
@.str.3 = [20 x i8] c"Failed to open %s.\0A\00"
@.str.4 = [23 x i8] c"Wrote key to file %s.\0A\00"
define void @output_der(i8* nocapture %0, i32 %1, i64 %2, i64* nocapture %3) {
%5 = alloca [15 x i8]
%6 = getelementptr inbounds [15 x i8], [15 x i8]* %5, i64 0, i64 0
%7 = load i64, i64* %3
%8 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %7)
%9 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %4
%12 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %4
%17 = sext i32 %1 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = tail call i32 @fwrite(i8* %18, i32 1, i64 %2, i32* %9)
%20 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%21 = call i32 @fclose(i32* %9)
ret void
}