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

/* Variables and functions */
int SHA256_DIGEST_LENGTH;
scalar_t__ SHA256_Final(unsigned char *, int /*<<< orphan*/ *);
scalar_t__ SHA256_Init(int /*<<< orphan*/ *);
scalar_t__ SHA256_Update(int /*<<< orphan*/ *, char const *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int m_buffer_sha256(const char *buffer, size_t buffer_len, char checksum[65]) {
  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  if (SHA256_Init(&sha256) == 0)
    return 0;
  if (SHA256_Update(&sha256, buffer, buffer_len) == 0)
    return 0;
  if (SHA256_Final(hash, &sha256) == 0)
    return 0;
  int i = 0;
  for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
    sprintf(checksum + (i * 2), "%02x", hash[i]);
  checksum[64] = 0;
  return 1;
}


x86 assembly (clang, -O0):
<m_buffer_sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
call   1030 <SHA256_Init@plt>
cmp    $0x0,%rax
jne    11a6 <m_buffer_sha256+0x66>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    126a <m_buffer_sha256+0x12a>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x34(%rbp),%rdi
call   1050 <SHA256_Update@plt>
cmp    $0x0,%rax
jne    11d4 <m_buffer_sha256+0x94>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    126a <m_buffer_sha256+0x12a>
mov    -0x48(%rbp),%rdi
lea    -0x34(%rbp),%rsi
call   1040 <SHA256_Final@plt>
cmp    $0x0,%rax
jne    11fe <m_buffer_sha256+0xbe>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    126a <m_buffer_sha256+0x12a>
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%eax
jge    1254 <m_buffer_sha256+0x114>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x3c(%rbp),%rcx
lea    0xdc3(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1060 <sprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    120c <m_buffer_sha256+0xcc>
mov    -0x20(%rbp),%rax
movb   $0x0,0x40(%rax)
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<m_buffer_sha256>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x38(%rbp)
mov    %rsi,%rbx
mov    %rdi,%r13
mov    0x2e7e(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    -0x2c(%rbp),%rdi
call   1030 <SHA256_Init@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     11ff <m_buffer_sha256+0xbf>
lea    -0x2c(%rbp),%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <SHA256_Update@plt>
test   %rax,%rax
je     11ff <m_buffer_sha256+0xbf>
lea    -0x2c(%rbp),%rsi
mov    %r14,%rdi
call   1040 <SHA256_Final@plt>
test   %rax,%rax
je     11ff <m_buffer_sha256+0xbf>
cmpl   $0x0,(%r12)
jle    11f1 <m_buffer_sha256+0xb1>
xor    %ebx,%ebx
lea    0xe43(%rip),%r15
mov    -0x38(%rbp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1060 <sprintf@plt>
add    $0x1,%rbx
movslq (%r12),%rax
add    $0x2,%r13
cmp    %rax,%rbx
jl     11d0 <m_buffer_sha256+0x90>
mov    -0x38(%rbp),%rax
movb   $0x0,0x40(%rax)
mov    $0x1,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA256_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define i32 @m_buffer_sha256(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%13 = load i32, i32* @SHA256_DIGEST_LENGTH
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = alloca i8, i64 %14
store i64 %14, i64* %9
%17 = call i64 @SHA256_Init(i32* %10)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 0, i32* %4
store i32 1, i32* %11
br label %52
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i64 @SHA256_Update(i32* %10, i8* %21, i64 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 0, i32* %4
store i32 1, i32* %11
br label %52
26: ; preds = %20
%27 = call i64 @SHA256_Final(i8* %16, i32* %10)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 0, i32* %4
store i32 1, i32* %11
br label %52
30: ; preds = %26
store i32 0, i32* %12
store i32 0, i32* %12
br label %31
31: ; preds = %46, %30
%32 = load i32, i32* %12
%33 = load i32, i32* @SHA256_DIGEST_LENGTH
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %49
35: ; preds = %31
%36 = load i8*, i8** %7
%37 = load i32, i32* %12
%38 = mul nsw i32 %37, 2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %16, i64 %42
%44 = load i8, i8* %43
%45 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %44)
br label %46
46: ; preds = %35
%47 = load i32, i32* %12
%48 = add nsw i32 %47, 1
store i32 %48, i32* %12
br label %31
49: ; preds = %31
%50 = load i8*, i8** %7
%51 = getelementptr inbounds i8, i8* %50, i64 64
store i8 0, i8* %51
store i32 1, i32* %4
store i32 1, i32* %11
br label %52
52: ; preds = %49, %29, %25, %19
%53 = load i8*, i8** %8
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@SHA256_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%02x\00"
define i32 @m_buffer_sha256(i8* %0, i64 %1, i8* nocapture writeonly %2) {
%4 = alloca i32
%5 = load i32, i32* @SHA256_DIGEST_LENGTH
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i32* %4 to i8*
%9 = call i64 @SHA256_Init(i32* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %33, label %11
11: ; preds = %3
%12 = call i64 @SHA256_Update(i32* %4, i8* %0, i64 %1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %33, label %14
14: ; preds = %11
%15 = call i64 @SHA256_Final(i8* %7, i32* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %33, label %17
17: ; preds = %14
%18 = load i32, i32* @SHA256_DIGEST_LENGTH
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %17, %20
%21 = phi i64 [ %27, %20 ], [ 0, %17 ]
%22 = shl nuw nsw i64 %21, 1
%23 = getelementptr inbounds i8, i8* %2, i64 %22
%24 = getelementptr inbounds i8, i8* %7, i64 %21
%25 = load i8, i8* %24
%26 = call i32 @sprintf(i8* dereferenceable(1) %23, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %25)
%27 = add nuw nsw i64 %21, 1
%28 = load i32, i32* @SHA256_DIGEST_LENGTH
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %20, label %31
31: ; preds = %20, %17
%32 = getelementptr inbounds i8, i8* %2, i64 64
store i8 0, i8* %32
br label %33
33: ; preds = %14, %11, %3, %31
%34 = phi i32 [ 1, %31 ], [ 0, %3 ], [ 0, %11 ], [ 0, %14 ]
ret i32 %34
}