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

/* Variables and functions */
int /*<<< orphan*/ ERR_load_crypto_strings();
int /*<<< orphan*/ ERR_print_errors_fp(int /*<<< orphan*/);
int /*<<< orphan*/ OpenSSL_add_all_algorithms();
int /*<<< orphan*/ *PEM_read_PrivateKey(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_X509(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *PKCS12_create(char *, char *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/, int /*<<< orphan*/,
                                  int /*<<< orphan*/, int /*<<< orphan*/,
                                  int /*<<< orphan*/);
int /*<<< orphan*/ PKCS12_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ i2d_PKCS12_fp(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  FILE *fp;
  EVP_PKEY *pkey;
  X509 *cert;
  PKCS12 *p12;
  if (argc != 5) {
    fprintf(stderr, "Usage: pkwrite infile password name p12file\n");
    exit(1);
  }
  OpenSSL_add_all_algorithms();
  ERR_load_crypto_strings();
  if ((fp = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Error opening file %s\n", argv[1]);
    exit(1);
  }
  cert = PEM_read_X509(fp, NULL, NULL, NULL);
  rewind(fp);
  pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  fclose(fp);
  p12 = PKCS12_create(argv[2], argv[3], pkey, cert, NULL, 0, 0, 0, 0, 0);
  if (!p12) {
    fprintf(stderr, "Error creating PKCS#12 structure\n");
    ERR_print_errors_fp(stderr);
    exit(1);
  }
  if ((fp = fopen(argv[4], "wb")) == NULL) {
    fprintf(stderr, "Error opening file %s\n", argv[1]);
    ERR_print_errors_fp(stderr);
    exit(1);
  }
  i2d_PKCS12_fp(fp, p12);
  PKCS12_free(p12);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     1211 <main+0x41>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    $0x0,%al
call   1090 <OpenSSL_add_all_algorithms@plt>
mov    $0x0,%al
call   10d0 <ERR_load_crypto_strings@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdff(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    126a <main+0x9a>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <PEM_read_X509@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10c0 <PEM_read_PrivateKey@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0x18(%rsp)
call   1050 <PKCS12_create@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    132c <main+0x15c>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
lea    0xd39(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10f0 <ERR_print_errors_fp@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
lea    0xd2d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1385 <main+0x1b5>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xcc9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
call   10f0 <ERR_print_errors_fp@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10a0 <i2d_PKCS12_fp@plt>
mov    -0x30(%rbp),%rdi
call   1040 <PKCS12_free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x5,%edi
jne    12b6 <main+0xe6>
mov    %rsi,%r14
xor    %eax,%eax
call   1090 <OpenSSL_add_all_algorithms@plt>
xor    %eax,%eax
call   10d0 <ERR_load_crypto_strings@plt>
mov    0x8(%r14),%rdi
lea    0xe30(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12d7 <main+0x107>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <PEM_read_X509@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10c0 <PEM_read_PrivateKey@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x10(%r14),%rdi
mov    0x18(%r14),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0x0
push   $0x0
push   $0x0
call   1050 <PKCS12_create@plt>
add    $0x20,%rsp
test   %rax,%rax
je     12fc <main+0x12c>
mov    %rax,%r15
mov    0x20(%r14),%rdi
lea    0xdec(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1315 <main+0x145>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   10a0 <i2d_PKCS12_fp@plt>
mov    %r15,%rdi
call   1040 <PKCS12_free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd44(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2cdd(%rip),%rbx
mov    (%rbx),%edi
lea    0xd3a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1330 <main+0x160>
mov    0x2cc4(%rip),%rbx
mov    (%rbx),%edi
mov    0x8(%r14),%rdx
lea    0xd06(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
call   10f0 <ERR_print_errors_fp@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"Usage: pkwrite infile password name p12file\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Error opening file %s\0A\00"
@.str.3 = [34 x i8] c"Error creating PKCS structure\0A\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 5
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 (...) @OpenSSL_add_all_algorithms()
%18 = call i32 (...) @ERR_load_crypto_strings()
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %31
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %16
%32 = load i32*, i32** %6
%33 = call i32* @PEM_read_X509(i32* %32, i32* null, i32* null, i32* null)
store i32* %33, i32** %8
%34 = load i32*, i32** %6
%35 = call i32 @rewind(i32* %34)
%36 = load i32*, i32** %6
%37 = call i32* @PEM_read_PrivateKey(i32* %36, i32* null, i32* null, i32* null)
store i32* %37, i32** %7
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 3
%45 = load i8*, i8** %44
%46 = load i32*, i32** %7
%47 = load i32*, i32** %8
%48 = call i32* @PKCS12_create(i8* %42, i8* %45, i32* %46, i32* %47, i32* null, i32 0, i32 0, i32 0, i32 0, i32 0)
store i32* %48, i32** %9
%49 = load i32*, i32** %9
%50 = icmp ne i32* %49, null
br i1 %50, label %57, label %51
51: ; preds = %31
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* @stderr
%55 = call i32 @ERR_print_errors_fp(i32 %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %31
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 4
%60 = load i8*, i8** %59
%61 = call i32* @fopen(i8* %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %61, i32** %6
%62 = icmp eq i32* %61, null
br i1 %62, label %63, label %72
63: ; preds = %57
%64 = load i32, i32* @stderr
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %67)
%69 = load i32, i32* @stderr
%70 = call i32 @ERR_print_errors_fp(i32 %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %57
%73 = load i32*, i32** %6
%74 = load i32*, i32** %9
%75 = call i32 @i2d_PKCS12_fp(i32* %73, i32* %74)
%76 = load i32*, i32** %9
%77 = call i32 @PKCS12_free(i32* %76)
%78 = load i32*, i32** %6
%79 = call i32 @fclose(i32* %78)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"Usage: pkwrite infile password name p12file\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Error opening file %s\0A\00"
@.str.3 = [34 x i8] c"Error creating PKCS structure\0A\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 5
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 (...) @OpenSSL_add_all_algorithms()
%10 = tail call i32 (...) @ERR_load_crypto_strings()
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %11
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = tail call i32* @PEM_read_X509(i32* %13, i32* null, i32* null, i32* null)
%22 = tail call i32 @rewind(i32* %13)
%23 = tail call i32* @PEM_read_PrivateKey(i32* %13, i32* null, i32* null, i32* null)
%24 = tail call i32 @fclose(i32* %13)
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8*, i8** %1, i64 3
%28 = load i8*, i8** %27
%29 = tail call i32* @PKCS12_create(i8* %26, i8* %28, i32* %23, i32* %21, i32* null, i32 0, i32 0, i32 0, i32 0, i32 0)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %37
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* @stderr
%35 = tail call i32 @ERR_print_errors_fp(i32 %34)
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %20
%38 = getelementptr inbounds i8*, i8** %1, i64 4
%39 = load i8*, i8** %38
%40 = tail call i32* @fopen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %49
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %11
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = load i32, i32* @stderr
%47 = tail call i32 @ERR_print_errors_fp(i32 %46)
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %37
%50 = tail call i32 @i2d_PKCS12_fp(i32* %40, i32* %29)
%51 = tail call i32 @PKCS12_free(i32* %29)
%52 = tail call i32 @fclose(i32* %40)
ret i32 0
}