#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *malloc(int);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
size_t strcspn(char *, char *);
char *rsa_encrypt_msg(const char *pub_key_path, const char *msg) {
  char cmd[(512)];
  char *enc_msg = malloc((1024) * sizeof(char));
  sprintf(cmd,
          "echo \"%s\" | openssl rsautl -encrypt -pubin "
          "-inkey %s | openssl enc -base64 -A",
          msg, pub_key_path);
  FILE *enc_msg_stream = popen((const char *)cmd, "r");
  if (fgets(enc_msg, (1024), enc_msg_stream) != NULL) {
    enc_msg[strcspn(enc_msg, "\n")] = '\0';
  }
  if (pclose(enc_msg_stream) != 0) {
    fprintf(stderr,
            "[ERROR] Could not encrypt the message using the "
            "public rsa key: %s\n[ERROR] Exiting\n",
            pub_key_path);
    exit(-1);
  }
  return enc_msg;
}


x86 assembly (clang, -O0):
<rsa_encrypt_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x218(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe46(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x210(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x220(%rbp)
mov    -0x218(%rbp),%rdi
mov    -0x220(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     122a <rsa_encrypt_msg+0xaa>
mov    -0x218(%rbp),%rax
mov    %rax,-0x228(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1040 <strcspn@plt>
mov    %rax,%rcx
mov    -0x228(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x220(%rbp),%rdi
call   1030 <pclose@plt>
cmp    $0x0,%rax
je     1263 <rsa_encrypt_msg+0xe3>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdfc(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x218(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_encrypt_msg>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
lea    0xe58(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1090 <sprintf@plt>
lea    0xe8e(%rip),%rsi
mov    %r12,%rdi
call   1080 <popen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11f4 <rsa_encrypt_msg+0x74>
lea    0xebb(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcspn@plt>
movb   $0x0,(%r15,%rax,1)
mov    %rbx,%rdi
call   1030 <pclose@plt>
test   %rax,%rax
jne    1213 <rsa_encrypt_msg+0x93>
mov    %r15,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [78 x i8] c"echo \22%s\22 | openssl rsautl -encrypt -pubin -inkey %s | openssl enc -base64 -A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.3 = [84 x i8] c"[ERROR] Could not encrypt the message using the public rsa key: %s\0A[ERROR] Exiting\0A\00"
define i8* @rsa_encrypt_msg(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = call i8* @malloc(i32 1024)
store i8* %8, i8** %6
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = load i8*, i8** %3
%12 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %11)
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%14 = call i32* @popen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i8*, i8** %6
%16 = load i32*, i32** %7
%17 = call i32* @fgets(i8* %15, i32 1024, i32* %16)
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8*, i8** %6
%21 = load i8*, i8** %6
%22 = call i64 @strcspn(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %19, %2
%25 = load i32*, i32** %7
%26 = call i64 @pclose(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %3
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %24
%34 = load i8*, i8** %6
ret i8* %34
}

LLVM IR (-O2):
@.str = [78 x i8] c"echo \22%s\22 | openssl rsautl -encrypt -pubin -inkey %s | openssl enc -base64 -A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.3 = [84 x i8] c"[ERROR] Could not encrypt the message using the public rsa key: %s\0A[ERROR] Exiting\0A\00"
define i8* @rsa_encrypt_msg(i8* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%7 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32* @fgets(i8* %5, i32 1024, i32* %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i64 @strcspn(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = getelementptr inbounds i8, i8* %5, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %10, %2
%14 = tail call i64 @pclose(i32* %7)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
ret i8* %5
}