#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*/ MP_INT;

/* Variables and functions */
int /*<<< orphan*/ mpz_add_ui(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                              unsigned char const);
int /*<<< orphan*/ mpz_mul_2exp(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/ *, int /*<<< orphan*/);
void mp_unlinearize_msb_first(MP_INT *value, const unsigned char *buf,
                              unsigned int len) {
  unsigned int i;
  mpz_set_ui(value, 0);
  for (i = 0; i + 4 <= len; i += 4) {
    unsigned long limb = (((unsigned long)(unsigned char)(buf + i)[0] << 24) |
                          ((unsigned long)(unsigned char)(buf + i)[1] << 16) |
                          ((unsigned long)(unsigned char)(buf + i)[2] << 8) |
                          ((unsigned long)(unsigned char)(buf + i)[3]));
    mpz_mul_2exp(value, value, 32);
    mpz_add_ui(value, value, limb);
  }
  for (; i < len; i++) {
    mpz_mul_2exp(value, value, 8);
    mpz_add_ui(value, value, buf[i]);
  }
}


x86 assembly (clang, -O0):
<mp_unlinearize_msb_first>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1050 <mpz_set_ui@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x4,%eax
cmp    -0x14(%rbp),%eax
ja     11e0 <mp_unlinearize_msb_first+0xb0>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
shl    $0x18,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
movzbl 0x1(%rcx,%rdx,1),%ecx
shl    $0x10,%rcx
or     %rcx,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
movzbl 0x2(%rcx,%rdx,1),%ecx
shl    $0x8,%rcx
or     %rcx,%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
movzbl 0x3(%rcx,%rdx,1),%ecx
or     %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x20,%edx
call   1040 <mpz_mul_2exp@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
movzbl %al,%edx
call   1030 <mpz_add_ui@plt>
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    1155 <mp_unlinearize_msb_first+0x25>
jmp    11e5 <mp_unlinearize_msb_first+0xb5>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1229 <mp_unlinearize_msb_first+0xf9>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x8,%edx
call   1040 <mpz_mul_2exp@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
call   1030 <mpz_add_ui@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11e5 <mp_unlinearize_msb_first+0xb5>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_unlinearize_msb_first>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r13
xor    %ebx,%ebx
xor    %esi,%esi
call   1050 <mpz_set_ui@plt>
cmp    $0x4,%r15d
jb     1193 <mp_unlinearize_msb_first+0x63>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%eax
movzbl 0x3(%r14,%rax,1),%r12d
mov    %r13,%rdi
mov    %r13,%rsi
mov    $0x20,%edx
call   1040 <mpz_mul_2exp@plt>
mov    %r13,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1030 <mpz_add_ui@plt>
lea    0x4(%rbp),%ebx
add    $0x8,%ebp
cmp    %r15d,%ebp
mov    %ebx,%ebp
jbe    1160 <mp_unlinearize_msb_first+0x30>
cmp    %r15d,%ebx
jae    11c9 <mp_unlinearize_msb_first+0x99>
mov    %ebx,%ebp
mov    %r15d,%r15d
nopl   (%rax)
mov    %r13,%rdi
mov    %r13,%rsi
mov    $0x8,%edx
call   1040 <mpz_mul_2exp@plt>
movzbl (%r14,%rbp,1),%edx
mov    %r13,%rdi
mov    %r13,%rsi
call   1030 <mpz_add_ui@plt>
add    $0x1,%rbp
cmp    %ebp,%r15d
jne    11a0 <mp_unlinearize_msb_first+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @mp_unlinearize_msb_first(i32* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @mpz_set_ui(i32* %0, i32 0)
%5 = icmp ult i32 %2, 4
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %1, i64 3
br label %13
8: ; preds = %13, %3
%9 = phi i32 [ 0, %3 ], [ %14, %13 ]
%10 = icmp ult i32 %9, %2
br i1 %10, label %11, label %32
11: ; preds = %8
%12 = zext i32 %9 to i64
br label %23
13: ; preds = %6, %13
%14 = phi i32 [ %21, %13 ], [ 4, %6 ]
%15 = phi i32 [ %14, %13 ], [ 0, %6 ]
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %7, i64 %16
%18 = load i8, i8* %17
%19 = tail call i32 @mpz_mul_2exp(i32* %0, i32* %0, i32 32)
%20 = tail call i32 @mpz_add_ui(i32* %0, i32* %0, i8 zeroext %18)
%21 = add i32 %14, 4
%22 = icmp ugt i32 %21, %2
br i1 %22, label %8, label %13
23: ; preds = %11, %23
%24 = phi i64 [ %12, %11 ], [ %29, %23 ]
%25 = tail call i32 @mpz_mul_2exp(i32* %0, i32* %0, i32 8)
%26 = getelementptr inbounds i8, i8* %1, i64 %24
%27 = load i8, i8* %26
%28 = tail call i32 @mpz_add_ui(i32* %0, i32* %0, i8 zeroext %27)
%29 = add nuw nsw i64 %24, 1
%30 = trunc i64 %29 to i32
%31 = icmp eq i32 %30, %2
br i1 %31, label %32, label %23
32: ; preds = %23, %8
ret void
}