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

/* Variables and functions */
int /*<<< orphan*/ NID_sha1;
int /*<<< orphan*/ *PEM_read_RSA_PUBKEY(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ **,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_new();
int RSA_verify(int /*<<< orphan*/, unsigned char *, int, unsigned char *, int,
               int /*<<< orphan*/ *);
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ *fopen(char *, char *);
int read_file(char *, unsigned char *, int);
scalar_t__ sha1(unsigned char *, int, unsigned char *);
int rsa_verify_file(char *message_file_name, char *key_file_name,
                    char *signature_file_name) {
  FILE *key = fopen(key_file_name, "rb");
  if (key == NULL)
    return -1;
  RSA *rsa = RSA_new();
  rsa = PEM_read_RSA_PUBKEY(key, &rsa, NULL, NULL);
  unsigned char buf_signature[16384];
  int bytes_signature =
      read_file(signature_file_name, buf_signature, sizeof(buf_signature));
  if (bytes_signature == 0)
    return -2;
  unsigned char buf_message[16384];
  int bytes_message =
      read_file(message_file_name, buf_message, sizeof(buf_message));
  if (bytes_message == 0)
    return -3;
  unsigned char buf_digest[SHA_DIGEST_LENGTH];
  if (sha1(buf_message, bytes_message, buf_digest) != 0)
    return -4;
  int result = RSA_verify(NID_sha1, buf_digest, sizeof(buf_digest),
                          buf_signature, bytes_signature, rsa);
  return result;
}


x86 assembly (clang, -O0):
<rsa_verify_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8070,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a2 <rsa_verify_file+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ee <rsa_verify_file+0x18e>
mov    $0x0,%al
call   1060 <RSA_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <PEM_read_RSA_PUBKEY@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x4030(%rbp),%rsi
mov    $0x4000,%edx
call   1040 <read_file@plt>
mov    %eax,-0x4034(%rbp)
cmpl   $0x0,-0x4034(%rbp)
jne    11f9 <rsa_verify_file+0x99>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12ee <rsa_verify_file+0x18e>
mov    -0x10(%rbp),%rdi
lea    -0x8040(%rbp),%rsi
mov    $0x4000,%edx
call   1040 <read_file@plt>
mov    %eax,-0x8044(%rbp)
cmpl   $0x0,-0x8044(%rbp)
jne    122d <rsa_verify_file+0xcd>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12ee <rsa_verify_file+0x18e>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x8070(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x8050(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x8068(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x8058(%rbp)
lea    -0x8040(%rbp),%rdi
mov    -0x8044(%rbp),%esi
call   1030 <sha1@plt>
cmp    $0x0,%rax
je     129b <rsa_verify_file+0x13b>
movl   $0xfffffffc,-0x4(%rbp)
movl   $0x1,-0x805c(%rbp)
jmp    12e4 <rsa_verify_file+0x184>
mov    -0x8068(%rbp),%rsi
mov    -0x8070(%rbp),%rax
mov    0x2d28(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
lea    -0x4030(%rbp),%rcx
mov    -0x4034(%rbp),%r8d
mov    -0x30(%rbp),%r9
call   1050 <RSA_verify@plt>
mov    %eax,-0x8060(%rbp)
mov    -0x8060(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x805c(%rbp)
mov    -0x8050(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_verify_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x8010,%rsp
mov    %rdx,%r14
mov    %rsi,%rax
mov    %rdi,%r15
lea    0xe7e(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     124f <rsa_verify_file+0xef>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <RSA_new@plt>
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <PEM_read_RSA_PUBKEY@plt>
mov    %rax,-0x28(%rbp)
lea    -0x8030(%rbp),%rsi
mov    %r14,%rdi
mov    $0x4000,%edx
call   1040 <read_file@plt>
test   %eax,%eax
je     1256 <rsa_verify_file+0xf6>
mov    %eax,%r14d
lea    -0x4030(%rbp),%rsi
mov    %r15,%rdi
mov    $0x4000,%edx
call   1040 <read_file@plt>
test   %eax,%eax
je     125d <rsa_verify_file+0xfd>
mov    %rsp,%r12
mov    0x2df2(%rip),%rcx
mov    (%rcx),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
lea    -0x4030(%rbp),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1030 <sha1@plt>
mov    %rax,%rcx
mov    $0xfffffffc,%eax
test   %rcx,%rcx
jne    124a <rsa_verify_file+0xea>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%r9
lea    -0x8030(%rbp),%rcx
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14d,%r8d
call   1050 <RSA_verify@plt>
mov    %r12,%rsp
jmp    1262 <rsa_verify_file+0x102>
mov    $0xffffffff,%eax
jmp    1262 <rsa_verify_file+0x102>
mov    $0xfffffffe,%eax
jmp    1262 <rsa_verify_file+0x102>
mov    $0xfffffffd,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SHA_DIGEST_LENGTH = global i32 0
@NID_sha1 = global i32 0
define i32 @rsa_verify_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [16384 x i8]
%11 = alloca i32
%12 = alloca [16384 x i8]
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%18 = load i8*, i8** %6
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %3
store i32 -1, i32* %4
br label %60
23: ; preds = %3
%24 = call i32* (...) @RSA_new()
store i32* %24, i32** %9
%25 = load i32*, i32** %8
%26 = call i32* @PEM_read_RSA_PUBKEY(i32* %25, i32** %9, i32* null, i32* null)
store i32* %26, i32** %9
%27 = load i8*, i8** %7
%28 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%29 = call i32 @read_file(i8* %27, i8* %28, i32 16384)
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %23
store i32 -2, i32* %4
br label %60
33: ; preds = %23
%34 = load i8*, i8** %5
%35 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%36 = call i32 @read_file(i8* %34, i8* %35, i32 16384)
store i32 %36, i32* %13
%37 = load i32, i32* %13
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -3, i32* %4
br label %60
40: ; preds = %33
%41 = load i32, i32* @SHA_DIGEST_LENGTH
%42 = zext i32 %41 to i64
%43 = call i8* @llvm.stacksave()
store i8* %43, i8** %14
%44 = alloca i8, i64 %42
store i64 %42, i64* %15
%45 = getelementptr inbounds [16384 x i8], [16384 x i8]* %12, i64 0, i64 0
%46 = load i32, i32* %13
%47 = call i64 @sha1(i8* %45, i32 %46, i8* %44)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %40
store i32 -4, i32* %4
store i32 1, i32* %16
br label %58
50: ; preds = %40
%51 = load i32, i32* @NID_sha1
%52 = trunc i64 %42 to i32
%53 = getelementptr inbounds [16384 x i8], [16384 x i8]* %10, i64 0, i64 0
%54 = load i32, i32* %11
%55 = load i32*, i32** %9
%56 = call i32 @RSA_verify(i32 %51, i8* %44, i32 %52, i8* %53, i32 %54, i32* %55)
store i32 %56, i32* %17
%57 = load i32, i32* %17
store i32 %57, i32* %4
store i32 1, i32* %16
br label %58
58: ; preds = %50, %49
%59 = load i8*, i8** %14
br label %60
60: ; preds = %58, %39, %32, %22
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SHA_DIGEST_LENGTH = global i32 0
@NID_sha1 = global i32 0
define i32 @rsa_verify_file(i8* %0, i8* nocapture %1, i8* %2) {
%4 = alloca i32*
%5 = alloca [16384 x i8]
%6 = alloca [16384 x i8]
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %37, label %9
9: ; preds = %3
%10 = bitcast i32** %4 to i8*
%11 = tail call i32* (...) @RSA_new()
store i32* %11, i32** %4
%12 = call i32* @PEM_read_RSA_PUBKEY(i32* %7, i32** %4, i32* null, i32* null)
store i32* %12, i32** %4
%13 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%14 = call i32 @read_file(i8* %2, i8* %13, i32 16384)
%15 = icmp eq i32 %14, 0
br i1 %15, label %35, label %16
16: ; preds = %9
%17 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%18 = call i32 @read_file(i8* %0, i8* %17, i32 16384)
%19 = icmp eq i32 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %16
%21 = load i32, i32* @SHA_DIGEST_LENGTH
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
%24 = alloca i8, i64 %22
%25 = call i64 @sha1(i8* %17, i32 %18, i8* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i32, i32* @NID_sha1
%29 = load i32*, i32** %4
%30 = call i32 @RSA_verify(i32 %28, i8* %24, i32 %21, i8* %13, i32 %14, i32* %29)
br label %31
31: ; preds = %20, %27
%32 = phi i32 [ %30, %27 ], [ -4, %20 ]
br label %33
33: ; preds = %16, %31
%34 = phi i32 [ %32, %31 ], [ -3, %16 ]
br label %35
35: ; preds = %9, %33
%36 = phi i32 [ %34, %33 ], [ -2, %9 ]
br label %37
37: ; preds = %3, %35
%38 = phi i32 [ %36, %35 ], [ -1, %3 ]
ret i32 %38
}