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

/* Variables and functions */
int /*<<< orphan*/ *EVP_PKEY_get1_RSA(int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_RSA_PUBKEY(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ PEM_read_X509(int /*<<< orphan*/ *, int /*<<< orphan*/ **,
                                 int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_get_pubkey(int /*<<< orphan*/ *);
scalar_t__ check(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ output(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strrchr(char *, char);
int main(int argc, char *argv[]) {
  int cert_mode = 0;
  FILE *fp;
  X509 *cert = NULL;
  RSA *pubkey = NULL;
  EVP_PKEY *key;
  char *progname;
  if (argc != 3 || (strcmp(argv[1], "-cert") && strcmp(argv[1], "-pub"))) {
    progname = strrchr(argv[0], '/');
    if (progname)
      progname++;
    else
      progname = argv[0];
    fprintf(stderr, "Usage: %s <-cert | -pub> <file>\n", progname);
    return -1;
  }
  if (!strcmp(argv[1], "-cert"))
    cert_mode = 1;
  fp = fopen(argv[2], "r");
  if (!fp) {
    fprintf(stderr, "Couldn't open file %s!\n", argv[2]);
    return -1;
  }
  if (cert_mode) {
    if (!PEM_read_X509(fp, &cert, NULL, NULL)) {
      fprintf(stderr, "Couldn't read certificate.\n");
      goto fail;
    }
    key = X509_get_pubkey(cert);
    if (!(pubkey = EVP_PKEY_get1_RSA(key))) {
      fprintf(stderr, "Couldn't convert to a RSA style key.\n");
      goto fail;
    }
  } else {
    if (!(pubkey = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL))) {
      fprintf(stderr, "Couldn't read public key file.\n");
      goto fail;
    }
  }
  if (check(pubkey)) {
    output(pubkey);
  }
fail:
  X509_free(cert);
  RSA_free(pubkey);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    1243 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdea(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12a6 <main+0xd6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd2(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12a6 <main+0xd6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1274 <main+0xa4>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    127f <main+0xaf>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1426 <main+0x256>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd4b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12cb <main+0xfb>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd52(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1319 <main+0x149>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd28(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1426 <main+0x256>
cmpl   $0x0,-0x14(%rbp)
je     13a1 <main+0x1d1>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1030 <PEM_read_X509@plt>
cmp    $0x0,%eax
jne    135c <main+0x18c>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
lea    0xcf6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1404 <main+0x234>
mov    -0x28(%rbp),%rdi
call   10a0 <X509_get_pubkey@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10b0 <EVP_PKEY_get1_RSA@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    139c <main+0x1cc>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
lea    0xcd2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1404 <main+0x234>
jmp    13e3 <main+0x213>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10f0 <PEM_read_RSA_PUBKEY@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    13de <main+0x20e>
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
lea    0xcb6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1404 <main+0x234>
jmp    13e3 <main+0x213>
mov    -0x30(%rbp),%rdi
call   10e0 <check@plt>
cmp    $0x0,%rax
je     13ff <main+0x22f>
mov    -0x30(%rbp),%rdi
call   1040 <output@plt>
jmp    1404 <main+0x234>
mov    -0x28(%rbp),%rdi
call   10c0 <X509_free@plt>
mov    -0x30(%rbp),%rdi
call   1060 <RSA_free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movq   $0x0,0x8(%rsp)
cmp    $0x3,%edi
jne    1214 <main+0x44>
mov    0x8(%rbx),%rdi
lea    0xe0b(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1255 <main+0x85>
mov    0x8(%rbx),%rdi
lea    0xdfc(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1255 <main+0x85>
mov    (%rbx),%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%rdx
test   %rax,%rax
cmove  %rbx,%rdx
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rbx),%rdi
lea    0xda0(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%r15
mov    0x10(%rbx),%rdi
lea    0xdb9(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12b0 <main+0xe0>
mov    %rax,%r14
test   %r15,%r15
je     12c9 <main+0xf9>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10f0 <PEM_read_RSA_PUBKEY@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12f8 <main+0x128>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
jmp    1331 <main+0x161>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xd6a(%rip),%rsi
jmp    123f <main+0x6f>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <PEM_read_X509@plt>
test   %eax,%eax
je     130f <main+0x13f>
mov    0x8(%rsp),%rdi
call   10a0 <X509_get_pubkey@plt>
mov    %rax,%rdi
call   10b0 <EVP_PKEY_get1_RSA@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1321 <main+0x151>
mov    %rbx,%rdi
call   10e0 <check@plt>
test   %rax,%rax
je     133a <main+0x16a>
mov    %rbx,%rdi
call   1040 <output@plt>
jmp    133a <main+0x16a>
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
jmp    1331 <main+0x161>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    0xd31(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   10c0 <X509_free@plt>
mov    %rbx,%rdi
call   1060 <RSA_free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    124b <main+0x7b>

LLVM IR (-O0):
@.str = [6 x i8] c"-cert\00"
@.str.1 = [5 x i8] c"-pub\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Usage: %s <-cert | -pub> <file>\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [24 x i8] c"Couldn't open file %s!\0A\00"
@.str.5 = [28 x i8] c"Couldn't read certificate.\0A\00"
@.str.6 = [38 x i8] c"Couldn't convert to a RSA style key.\0A\00"
@.str.7 = [32 x i8] c"Couldn't read public key file.\0A\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*
%10 = alloca i32*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32* null, i32** %8
store i32* null, i32** %9
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %26, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %44
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %44
26: ; preds = %20, %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 0
%29 = load i8*, i8** %28
%30 = call i8* @strrchr(i8* %29, i8 signext 47)
store i8* %30, i8** %11
%31 = load i8*, i8** %11
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i8*, i8** %11
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %11
br label %40
36: ; preds = %26
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
store i8* %39, i8** %11
br label %40
40: ; preds = %36, %33
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %11
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %42)
store i32 -1, i32* %3
br label %107
44: ; preds = %20, %14
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%49 = icmp ne i64 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %44
store i32 1, i32* %6
br label %51
51: ; preds = %50, %44
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i32* @fopen(i8* %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %55, i32** %7
%56 = load i32*, i32** %7
%57 = icmp ne i32* %56, null
br i1 %57, label %64, label %58
58: ; preds = %51
%59 = load i32, i32* @stderr
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 2
%62 = load i8*, i8** %61
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %62)
store i32 -1, i32* %3
br label %107
64: ; preds = %51
%65 = load i32, i32* %6
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %84
67: ; preds = %64
%68 = load i32*, i32** %7
%69 = call i32 @PEM_read_X509(i32* %68, i32** %8, i32* null, i32* null)
%70 = icmp ne i32 %69, 0
br i1 %70, label %74, label %71
71: ; preds = %67
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %100
74: ; preds = %67
%75 = load i32*, i32** %8
%76 = call i32* @X509_get_pubkey(i32* %75)
store i32* %76, i32** %10
%77 = load i32*, i32** %10
%78 = call i32* @EVP_PKEY_get1_RSA(i32* %77)
store i32* %78, i32** %9
%79 = icmp ne i32* %78, null
br i1 %79, label %83, label %80
80: ; preds = %74
%81 = load i32, i32* @stderr
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %100
83: ; preds = %74
br label %92
84: ; preds = %64
%85 = load i32*, i32** %7
%86 = call i32* @PEM_read_RSA_PUBKEY(i32* %85, i32* null, i32* null, i32* null)
store i32* %86, i32** %9
%87 = icmp ne i32* %86, null
br i1 %87, label %91, label %88
88: ; preds = %84
%89 = load i32, i32* @stderr
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
br label %100
91: ; preds = %84
br label %92
92: ; preds = %91, %83
%93 = load i32*, i32** %9
%94 = call i64 @check(i32* %93)
%95 = icmp ne i64 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %92
%97 = load i32*, i32** %9
%98 = call i32 @output(i32* %97)
br label %99
99: ; preds = %96, %92
br label %100
100: ; preds = %99, %88, %80, %71
%101 = load i32*, i32** %8
%102 = call i32 @X509_free(i32* %101)
%103 = load i32*, i32** %9
%104 = call i32 @RSA_free(i32* %103)
%105 = load i32*, i32** %7
%106 = call i32 @fclose(i32* %105)
store i32 0, i32* %3
br label %107
107: ; preds = %100, %58, %40
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@.str = [6 x i8] c"-cert\00"
@.str.1 = [5 x i8] c"-pub\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Usage: %s <-cert | -pub> <file>\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [24 x i8] c"Couldn't open file %s!\0A\00"
@.str.5 = [28 x i8] c"Couldn't read certificate.\0A\00"
@.str.6 = [38 x i8] c"Couldn't convert to a RSA style key.\0A\00"
@.str.7 = [32 x i8] c"Couldn't read public key file.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%5 = icmp eq i32 %0, 3
br i1 %5, label %6, label %15
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %11, %2
%16 = load i8*, i8** %1
%17 = tail call i8* @strrchr(i8* dereferenceable(1) %16, i8 signext 47)
%18 = icmp eq i8* %17, null
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = select i1 %18, i8* %16, i8* %19
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %68
23: ; preds = %11, %6
%24 = load i8*, i8** %7
%25 = tail call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = tail call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %26
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %68
34: ; preds = %23
%35 = icmp eq i64 %25, 0
br i1 %35, label %36, label %50
36: ; preds = %34
%37 = call i32 @PEM_read_X509(i32* %28, i32** %3, i32* null, i32* null)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %62
42: ; preds = %36
%43 = load i32*, i32** %3
%44 = call i32* @X509_get_pubkey(i32* %43)
%45 = call i32* @EVP_PKEY_get1_RSA(i32* %44)
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %56
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %62
50: ; preds = %34
%51 = tail call i32* @PEM_read_RSA_PUBKEY(i32* %28, i32* null, i32* null, i32* null)
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = tail call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
br label %62
56: ; preds = %50, %42
%57 = phi i32* [ %45, %42 ], [ %51, %50 ]
%58 = call i64 @check(i32* %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %56
%61 = call i32 @output(i32* %57)
br label %62
62: ; preds = %56, %60, %53, %47, %39
%63 = phi i32* [ %57, %60 ], [ %57, %56 ], [ null, %47 ], [ null, %39 ], [ null, %53 ]
%64 = load i32*, i32** %3
%65 = call i32 @X509_free(i32* %64)
%66 = call i32 @RSA_free(i32* %63)
%67 = call i32 @fclose(i32* %28)
br label %68
68: ; preds = %62, %30, %15
%69 = phi i32 [ -1, %15 ], [ 0, %62 ], [ -1, %30 ]
ret i32 %69
}