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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memcpy(void *, unsigned char *, int);
int /*<<< orphan*/ sha256(void const *, size_t, unsigned char *);
int /*<<< orphan*/ sha256_inplace(unsigned char *);
void *sha256_chain(const void *seed, size_t seed_length, size_t elements,
                   void *tail_element) {
  unsigned char tmp_element[32];
  assert(elements >= 2);
  sha256(seed, seed_length, tmp_element);
  for (size_t i = 0; i < (elements - 1); ++i) {
    sha256_inplace(tmp_element);
  }
  memcpy(tail_element, tmp_element, 32);
  return tail_element;
}


x86 assembly (clang, -O0):
<sha256_chain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x2,-0x18(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <sha256@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    11b2 <sha256_chain+0x72>
lea    -0x40(%rbp),%rdi
call   1060 <sha256_inplace@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1183 <sha256_chain+0x43>
mov    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256_chain>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
xor    %edi,%edi
cmp    $0x2,%rdx
setae  %dil
call   1040 <assert@plt>
mov    %rsp,%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <sha256@plt>
add    $0xffffffffffffffff,%rbx
je     118e <sha256_chain+0x4e>
mov    %rsp,%r15
nopl   (%rax)
mov    %r15,%rdi
call   1060 <sha256_inplace@plt>
add    $0xffffffffffffffff,%rbx
jne    1180 <sha256_chain+0x40>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @sha256_chain(i8* %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca [32 x i8]
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i8* %3, i8** %8
%11 = load i64, i64* %7
%12 = icmp uge i64 %11, 2
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%18 = call i32 @sha256(i8* %15, i64 %16, i8* %17)
store i64 0, i64* %10
br label %19
19: ; preds = %27, %4
%20 = load i64, i64* %10
%21 = load i64, i64* %7
%22 = sub i64 %21, 1
%23 = icmp ult i64 %20, %22
br i1 %23, label %24, label %30
24: ; preds = %19
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%26 = call i32 @sha256_inplace(i8* %25)
br label %27
27: ; preds = %24
%28 = load i64, i64* %10
%29 = add i64 %28, 1
store i64 %29, i64* %10
br label %19
30: ; preds = %19
%31 = load i8*, i8** %8
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%33 = call i32 @memcpy(i8* %31, i8* %32, i32 32)
%34 = load i8*, i8** %8
ret i8* %34
}

LLVM IR (-O2):
define i8* @sha256_chain(i8* %0, i64 %1, i64 %2, i8* returned %3) {
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%7 = icmp ugt i64 %2, 1
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = call i32 @sha256(i8* %0, i64 %1, i8* %6)
%11 = add i64 %2, -1
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %15, %4
%14 = call i32 @memcpy(i8* %3, i8* %6, i32 32)
ret i8* %3
15: ; preds = %4, %15
%16 = phi i64 [ %18, %15 ], [ 0, %4 ]
%17 = call i32 @sha256_inplace(i8* %6)
%18 = add nuw i64 %16, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %13, label %15
}