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

/* Variables and functions */
int /*<<< orphan*/ BIO_free_all(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_read_filename(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ BIO_s_file();
int /*<<< orphan*/ *PEM_read_bio_RSAPublicKey(int /*<<< orphan*/ *,
                                              int /*<<< orphan*/ *,
                                              int /*<<< orphan*/ *,
                                              int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int update_key(void **, int, int /*<<< orphan*/ *, void **,
               int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  void *data_buf[1024 * 20] = {0};
  int buf_len = 0;
  FILE *buf_file = fopen("test_ota_1.pem", "rb");
  if (buf_file == NULL)
    printf("open test_ota_1.pem failed\n");
  buf_len = fread(data_buf, 1024 * 20, 1024 * 20, buf_file);
  printf("test_ota_1.pem length is %d", buf_len);
  fclose(buf_file);
  BIO *key = NULL;
  RSA *r = NULL;
  key = BIO_new(BIO_s_file());
  BIO_read_filename(key, "test_sb_rsa2048.pem");
  r = PEM_read_bio_RSAPublicKey(key, NULL, NULL, NULL);
  BIO_free_all(key);
  void *aes_key[256] = {0};
  FILE *aes_file = fopen("test_sb_aes256.bin", "rb");
  if (NULL == aes_file)
    printf("open test_sb_aes256.bin failed");
  int aes_len = fread(aes_key, 256, 256, aes_file);
  printf("aes key is %d", aes_len);
  fclose(aes_file);
  int ret = update_key(data_buf, buf_len, r, aes_key, NULL, 0);
  if (ret == -1)
    printf("update key failed");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x28840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x28010(%rbp),%rdi
xor    %esi,%esi
mov    $0x28000,%edx
call   1070 <memset@plt>
movl   $0x0,-0x28014(%rbp)
lea    0xe13(%rip),%rdi
lea    0xe1b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28020(%rbp)
cmpq   $0x0,-0x28020(%rbp)
jne    121c <main+0x6c>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x28010(%rbp),%rdi
mov    -0x28020(%rbp),%rcx
mov    $0x5000,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x28014(%rbp)
mov    -0x28014(%rbp),%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28020(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x28028(%rbp)
movq   $0x0,-0x28030(%rbp)
mov    $0x0,%al
call   10d0 <BIO_s_file@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x28028(%rbp)
mov    -0x28028(%rbp),%rdi
lea    0xdb5(%rip),%rsi
call   1090 <BIO_read_filename@plt>
mov    -0x28028(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10c0 <PEM_read_bio_RSAPublicKey@plt>
mov    %rax,-0x28030(%rbp)
mov    -0x28028(%rbp),%rdi
call   10b0 <BIO_free_all@plt>
lea    -0x28830(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
lea    0xd81(%rip),%rdi
lea    0xd2b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28838(%rbp)
xor    %eax,%eax
cmp    -0x28838(%rbp),%rax
jne    130d <main+0x15d>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x28830(%rbp),%rdi
mov    -0x28838(%rbp),%rcx
mov    $0x100,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x2883c(%rbp)
mov    -0x2883c(%rbp),%esi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28838(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x28010(%rbp),%rdi
mov    -0x28014(%rbp),%esi
mov    -0x28030(%rbp),%rdx
lea    -0x28830(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <update_key@plt>
mov    %eax,-0x28840(%rbp)
cmpl   $0xffffffff,-0x28840(%rbp)
jne    1396 <main+0x1e6>
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x28840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28808,%rsp
lea    0x800(%rsp),%rdi
mov    $0x28000,%edx
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xe18(%rip),%rdi
lea    0xe20(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1208 <main+0x48>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
lea    0x800(%rsp),%rdi
mov    $0x5000,%esi
mov    $0x5000,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
lea    0xde6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
call   10e0 <BIO_s_file@plt>
mov    %eax,%edi
call   1090 <BIO_new@plt>
mov    %rax,%rbx
lea    0xdd9(%rip),%rsi
mov    %rax,%rdi
call   10a0 <BIO_read_filename@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10d0 <PEM_read_bio_RSAPublicKey@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   10c0 <BIO_free_all@plt>
mov    %rsp,%rdi
mov    $0x800,%edx
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xdb6(%rip),%rdi
lea    0xd7c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12ae <main+0xee>
lea    0xdae(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    $0x100,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
lea    0xda7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0x800(%rsp),%rdi
mov    %r15d,%esi
mov    %r14,%rdx
mov    %rbp,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <update_key@plt>
cmp    $0xffffffff,%eax
jne    130d <main+0x14d>
lea    0xd7c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x28808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"test_ota_1.pem\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [28 x i8] c"open test_ota_1.pem failed\0A\00"
@.str.3 = [28 x i8] c"test_ota_1.pem length is %d\00"
@.str.4 = [20 x i8] c"test_sb_rsa2048.pem\00"
@.str.5 = [19 x i8] c"test_sb_aes256.bin\00"
@.str.6 = [31 x i8] c"open test_sb_aes256.bin failed\00"
@.str.7 = [14 x i8] c"aes key is %d\00"
@.str.8 = [18 x i8] c"update key failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [20480 x i8*]
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca [256 x i8*]
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = bitcast [20480 x i8*]* %6 to i8*
store i32 0, i32* %7
%16 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = getelementptr inbounds [20480 x i8*], [20480 x i8*]* %6, i64 0, i64 0
%23 = load i32*, i32** %8
%24 = call i32 @fread(i8** %22, i32 20480, i32 20480, i32* %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
store i32* null, i32** %9
store i32* null, i32** %10
%29 = call i32 (...) @BIO_s_file()
%30 = call i32* @BIO_new(i32 %29)
store i32* %30, i32** %9
%31 = load i32*, i32** %9
%32 = call i32 @BIO_read_filename(i32* %31, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%33 = load i32*, i32** %9
%34 = call i32* @PEM_read_bio_RSAPublicKey(i32* %33, i32* null, i32* null, i32* null)
store i32* %34, i32** %10
%35 = load i32*, i32** %9
%36 = call i32 @BIO_free_all(i32* %35)
%37 = bitcast [256 x i8*]* %11 to i8*
%38 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %38, i32** %12
%39 = load i32*, i32** %12
%40 = icmp eq i32* null, %39
br i1 %40, label %41, label %43
41: ; preds = %21
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %21
%44 = getelementptr inbounds [256 x i8*], [256 x i8*]* %11, i64 0, i64 0
%45 = load i32*, i32** %12
%46 = call i32 @fread(i8** %44, i32 256, i32 256, i32* %45)
store i32 %46, i32* %13
%47 = load i32, i32* %13
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %47)
%49 = load i32*, i32** %12
%50 = call i32 @fclose(i32* %49)
%51 = getelementptr inbounds [20480 x i8*], [20480 x i8*]* %6, i64 0, i64 0
%52 = load i32, i32* %7
%53 = load i32*, i32** %10
%54 = getelementptr inbounds [256 x i8*], [256 x i8*]* %11, i64 0, i64 0
%55 = call i32 @update_key(i8** %51, i32 %52, i32* %53, i8** %54, i32* null, i32 0)
store i32 %55, i32* %14
%56 = load i32, i32* %14
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %43
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %60
60: ; preds = %58, %43
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"test_ota_1.pem\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.3 = [28 x i8] c"test_ota_1.pem length is %d\00"
@.str.4 = [20 x i8] c"test_sb_rsa2048.pem\00"
@.str.5 = [19 x i8] c"test_sb_aes256.bin\00"
@.str.6 = [31 x i8] c"open test_sb_aes256.bin failed\00"
@.str.7 = [14 x i8] c"aes key is %d\00"
@.str.8 = [18 x i8] c"update key failed\00"
@str = [27 x i8] c"open test_ota_1.pem failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [20480 x i8*]
%4 = alloca [256 x i8*]
%5 = bitcast [20480 x i8*]* %3 to i8*
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds [20480 x i8*], [20480 x i8*]* %3, i64 0, i64 0
%12 = call i32 @fread(i8** %11, i32 20480, i32 20480, i32* %6)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = tail call i32 @fclose(i32* %6)
%15 = tail call i32 (...) @BIO_s_file()
%16 = tail call i32* @BIO_new(i32 %15)
%17 = tail call i32 @BIO_read_filename(i32* %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%18 = tail call i32* @PEM_read_bio_RSAPublicKey(i32* %16, i32* null, i32* null, i32* null)
%19 = tail call i32 @BIO_free_all(i32* %16)
%20 = bitcast [256 x i8*]* %4 to i8*
%21 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %10
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %25
25: ; preds = %23, %10
%26 = getelementptr inbounds [256 x i8*], [256 x i8*]* %4, i64 0, i64 0
%27 = call i32 @fread(i8** %26, i32 256, i32 256, i32* %21)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %27)
%29 = tail call i32 @fclose(i32* %21)
%30 = call i32 @update_key(i8** %11, i32 %12, i32* %18, i8** %26, i32* null, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %34
34: ; preds = %32, %25
ret i32 0
}