#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 *mpz_t;

/* Variables and functions */
int crypt(int *, int *, int *, int *, size_t);
int /*<<< orphan*/ gmp_sscanf(char const *, char *, int *, ...);
scalar_t__ malloc(int);
int /*<<< orphan*/ mpz_clear(int *);
int /*<<< orphan*/ mpz_export(char *, size_t *, int, int, int /*<<< orphan*/,
                              int /*<<< orphan*/, int *);
int /*<<< orphan*/ mpz_init2(int *, size_t);
int strlen(char const *);
const char *decrypt_pub(const char *cipher_text, const char *public_key) {
  int len;
  gmp_sscanf(public_key, "-----RSA Public Key-----\nl: %d", &len);
  mpz_t n, e, message, cipher;
  mpz_init2(n, len);
  mpz_init2(e, len);
  gmp_sscanf(public_key,
             "-----RSA Public Key-----\nl: %d\nn: %Zx\ne: %Zx\n-----End-----\n",
             &len, n, e);
  size_t max_bit = (strlen(cipher_text) * 4 / len + 1) * len;
  mpz_init2(message, max_bit);
  mpz_init2(cipher, max_bit);
  gmp_sscanf(cipher_text, "%Zx", cipher);
  int round = crypt(n, e, cipher, message, max_bit);
  char *buf = (char *)malloc(sizeof(char) * (round * len / 8 + 1));
  size_t length;
  mpz_export(buf, &length, 1, sizeof(char), 0, 0, message);
  buf[length] = '\0';
  mpz_clear(n);
  mpz_clear(e);
  mpz_clear(message);
  mpz_clear(cipher);
  return buf;
}


x86 assembly (clang, -O0):
<decrypt_pub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1080 <gmp_sscanf@plt>
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rsi
call   1090 <mpz_init2@plt>
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rsi
call   1090 <mpz_init2@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe5c(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1080 <gmp_sscanf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
shl    $0x2,%eax
cltd
idivl  -0x14(%rbp)
add    $0x1,%eax
imul   -0x14(%rbp),%eax
cltq
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1090 <mpz_init2@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1090 <mpz_init2@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1080 <gmp_sscanf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1030 <crypt@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
imul   -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x30(%rbp),%rax
lea    -0x58(%rbp),%rsi
mov    $0x1,%ecx
xor    %r9d,%r9d
mov    %ecx,%edx
mov    %r9d,%r8d
mov    %rax,(%rsp)
call   1050 <mpz_export@plt>
mov    -0x50(%rbp),%rax
mov    -0x58(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1070 <mpz_clear@plt>
mov    -0x28(%rbp),%rdi
call   1070 <mpz_clear@plt>
mov    -0x30(%rbp),%rdi
call   1070 <mpz_clear@plt>
mov    -0x38(%rbp),%rdi
call   1070 <mpz_clear@plt>
mov    -0x50(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decrypt_pub>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe7a(%rip),%rsi
lea    0x14(%rsp),%r15
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <gmp_sscanf@plt>
movslq 0x14(%rsp),%rsi
call   1090 <mpz_init2@plt>
movslq 0x14(%rsp),%rsi
call   1090 <mpz_init2@plt>
lea    0xe6c(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <gmp_sscanf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
shl    $0x2,%eax
mov    0x14(%rsp),%ecx
cltd
idiv   %ecx
add    $0x1,%eax
imul   %ecx,%eax
movslq %eax,%rbx
mov    %rbx,%rsi
call   1090 <mpz_init2@plt>
mov    %rbx,%rsi
call   1090 <mpz_init2@plt>
lea    0xe69(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <gmp_sscanf@plt>
mov    %rbx,%r8
call   1030 <crypt@plt>
imul   0x14(%rsp),%eax
lea    0x7(%rax),%edi
test   %eax,%eax
cmovns %eax,%edi
sar    $0x3,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0x18(%rsp),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <mpz_export@plt>
mov    0x18(%rsp),%rax
movb   $0x0,(%rbx,%rax,1)
call   1070 <mpz_clear@plt>
call   1070 <mpz_clear@plt>
call   1070 <mpz_clear@plt>
call   1070 <mpz_clear@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [31 x i8] c"-----RSA Public Key-----\0Al: %d\00"
@.str.1 = [60 x i8] c"-----RSA Public Key-----\0Al: %d\0An: %Zx\0Ae: %Zx\0A-----End-----\0A\00"
@.str.2 = [4 x i8] c"%Zx\00"
define i8* @decrypt_pub(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%14 = load i8*, i8** %4
%15 = call i32 (i8*, i8*, i32*, ...) @gmp_sscanf(i8* %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32* %5)
%16 = load i32*, i32** %6
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = call i32 @mpz_init2(i32* %16, i64 %18)
%20 = load i32*, i32** %7
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = call i32 @mpz_init2(i32* %20, i64 %22)
%24 = load i8*, i8** %4
%25 = load i32*, i32** %6
%26 = load i32*, i32** %7
%27 = call i32 (i8*, i8*, i32*, ...) @gmp_sscanf(i8* %24, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32* %25, i32* %26)
%28 = load i8*, i8** %3
%29 = call i32 @strlen(i8* %28)
%30 = mul nsw i32 %29, 4
%31 = load i32, i32* %5
%32 = sdiv i32 %30, %31
%33 = add nsw i32 %32, 1
%34 = load i32, i32* %5
%35 = mul nsw i32 %33, %34
%36 = sext i32 %35 to i64
store i64 %36, i64* %10
%37 = load i32*, i32** %8
%38 = load i64, i64* %10
%39 = call i32 @mpz_init2(i32* %37, i64 %38)
%40 = load i32*, i32** %9
%41 = load i64, i64* %10
%42 = call i32 @mpz_init2(i32* %40, i64 %41)
%43 = load i8*, i8** %3
%44 = load i32*, i32** %9
%45 = call i32 (i8*, i8*, i32*, ...) @gmp_sscanf(i8* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %44)
%46 = load i32*, i32** %6
%47 = load i32*, i32** %7
%48 = load i32*, i32** %9
%49 = load i32*, i32** %8
%50 = load i64, i64* %10
%51 = call i32 @crypt(i32* %46, i32* %47, i32* %48, i32* %49, i64 %50)
store i32 %51, i32* %11
%52 = load i32, i32* %11
%53 = load i32, i32* %5
%54 = mul nsw i32 %52, %53
%55 = sdiv i32 %54, 8
%56 = add nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = mul i64 1, %57
%59 = trunc i64 %58 to i32
%60 = call i64 @malloc(i32 %59)
%61 = inttoptr i64 %60 to i8*
store i8* %61, i8** %12
%62 = load i8*, i8** %12
%63 = load i32*, i32** %8
%64 = call i32 @mpz_export(i8* %62, i64* %13, i32 1, i32 1, i32 0, i32 0, i32* %63)
%65 = load i8*, i8** %12
%66 = load i64, i64* %13
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8 0, i8* %67
%68 = load i32*, i32** %6
%69 = call i32 @mpz_clear(i32* %68)
%70 = load i32*, i32** %7
%71 = call i32 @mpz_clear(i32* %70)
%72 = load i32*, i32** %8
%73 = call i32 @mpz_clear(i32* %72)
%74 = load i32*, i32** %9
%75 = call i32 @mpz_clear(i32* %74)
%76 = load i8*, i8** %12
ret i8* %76
}

LLVM IR (-O2):
@.str = [31 x i8] c"-----RSA Public Key-----\0Al: %d\00"
@.str.1 = [60 x i8] c"-----RSA Public Key-----\0Al: %d\0An: %Zx\0Ae: %Zx\0A-----End-----\0A\00"
@.str.2 = [4 x i8] c"%Zx\00"
define i8* @decrypt_pub(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = bitcast i32* %3 to i8*
%6 = call i32 (i8*, i8*, i32*, ...) @gmp_sscanf(i8* %1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32* %3)
%7 = load i32, i32* %3
%8 = sext i32 %7 to i64
%9 = call i32 @mpz_init2(i32* undef, i64 %8)
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = call i32 @mpz_init2(i32* undef, i64 %11)
%13 = call i32 (i8*, i8*, i32*, ...) @gmp_sscanf(i8* %1, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* undef, i32* undef)
%14 = call i32 @strlen(i8* %0)
%15 = shl nsw i32 %14, 2
%16 = load i32, i32* %3
%17 = sdiv i32 %15, %16
%18 = add nsw i32 %17, 1
%19 = mul nsw i32 %18, %16
%20 = sext i32 %19 to i64
%21 = call i32 @mpz_init2(i32* undef, i64 %20)
%22 = call i32 @mpz_init2(i32* undef, i64 %20)
%23 = call i32 (i8*, i8*, i32*, ...) @gmp_sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* undef)
%24 = call i32 @crypt(i32* undef, i32* undef, i32* undef, i32* undef, i64 %20)
%25 = load i32, i32* %3
%26 = mul nsw i32 %25, %24
%27 = sdiv i32 %26, 8
%28 = add nsw i32 %27, 1
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
%31 = bitcast i64* %4 to i8*
%32 = call i32 @mpz_export(i8* %30, i64* %4, i32 1, i32 1, i32 0, i32 0, i32* undef)
%33 = load i64, i64* %4
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = call i32 @mpz_clear(i32* undef)
%36 = call i32 @mpz_clear(i32* undef)
%37 = call i32 @mpz_clear(i32* undef)
%38 = call i32 @mpz_clear(i32* undef)
ret i8* %30
}