#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;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_bn2bin(int /*<<< orphan*/ *, unsigned char *);
int BN_num_bytes(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int ntohl(unsigned long);
void print_mpi(FILE *fd, int idx, char const *name, BIGNUM *num) {
  int i, n = BN_num_bytes(num);
  unsigned char *buf = malloc(n);
  BN_bn2bin(num, buf);
  fprintf(fd, "static mpi_limb_t const %s%d[] = {", name, idx);
  for (i = 0; i < n; i += 4) {
    if (!(i % 32))
      fprintf(fd, "\n\t");
    fprintf(fd, "0x%08x, ", ntohl(*(unsigned long *)&buf[n - i - 4]));
  }
  fprintf(fd, "};\n");
  free(buf);
}


x86 assembly (clang, -O0):
<print_mpi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <BN_num_bytes@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <BN_bn2bin@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1231 <print_mpi+0xd1>
mov    -0x24(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11ee <print_mpi+0x8e>
mov    -0x8(%rbp),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    -0x24(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%rdi
call   1080 <ntohl@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
jmp    11bc <print_mpi+0x5c>
mov    -0x8(%rbp),%rdi
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_mpi>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r13
mov    %esi,%ebp
mov    %rdi,%r15
mov    %rcx,%rdi
call   1060 <BN_num_bytes@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    %rax,%rsi
call   1070 <BN_bn2bin@plt>
lea    0xe54(%rip),%rsi
mov    %r15,%rdi
mov    %r13,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %ebx,%ebx
jle    1218 <print_mpi+0xa8>
mov    %ebx,%ebp
add    $0xfffffffc,%ebx
lea    0xe5b(%rip),%r13
xor    %r12d,%r12d
jmp    11f7 <print_mpi+0x87>
movslq %ebx,%rbx
mov    (%r14,%rbx,1),%rdi
call   1090 <ntohl@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x4,%r12
add    $0xfffffffc,%ebx
cmp    %rbp,%r12
jge    1218 <print_mpi+0xa8>
test   $0x1c,%r12b
jne    11d0 <print_mpi+0x60>
mov    $0x2,%esi
mov    $0x1,%edx
lea    0xe15(%rip),%rdi
mov    %r15,%rcx
call   1080 <fwrite@plt>
jmp    11d0 <print_mpi+0x60>
lea    0xe10(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"static mpi_limb_t const %s%d[] = {\00"
@.str.1 = [3 x i8] c"\0A\09\00"
@.str.2 = [9 x i8] c"0x%08x, \00"
@.str.3 = [4 x i8] c"};\0A\00"
define void @print_mpi(i32* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32* %3, i32** %8
%12 = load i32*, i32** %8
%13 = call i32 @BN_num_bytes(i32* %12)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %11
%16 = load i32*, i32** %8
%17 = load i8*, i8** %11
%18 = call i32 @BN_bn2bin(i32* %16, i8* %17)
%19 = load i32*, i32** %5
%20 = load i8*, i8** %7
%21 = load i32, i32* %6
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
store i32 0, i32* %9
br label %23
23: ; preds = %47, %4
%24 = load i32, i32* %9
%25 = load i32, i32* %10
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %50
27: ; preds = %23
%28 = load i32, i32* %9
%29 = srem i32 %28, 32
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = load i32*, i32** %5
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %31, %27
%35 = load i32*, i32** %5
%36 = load i8*, i8** %11
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = sub nsw i32 %37, %38
%40 = sub nsw i32 %39, 4
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %36, i64 %41
%43 = bitcast i8* %42 to i64*
%44 = load i64, i64* %43
%45 = call i32 @ntohl(i64 %44)
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %34
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 4
store i32 %49, i32* %9
br label %23
50: ; preds = %23
%51 = load i32*, i32** %5
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %51, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%53 = load i8*, i8** %11
%54 = call i32 @free(i8* %53)
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"static mpi_limb_t const %s%d[] = {\00"
@.str.1 = [3 x i8] c"\0A\09\00"
@.str.2 = [9 x i8] c"0x%08x, \00"
@.str.3 = [4 x i8] c"};\0A\00"
define void @print_mpi(i32* nocapture %0, i32 %1, i8* %2, i32* %3) {
%5 = tail call i32 @BN_num_bytes(i32* %3)
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 @BN_bn2bin(i32* %3, i8* %6)
%8 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %1)
%9 = icmp sgt i32 %5, 0
br i1 %9, label %10, label %30
10: ; preds = %4
%11 = add nsw i32 %5, -4
%12 = zext i32 %5 to i64
br label %13
13: ; preds = %10, %20
%14 = phi i64 [ 0, %10 ], [ %28, %20 ]
%15 = trunc i64 %14 to i32
%16 = and i32 %15, 28
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 2, i64 1, i32* %0)
br label %20
20: ; preds = %18, %13
%21 = sub i32 %11, %15
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %6, i64 %22
%24 = bitcast i8* %23 to i64*
%25 = load i64, i64* %24
%26 = tail call i32 @ntohl(i64 %25)
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = add nuw i64 %14, 4
%29 = icmp slt i64 %28, %12
br i1 %29, label %13, label %30
30: ; preds = %20, %4
%31 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 3, i64 1, i32* %0)
%32 = tail call i32 @free(i8* %6)
ret void
}