#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 unsigned char uint32_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sha256_init(unsigned char *);
int /*<<< orphan*/ sha256_transform(unsigned char *, unsigned char *,
                                    int /*<<< orphan*/);
void sha256(unsigned char *hash, const unsigned char *data, int len) {
  uint32_t S[16], T[16];
  int i, r;
  sha256_init(S);
  for (r = len; r > -9; r -= 64) {
    if (r < 64)
      memset(T, 0, 64);
    memcpy(T, data + len - r, r > 64 ? 64 : (r < 0 ? 0 : r));
    if (r >= 0 && r < 64)
      ((unsigned char *)T)[r + 3] = 0x80;
    if (r < 56)
      T[15] = 8 * len;
    sha256_transform(S, T, 0);
  }
  memcpy(hash, S, len);
}


x86 assembly (clang, -O0):
<sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <sha256_init@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
cmpl   $0xfffffff7,-0x48(%rbp)
jle    1241 <sha256+0x101>
cmpl   $0x40,-0x48(%rbp)
jge    1186 <sha256+0x46>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
movslq -0x48(%rbp),%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
cmpl   $0x40,-0x48(%rbp)
jle    11c0 <sha256+0x80>
mov    $0x40,%eax
mov    %eax,-0x5c(%rbp)
jmp    11e0 <sha256+0xa0>
cmpl   $0x0,-0x48(%rbp)
jge    11d4 <sha256+0x94>
xor    %eax,%eax
mov    %eax,-0x60(%rbp)
jmp    11da <sha256+0x9a>
mov    -0x48(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdi
mov    -0x5c(%rbp),%edx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x48(%rbp)
jl     1211 <sha256+0xd1>
cmpl   $0x40,-0x48(%rbp)
jge    1211 <sha256+0xd1>
mov    -0x48(%rbp),%eax
add    $0x3,%eax
cltq
movb   $0x80,-0x40(%rbp,%rax,1)
cmpl   $0x38,-0x48(%rbp)
jge    1224 <sha256+0xe4>
mov    -0x14(%rbp),%eax
shl    $0x3,%eax
mov    %al,-0x31(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1060 <sha256_transform@plt>
mov    -0x48(%rbp),%eax
sub    $0x40,%eax
mov    %eax,-0x48(%rbp)
jmp    1162 <sha256+0x22>
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x20(%rsp),%rdi
call   1040 <sha256_init@plt>
cmp    $0xfffffff8,%r15d
jl     121a <sha256+0xda>
mov    %rbp,0x30(%rsp)
movslq %r15d,%rbp
lea    0x0(,%r15,8),%eax
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%r12
mov    $0x40,%r14d
lea    0x20(%rsp),%r13
cmp    $0x3f,%rbp
jg     11af <sha256+0x6f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
test   %rbp,%rbp
mov    $0x0,%edx
cmovg  %ebp,%edx
cmp    $0x40,%edx
cmovae %r14d,%edx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <memcpy@plt>
test   $0xffffffc0,%ebp
jne    11d9 <sha256+0x99>
movb   $0x80,0x13(%rsp,%rbp,1)
cmp    $0x38,%rbp
jl     11fc <sha256+0xbc>
mov    %r13,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1060 <sha256_transform@plt>
add    $0xffffffffffffffc0,%rbp
add    $0x40,%rbx
cmp    $0x3f,%rbp
jle    11a0 <sha256+0x60>
jmp    11af <sha256+0x6f>
mov    0xc(%rsp),%eax
mov    %al,0x1f(%rsp)
lea    0x20(%rsp),%rdi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1060 <sha256_transform@plt>
mov    0x30(%rsp),%rbp
lea    0x20(%rsp),%rsi
mov    %rbp,%rdi
mov    %r15d,%edx
call   1050 <memcpy@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @sha256(i8* %0, i8* %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = call i32 @sha256_init(i8* %6)
%9 = icmp sgt i32 %2, -9
br i1 %9, label %10, label %42
10: ; preds = %3
%11 = sext i32 %2 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = trunc i32 %2 to i8
%14 = shl i8 %13, 3
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 15
br label %16
16: ; preds = %37, %10
%17 = phi i64 [ %11, %10 ], [ %39, %37 ]
%18 = icmp slt i64 %17, 64
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @memset(i8* %7, i32 0, i32 64)
br label %21
21: ; preds = %19, %16
%22 = sub nsw i64 0, %17
%23 = getelementptr inbounds i8, i8* %12, i64 %22
%24 = icmp sgt i64 %17, 0
%25 = select i1 %24, i64 %17, i64 0
%26 = trunc i64 %25 to i32
%27 = icmp ult i32 %26, 64
%28 = select i1 %27, i32 %26, i32 64
%29 = call i32 @memcpy(i8* %7, i8* %23, i32 %28)
%30 = and i64 %17, 4294967232
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %21
%33 = add nuw nsw i64 %17, 3
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %33
store i8 -128, i8* %34
br label %35
35: ; preds = %32, %21
%36 = icmp slt i64 %17, 56
br i1 %36, label %40, label %37
37: ; preds = %35
%38 = call i32 @sha256_transform(i8* %6, i8* %7, i32 0)
%39 = add nsw i64 %17, -64
br label %16
40: ; preds = %35
store i8 %14, i8* %15
%41 = call i32 @sha256_transform(i8* %6, i8* %7, i32 0)
br label %42
42: ; preds = %40, %3
%43 = call i32 @memcpy(i8* %0, i8* %6, i32 %2)
ret void
}