#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 unsigned int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ compute_root(unsigned char *, unsigned char *, unsigned int,
                                unsigned int, unsigned char const *, int,
                                unsigned char const *, unsigned int *);
int /*<<< orphan*/ copy_keypair_addr(unsigned int *, unsigned int const *);
int /*<<< orphan*/ fors_sk_to_leaf(unsigned char *, unsigned char const *,
                                   unsigned char const *, unsigned int *);
int /*<<< orphan*/ message_to_indices(unsigned int *, unsigned char const *);
int /*<<< orphan*/ set_tree_height(unsigned int *, int /*<<< orphan*/);
int /*<<< orphan*/ set_tree_index(unsigned int *, unsigned int);
int /*<<< orphan*/ set_type(unsigned int *, int);
int /*<<< orphan*/ thash(unsigned char *, unsigned char *, int,
                         unsigned char const *, unsigned int *);
void fors_pk_from_sig(unsigned char *pk, const unsigned char *sig,
                      const unsigned char *m, const unsigned char *pub_seed,
                      const uint32_t fors_addr[8]) {
  uint32_t indices[30];
  unsigned char roots[30 * 16];
  unsigned char leaf[16];
  uint32_t fors_tree_addr[8] = {0};
  uint32_t fors_pk_addr[8] = {0};
  uint32_t idx_offset;
  unsigned int i;
  copy_keypair_addr(fors_tree_addr, fors_addr);
  copy_keypair_addr(fors_pk_addr, fors_addr);
  set_type(fors_tree_addr, 3);
  set_type(fors_pk_addr, 4);
  message_to_indices(indices, m);
  for (i = 0; i < 30; i++) {
    idx_offset = i * (1 << 9);
    set_tree_height(fors_tree_addr, 0);
    set_tree_index(fors_tree_addr, indices[i] + idx_offset);
    fors_sk_to_leaf(leaf, sig, pub_seed, fors_tree_addr);
    sig += 16;
    compute_root(roots + i * 16, leaf, indices[i], idx_offset, sig, 9, pub_seed,
                 fors_tree_addr);
    sig += 16 * 9;
  }
  thash(pk, roots, 30, pub_seed, fors_pk_addr);
}


x86 assembly (clang, -O0):
<fors_pk_from_sig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2f0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x2b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
lea    -0x2d0(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1070 <memset@plt>
lea    -0x2b0(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <copy_keypair_addr@plt>
lea    -0x2d0(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <copy_keypair_addr@plt>
lea    -0x2b0(%rbp),%rdi
mov    $0x3,%esi
call   1050 <set_type@plt>
lea    -0x2d0(%rbp),%rdi
mov    $0x4,%esi
call   1050 <set_type@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <message_to_indices@plt>
movl   $0x0,-0x2d8(%rbp)
cmpl   $0x1e,-0x2d8(%rbp)
jae    1315 <fors_pk_from_sig+0x185>
mov    -0x2d8(%rbp),%eax
shl    $0x9,%eax
mov    %eax,-0x2d4(%rbp)
lea    -0x2b0(%rbp),%rdi
xor    %esi,%esi
call   1040 <set_tree_height@plt>
lea    -0x2b0(%rbp),%rdi
mov    -0x2d8(%rbp),%eax
mov    -0xa0(%rbp,%rax,4),%esi
add    -0x2d4(%rbp),%esi
call   1080 <set_tree_index@plt>
lea    -0x290(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x2b0(%rbp),%rcx
call   10a0 <fors_sk_to_leaf@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
lea    -0x280(%rbp),%rdi
mov    -0x2d8(%rbp),%eax
shl    $0x4,%eax
mov    %eax,%eax
add    %rax,%rdi
lea    -0x290(%rbp),%rsi
mov    -0x2d8(%rbp),%eax
mov    -0xa0(%rbp,%rax,4),%edx
mov    -0x2d4(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    -0x20(%rbp),%r10
lea    -0x2b0(%rbp),%rax
mov    $0x9,%r9d
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1090 <compute_root@plt>
mov    -0x10(%rbp),%rax
add    $0x90,%rax
mov    %rax,-0x10(%rbp)
mov    -0x2d8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2d8(%rbp)
jmp    1231 <fors_pk_from_sig+0xa1>
mov    -0x8(%rbp),%rdi
lea    -0x280(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x2d0(%rbp),%r8
mov    $0x1e,%edx
call   1030 <thash@plt>
add    $0x2f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fors_pk_from_sig>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2c8,%rsp
mov    %r8,%rbx
mov    %rcx,(%rsp)
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r8,%rsi
call   1060 <copy_keypair_addr@plt>
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <copy_keypair_addr@plt>
mov    %rbp,%rdi
mov    $0x3,%esi
call   1050 <set_type@plt>
mov    %r15,%rdi
mov    $0x4,%esi
call   1050 <set_type@plt>
lea    0x60(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <message_to_indices@plt>
lea    0xe0(%rsp),%rbp
xor    %r14d,%r14d
mov    (%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <set_tree_height@plt>
mov    (%rbx),%esi
add    %r14d,%esi
mov    %r15,%rdi
call   1070 <set_tree_index@plt>
lea    0x50(%rsp),%r15
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
lea    0x10(%rsp),%rcx
call   1090 <fors_sk_to_leaf@plt>
mov    (%rbx),%edx
lea    0x10(%r13),%r8
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14d,%ecx
mov    $0x9,%r9d
lea    0x10(%rsp),%rax
push   %rax
push   %r12
call   1080 <compute_root@plt>
add    $0x10,%rsp
add    $0x200,%r14
add    $0x4,%rbx
add    $0x10,%rbp
add    $0xa0,%r13
cmp    $0x3c00,%r14
jne    1220 <fors_pk_from_sig+0xa0>
lea    0xe0(%rsp),%rsi
lea    0x30(%rsp),%r8
mov    0x8(%rsp),%rdi
mov    $0x1e,%edx
mov    (%rsp),%rcx
call   1030 <thash@plt>
add    $0x2c8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @fors_pk_from_sig(i8* %0, i8* %1, i8* %2, i8* %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca [30 x i32]
%12 = alloca [480 x i8]
%13 = alloca [16 x i8]
%14 = alloca [8 x i32]
%15 = alloca [8 x i32]
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32* %4, i32** %10
%18 = bitcast [8 x i32]* %14 to i8*
%19 = bitcast [8 x i32]* %15 to i8*
%20 = getelementptr inbounds [8 x i32], [8 x i32]* %14, i64 0, i64 0
%21 = load i32*, i32** %10
%22 = call i32 @copy_keypair_addr(i32* %20, i32* %21)
%23 = getelementptr inbounds [8 x i32], [8 x i32]* %15, i64 0, i64 0
%24 = load i32*, i32** %10
%25 = call i32 @copy_keypair_addr(i32* %23, i32* %24)
%26 = getelementptr inbounds [8 x i32], [8 x i32]* %14, i64 0, i64 0
%27 = call i32 @set_type(i32* %26, i32 3)
%28 = getelementptr inbounds [8 x i32], [8 x i32]* %15, i64 0, i64 0
%29 = call i32 @set_type(i32* %28, i32 4)
%30 = getelementptr inbounds [30 x i32], [30 x i32]* %11, i64 0, i64 0
%31 = load i8*, i8** %8
%32 = call i32 @message_to_indices(i32* %30, i8* %31)
store i32 0, i32* %17
br label %33
33: ; preds = %73, %5
%34 = load i32, i32* %17
%35 = icmp ult i32 %34, 30
br i1 %35, label %36, label %76
36: ; preds = %33
%37 = load i32, i32* %17
%38 = mul i32 %37, 512
store i32 %38, i32* %16
%39 = getelementptr inbounds [8 x i32], [8 x i32]* %14, i64 0, i64 0
%40 = call i32 @set_tree_height(i32* %39, i32 0)
%41 = getelementptr inbounds [8 x i32], [8 x i32]* %14, i64 0, i64 0
%42 = load i32, i32* %17
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds [30 x i32], [30 x i32]* %11, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = load i32, i32* %16
%47 = add i32 %45, %46
%48 = call i32 @set_tree_index(i32* %41, i32 %47)
%49 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%50 = load i8*, i8** %7
%51 = load i8*, i8** %9
%52 = getelementptr inbounds [8 x i32], [8 x i32]* %14, i64 0, i64 0
%53 = call i32 @fors_sk_to_leaf(i8* %49, i8* %50, i8* %51, i32* %52)
%54 = load i8*, i8** %7
%55 = getelementptr inbounds i8, i8* %54, i64 16
store i8* %55, i8** %7
%56 = getelementptr inbounds [480 x i8], [480 x i8]* %12, i64 0, i64 0
%57 = load i32, i32* %17
%58 = mul i32 %57, 16
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %56, i64 %59
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%62 = load i32, i32* %17
%63 = zext i32 %62 to i64
%64 = getelementptr inbounds [30 x i32], [30 x i32]* %11, i64 0, i64 %63
%65 = load i32, i32* %64
%66 = load i32, i32* %16
%67 = load i8*, i8** %7
%68 = load i8*, i8** %9
%69 = getelementptr inbounds [8 x i32], [8 x i32]* %14, i64 0, i64 0
%70 = call i32 @compute_root(i8* %60, i8* %61, i32 %65, i32 %66, i8* %67, i32 9, i8* %68, i32* %69)
%71 = load i8*, i8** %7
%72 = getelementptr inbounds i8, i8* %71, i64 144
store i8* %72, i8** %7
br label %73
73: ; preds = %36
%74 = load i32, i32* %17
%75 = add i32 %74, 1
store i32 %75, i32* %17
br label %33
76: ; preds = %33
%77 = load i8*, i8** %6
%78 = getelementptr inbounds [480 x i8], [480 x i8]* %12, i64 0, i64 0
%79 = load i8*, i8** %9
%80 = getelementptr inbounds [8 x i32], [8 x i32]* %15, i64 0, i64 0
%81 = call i32 @thash(i8* %77, i8* %78, i32 30, i8* %79, i32* %80)
ret void
}

LLVM IR (-O2):
define void @fors_pk_from_sig(i8* %0, i8* %1, i8* %2, i8* %3, i32* %4) {
%6 = alloca [30 x i32]
%7 = alloca [480 x i8]
%8 = alloca [16 x i8]
%9 = alloca [8 x i32]
%10 = alloca [8 x i32]
%11 = bitcast [30 x i32]* %6 to i8*
%12 = getelementptr inbounds [480 x i8], [480 x i8]* %7, i64 0, i64 0
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%14 = bitcast [8 x i32]* %9 to i8*
%15 = bitcast [8 x i32]* %10 to i8*
%16 = getelementptr inbounds [8 x i32], [8 x i32]* %9, i64 0, i64 0
%17 = call i32 @copy_keypair_addr(i32* %16, i32* %4)
%18 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 0
%19 = call i32 @copy_keypair_addr(i32* %18, i32* %4)
%20 = call i32 @set_type(i32* %16, i32 3)
%21 = call i32 @set_type(i32* %18, i32 4)
%22 = getelementptr inbounds [30 x i32], [30 x i32]* %6, i64 0, i64 0
%23 = call i32 @message_to_indices(i32* %22, i8* %2)
br label %24
24: ; preds = %5, %24
%25 = phi i64 [ 0, %5 ], [ %41, %24 ]
%26 = phi i8* [ %1, %5 ], [ %40, %24 ]
%27 = call i32 @set_tree_height(i32* %16, i32 0)
%28 = getelementptr inbounds [30 x i32], [30 x i32]* %6, i64 0, i64 %25
%29 = load i32, i32* %28
%30 = trunc i64 %25 to i32
%31 = shl i32 %30, 9
%32 = add i32 %29, %31
%33 = call i32 @set_tree_index(i32* %16, i32 %32)
%34 = call i32 @fors_sk_to_leaf(i8* %13, i8* %26, i8* %3, i32* %16)
%35 = getelementptr inbounds i8, i8* %26, i64 16
%36 = shl nuw nsw i64 %25, 4
%37 = getelementptr inbounds [480 x i8], [480 x i8]* %7, i64 0, i64 %36
%38 = load i32, i32* %28
%39 = call i32 @compute_root(i8* %37, i8* %13, i32 %38, i32 %31, i8* %35, i32 9, i8* %3, i32* %16)
%40 = getelementptr inbounds i8, i8* %26, i64 160
%41 = add nuw nsw i64 %25, 1
%42 = icmp eq i64 %41, 30
br i1 %42, label %43, label %24
43: ; preds = %24
%44 = call i32 @thash(i8* %0, i8* %12, i32 30, i8* %3, i32* %18)
ret void
}