#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_clear(int /*<<< orphan*/ *);
int /*<<< orphan*/ mpz_div_2exp(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int);
void *mpz_get_ui(int /*<<< orphan*/ *);
int /*<<< orphan*/ mpz_init_set(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void mp_linearize_msb_first(unsigned char *buf, unsigned int len,
                            MP_INT *value) {
  unsigned int i;
  MP_INT aux;
  mpz_init_set(&aux, value);
  for (i = len; i >= 4; i -= 4) {
    unsigned long limb = mpz_get_ui(&aux);
    do {
      (buf + i - 4)[0] = (limb) >> 24;
      (buf + i - 4)[1] = (limb) >> 16;
      (buf + i - 4)[2] = (limb) >> 8;
      (buf + i - 4)[3] = (limb);
    } while (0);
    mpz_div_2exp(&aux, &aux, 32);
  }
  for (; i > 0; i--) {
    buf[i - 1] = mpz_get_ui(&aux);
    mpz_div_2exp(&aux, &aux, 8);
  }
  mpz_clear(&aux);
}


x86 assembly (clang, -O0):
<mp_linearize_msb_first>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1060 <mpz_init_set@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jb     11ec <mp_linearize_msb_first+0xac>
lea    -0x20(%rbp),%rdi
call   1040 <mpz_get_ui@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x18,%rax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,-0x4(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
shr    $0x10,%rax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,-0x3(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
shr    $0x8,%rax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,-0x2(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,-0x1(%rax,%rcx,1)
lea    -0x20(%rbp),%rsi
mov    $0x20,%edx
mov    %rsi,%rdi
call   1030 <mpz_div_2exp@plt>
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <mp_linearize_msb_first+0x26>
jmp    11f1 <mp_linearize_msb_first+0xb1>
cmpl   $0x0,-0x1c(%rbp)
jbe    1234 <mp_linearize_msb_first+0xf4>
lea    -0x20(%rbp),%rdi
call   1040 <mpz_get_ui@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
mov    %rsi,%rdi
call   1030 <mpz_div_2exp@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f1 <mp_linearize_msb_first+0xb1>
lea    -0x20(%rbp),%rdi
call   1050 <mpz_clear@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_linearize_msb_first>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
mov    %rdx,%rsi
call   1060 <mpz_init_set@plt>
cmp    $0x4,%ebx
jb     119b <mp_linearize_msb_first+0x5b>
mov    %ebx,%ebx
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <mpz_get_ui@plt>
bswap  %eax
mov    %eax,-0x4(%r14,%rbx,1)
mov    %r15,%rdi
mov    %r15,%rsi
mov    $0x20,%edx
call   1030 <mpz_div_2exp@plt>
add    $0xfffffffffffffffc,%rbx
test   $0xfffffffc,%ebx
jne    1170 <mp_linearize_msb_first+0x30>
test   %ebx,%ebx
je     11d3 <mp_linearize_msb_first+0x93>
mov    %ebx,%ebx
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <mpz_get_ui@plt>
mov    %al,-0x1(%r14,%rbx,1)
mov    %r15,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <mpz_div_2exp@plt>
add    $0xffffffffffffffff,%rbx
jne    11b0 <mp_linearize_msb_first+0x70>
lea    0xc(%rsp),%rdi
call   1050 <mpz_clear@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @mp_linearize_msb_first(i8* nocapture writeonly %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @mpz_init_set(i32* %4, i32* %2)
%7 = icmp ugt i32 %1, 3
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = zext i32 %1 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 -4
br label %18
11: ; preds = %18
%12 = trunc i64 %34 to i32
br label %13
13: ; preds = %11, %3
%14 = phi i32 [ %1, %3 ], [ %12, %11 ]
%15 = icmp eq i32 %14, 0
br i1 %15, label %46, label %16
16: ; preds = %13
%17 = zext i32 %14 to i64
br label %37
18: ; preds = %8, %18
%19 = phi i64 [ %9, %8 ], [ %34, %18 ]
%20 = call i8* @mpz_get_ui(i32* %4)
%21 = ptrtoint i8* %20 to i64
%22 = lshr i64 %21, 24
%23 = trunc i64 %22 to i8
%24 = getelementptr inbounds i8, i8* %10, i64 %19
store i8 %23, i8* %24
%25 = lshr i64 %21, 16
%26 = trunc i64 %25 to i8
%27 = getelementptr inbounds i8, i8* %24, i64 1
store i8 %26, i8* %27
%28 = lshr i64 %21, 8
%29 = trunc i64 %28 to i8
%30 = getelementptr inbounds i8, i8* %24, i64 2
store i8 %29, i8* %30
%31 = trunc i64 %21 to i8
%32 = getelementptr inbounds i8, i8* %24, i64 3
store i8 %31, i8* %32
%33 = call i32 @mpz_div_2exp(i32* %4, i32* %4, i32 32)
%34 = add nsw i64 %19, -4
%35 = and i64 %34, 4294967292
%36 = icmp eq i64 %35, 0
br i1 %36, label %11, label %18
37: ; preds = %16, %37
%38 = phi i64 [ %17, %16 ], [ %42, %37 ]
%39 = call i8* @mpz_get_ui(i32* %4)
%40 = ptrtoint i8* %39 to i64
%41 = trunc i64 %40 to i8
%42 = add nsw i64 %38, -1
%43 = getelementptr inbounds i8, i8* %0, i64 %42
store i8 %41, i8* %43
%44 = call i32 @mpz_div_2exp(i32* %4, i32* %4, i32 8)
%45 = icmp eq i64 %42, 0
br i1 %45, label %46, label %37
46: ; preds = %37, %13
%47 = call i32 @mpz_clear(i32* %4)
ret void
}