#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 uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ src;

/* Variables and functions */
int /*<<< orphan*/ abort();
void _nettle_write_be32(size_t length, uint8_t *dst, const uint32_t *src) {
  size_t i;
  size_t words;
  unsigned leftover;
  words = length / 4;
  leftover = length % 4;
  for (i = 0; i < words; i++, dst += 4)
    do {
      (dst)[0] = ((src[i]) >> 24) & 0xff;
      (dst)[1] = ((src[i]) >> 16) & 0xff;
      (dst)[2] = ((src[i]) >> 8) & 0xff;
      (dst)[3] = (src[i]) & 0xff;
    } while (0);
  if (leftover) {
    uint32_t word;
    unsigned j = leftover;
    word = src[i];
    switch (leftover) {
    default:
      abort();
    case 3:
      dst[--j] = (word >> 8) & 0xff;
    case 2:
      dst[--j] = (word >> 16) & 0xff;
    case 1:
      dst[--j] = (word >> 24) & 0xff;
    }
  }
}


x86 assembly (clang, -O0):
<_nettle_write_be32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
and    $0x3,%rax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11e0 <_nettle_write_be32+0xd0>
jmp    1156 <_nettle_write_be32+0x46>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
and    $0xff,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
jmp    11c3 <_nettle_write_be32+0xb3>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
jmp    1143 <_nettle_write_be32+0x33>
cmpl   $0x0,-0x2c(%rbp)
je     129a <_nettle_write_be32+0x18a>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x1,%eax
je     1277 <_nettle_write_be32+0x167>
jmp    1212 <_nettle_write_be32+0x102>
mov    -0x38(%rbp),%eax
sub    $0x2,%eax
je     1259 <_nettle_write_be32+0x149>
jmp    1223 <_nettle_write_be32+0x113>
mov    -0x38(%rbp),%eax
sub    $0x3,%eax
je     123b <_nettle_write_be32+0x12b>
jmp    1234 <_nettle_write_be32+0x124>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%edx
sar    $0x8,%edx
and    $0xff,%edx
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x34(%rbp)
mov    %ecx,%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%edx
sar    $0x10,%edx
and    $0xff,%edx
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x34(%rbp)
mov    %ecx,%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%edx
sar    $0x18,%edx
and    $0xff,%edx
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x34(%rbp)
mov    %ecx,%ecx
mov    %edx,(%rax,%rcx,4)
jmp    129a <_nettle_write_be32+0x18a>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nettle_write_be32>:
push   %rax
mov    %edi,%r8d
and    $0x3,%r8d
cmp    $0x4,%rdi
jae    1122 <_nettle_write_be32+0x12>
xor    %edi,%edi
jmp    115b <_nettle_write_be32+0x4b>
shr    $0x2,%rdi
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
movzbl 0x3(%rdx,%rcx,4),%eax
mov    %eax,(%rsi)
movzbl 0x2(%rdx,%rcx,4),%eax
mov    %eax,0x4(%rsi)
movzbl 0x1(%rdx,%rcx,4),%eax
mov    %eax,0x8(%rsi)
movzbl (%rdx,%rcx,4),%eax
mov    %eax,0xc(%rsi)
add    $0x1,%rcx
add    $0x10,%rsi
cmp    %rcx,%rdi
jne    1130 <_nettle_write_be32+0x20>
test   %r8d,%r8d
je     118b <_nettle_write_be32+0x7b>
mov    (%rdx,%rdi,4),%ecx
cmp    $0x1,%r8d
je     1186 <_nettle_write_be32+0x76>
cmp    $0x2,%r8d
je     117b <_nettle_write_be32+0x6b>
cmp    $0x3,%r8d
jne    118d <_nettle_write_be32+0x7d>
movzbl %ch,%eax
mov    %eax,0x8(%rsi)
mov    %ecx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x4(%rsi)
shr    $0x18,%ecx
mov    %ecx,(%rsi)
pop    %rax
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
define void @_nettle_write_be32(i64 %0, i32* %1, i32* %2) {
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i64 %0, i64* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%12 = load i64, i64* %4
%13 = udiv i64 %12, 4
store i64 %13, i64* %8
%14 = load i64, i64* %4
%15 = urem i64 %14, 4
%16 = trunc i64 %15 to i32
store i32 %16, i32* %9
store i64 0, i64* %7
br label %17
17: ; preds = %55, %3
%18 = load i64, i64* %7
%19 = load i64, i64* %8
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %60
21: ; preds = %17
br label %22
22: ; preds = %21
%23 = load i32*, i32** %6
%24 = load i64, i64* %7
%25 = getelementptr inbounds i32, i32* %23, i64 %24
%26 = load i32, i32* %25
%27 = ashr i32 %26, 24
%28 = and i32 %27, 255
%29 = load i32*, i32** %5
%30 = getelementptr inbounds i32, i32* %29, i64 0
store i32 %28, i32* %30
%31 = load i32*, i32** %6
%32 = load i64, i64* %7
%33 = getelementptr inbounds i32, i32* %31, i64 %32
%34 = load i32, i32* %33
%35 = ashr i32 %34, 16
%36 = and i32 %35, 255
%37 = load i32*, i32** %5
%38 = getelementptr inbounds i32, i32* %37, i64 1
store i32 %36, i32* %38
%39 = load i32*, i32** %6
%40 = load i64, i64* %7
%41 = getelementptr inbounds i32, i32* %39, i64 %40
%42 = load i32, i32* %41
%43 = ashr i32 %42, 8
%44 = and i32 %43, 255
%45 = load i32*, i32** %5
%46 = getelementptr inbounds i32, i32* %45, i64 2
store i32 %44, i32* %46
%47 = load i32*, i32** %6
%48 = load i64, i64* %7
%49 = getelementptr inbounds i32, i32* %47, i64 %48
%50 = load i32, i32* %49
%51 = and i32 %50, 255
%52 = load i32*, i32** %5
%53 = getelementptr inbounds i32, i32* %52, i64 3
store i32 %51, i32* %53
br label %54
54: ; preds = %22
br label %55
55: ; preds = %54
%56 = load i64, i64* %7
%57 = add i64 %56, 1
store i64 %57, i64* %7
%58 = load i32*, i32** %5
%59 = getelementptr inbounds i32, i32* %58, i64 4
store i32* %59, i32** %5
br label %17
60: ; preds = %17
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %100
63: ; preds = %60
%64 = load i32, i32* %9
store i32 %64, i32* %11
%65 = load i32*, i32** %6
%66 = load i64, i64* %7
%67 = getelementptr inbounds i32, i32* %65, i64 %66
%68 = load i32, i32* %67
store i32 %68, i32* %10
%69 = load i32, i32* %9
switch i32 %69, label %70 [
i32 3, label %72
i32 2, label %81
i32 1, label %90
]
70: ; preds = %63
%71 = call i32 (...) @abort()
unreachable
72: ; preds = %63
%73 = load i32, i32* %10
%74 = ashr i32 %73, 8
%75 = and i32 %74, 255
%76 = load i32*, i32** %5
%77 = load i32, i32* %11
%78 = add i32 %77, -1
store i32 %78, i32* %11
%79 = zext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %76, i64 %79
store i32 %75, i32* %80
br label %81
81: ; preds = %63, %72
%82 = load i32, i32* %10
%83 = ashr i32 %82, 16
%84 = and i32 %83, 255
%85 = load i32*, i32** %5
%86 = load i32, i32* %11
%87 = add i32 %86, -1
store i32 %87, i32* %11
%88 = zext i32 %87 to i64
%89 = getelementptr inbounds i32, i32* %85, i64 %88
store i32 %84, i32* %89
br label %90
90: ; preds = %63, %81
%91 = load i32, i32* %10
%92 = ashr i32 %91, 24
%93 = and i32 %92, 255
%94 = load i32*, i32** %5
%95 = load i32, i32* %11
%96 = add i32 %95, -1
store i32 %96, i32* %11
%97 = zext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %94, i64 %97
store i32 %93, i32* %98
br label %99
99: ; preds = %90
br label %100
100: ; preds = %99, %60
ret void
}

LLVM IR (-O2):
define void @_nettle_write_be32(i64 %0, i32* nocapture writeonly %1, i32* nocapture %2) {
%4 = trunc i64 %0 to i32
%5 = and i32 %4, 3
%6 = icmp ult i64 %0, 4
br i1 %6, label %29, label %7
7: ; preds = %3
%8 = lshr i64 %0, 2
br label %9
9: ; preds = %7, %9
%10 = phi i32* [ %27, %9 ], [ %1, %7 ]
%11 = phi i64 [ %26, %9 ], [ 0, %7 ]
%12 = getelementptr inbounds i32, i32* %2, i64 %11
%13 = load i32, i32* %12
%14 = lshr i32 %13, 24
store i32 %14, i32* %10
%15 = load i32, i32* %12
%16 = lshr i32 %15, 16
%17 = and i32 %16, 255
%18 = getelementptr inbounds i32, i32* %10, i64 1
store i32 %17, i32* %18
%19 = load i32, i32* %12
%20 = lshr i32 %19, 8
%21 = and i32 %20, 255
%22 = getelementptr inbounds i32, i32* %10, i64 2
store i32 %21, i32* %22
%23 = load i32, i32* %12
%24 = and i32 %23, 255
%25 = getelementptr inbounds i32, i32* %10, i64 3
store i32 %24, i32* %25
%26 = add nuw nsw i64 %11, 1
%27 = getelementptr inbounds i32, i32* %10, i64 4
%28 = icmp eq i64 %26, %8
br i1 %28, label %29, label %9
29: ; preds = %9, %3
%30 = phi i64 [ 0, %3 ], [ %8, %9 ]
%31 = phi i32* [ %1, %3 ], [ %27, %9 ]
%32 = icmp eq i32 %5, 0
br i1 %32, label %48, label %33
33: ; preds = %29
%34 = getelementptr inbounds i32, i32* %2, i64 %30
%35 = load i32, i32* %34
switch i32 %5, label %36 [
i32 3, label %38
i32 2, label %42
i32 1, label %46
]
36: ; preds = %33
%37 = tail call i32 (...) @abort()
unreachable
38: ; preds = %33
%39 = lshr i32 %35, 8
%40 = and i32 %39, 255
%41 = getelementptr inbounds i32, i32* %31, i64 2
store i32 %40, i32* %41
br label %42
42: ; preds = %33, %38
%43 = lshr i32 %35, 16
%44 = and i32 %43, 255
%45 = getelementptr inbounds i32, i32* %31, i64 1
store i32 %44, i32* %45
br label %46
46: ; preds = %33, %42
%47 = lshr i32 %35, 24
store i32 %47, i32* %31
br label %48
48: ; preds = %46, %29
ret void
}