#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);
scalar_t__ memcmp(unsigned char *, void *, int);
int /*<<< orphan*/ memcpy(void *, void *, int);
int /*<<< orphan*/ sha256_inplace(unsigned char *);
int sha256_chain_verify_element(void *element, size_t element_index,
                                void *tail_element, size_t chain_length) {
  unsigned char tmp_element[32];
  int delta_count = (chain_length - element_index);
  assert(delta_count >= 1);
  memcpy((void *)tmp_element, element, 32);
  for (int i = 0; i < (delta_count - 1); ++i) {
    sha256_inplace(tmp_element);
  }
  return (memcmp(tmp_element, tail_element, 32) != 0);
}


x86 assembly (clang, -O0):
<sha256_chain_verify_element>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    -0x44(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11b5 <sha256_chain_verify_element+0x75>
lea    -0x40(%rbp),%rdi
call   1060 <sha256_inplace@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    118d <sha256_chain_verify_element+0x4d>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x20,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256_chain_verify_element>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rdi,%r15
sub    %esi,%ebx
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1040 <assert@plt>
mov    %rsp,%rdi
mov    %r15,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
cmp    $0x2,%ebx
jl     118d <sha256_chain_verify_element+0x4d>
add    $0xffffffff,%ebx
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %r15,%rdi
call   1060 <sha256_inplace@plt>
add    $0xffffffff,%ebx
jne    1180 <sha256_chain_verify_element+0x40>
mov    %rsp,%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1030 <memcmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
mov    %ecx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @sha256_chain_verify_element(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%7 = sub i64 %3, %1
%8 = trunc i64 %7 to i32
%9 = icmp sgt i32 %8, 0
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = call i32 @memcpy(i8* %6, i8* %0, i32 32)
%13 = icmp sgt i32 %8, 1
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = add nsw i32 %8, -1
br label %20
16: ; preds = %20, %4
%17 = call i64 @memcmp(i8* %6, i8* %2, i32 32)
%18 = icmp ne i64 %17, 0
%19 = zext i1 %18 to i32
ret i32 %19
20: ; preds = %14, %20
%21 = phi i32 [ %23, %20 ], [ 0, %14 ]
%22 = call i32 @sha256_inplace(i8* %6)
%23 = add nuw nsw i32 %21, 1
%24 = icmp eq i32 %23, %15
br i1 %24, label %16, label %20
}