#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;
int /*<<< orphan*/ SHA256_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA256_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA256_Update(int /*<<< orphan*/ *, unsigned char *,
                                 int /*<<< orphan*/);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ strlen(char const *);
char *get_sha256(const char *input) {
  SHA256_CTX context;
  unsigned char md[SHA256_DIGEST_LENGTH];
  int i = 0;
  char *tmp = NULL;
  char *result = (char *)calloc(SHA256_DIGEST_LENGTH * 2 + 1, 1);
  SHA256_Init(&context);
  SHA256_Update(&context, (unsigned char *)input, strlen(input));
  SHA256_Final(md, &context);
  tmp = result;
  for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
    snprintf(tmp, 3, "%.2x", (unsigned int)md[i]);
    tmp += 2;
  }
  return result;
}


x86 assembly (clang, -O0):
<get_sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
shl    $0x1,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x38(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <SHA256_Init@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    -0xc(%rbp),%rdi
call   1080 <SHA256_Update@plt>
mov    -0x40(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <SHA256_Final@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%eax
jge    1256 <get_sha256+0xf6>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%ecx
mov    $0x3,%esi
lea    0xdc9(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1209 <get_sha256+0xa9>
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sha256>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    %rsp,-0x38(%rbp)
mov    0x2e61(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x0(%r13),%eax
lea    (%rax,%rax,1),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
lea    -0x3c(%rbp),%rbx
mov    %rbx,%rdi
call   1050 <SHA256_Init@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1080 <SHA256_Update@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <SHA256_Final@plt>
cmpl   $0x0,0x0(%r13)
jle    1216 <get_sha256+0xb6>
xor    %r14d,%r14d
lea    0xe17(%rip),%r15
mov    -0x30(%rbp),%rbx
nopl   (%rax)
movzbl (%r12,%r14,1),%ecx
mov    %rbx,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1040 <snprintf@plt>
add    $0x2,%rbx
add    $0x1,%r14
movslq 0x0(%r13),%rax
cmp    %rax,%r14
jl     11f0 <get_sha256+0x90>
mov    -0x38(%rbp),%rsp
mov    -0x30(%rbp),%rax
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"%.2x\00"
define i8* @get_sha256(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %2
%9 = load i32, i32* @SHA256_DIGEST_LENGTH
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
store i32 0, i32* %6
store i8* null, i8** %7
%13 = load i32, i32* @SHA256_DIGEST_LENGTH
%14 = mul nsw i32 %13, 2
%15 = add nsw i32 %14, 1
%16 = call i64 @calloc(i32 %15, i32 1)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %8
%18 = call i32 @SHA256_Init(i32* %3)
%19 = load i8*, i8** %2
%20 = load i8*, i8** %2
%21 = call i32 @strlen(i8* %20)
%22 = call i32 @SHA256_Update(i32* %3, i8* %19, i32 %21)
%23 = call i32 @SHA256_Final(i8* %12, i32* %3)
%24 = load i8*, i8** %8
store i8* %24, i8** %7
store i32 0, i32* %6
br label %25
25: ; preds = %39, %1
%26 = load i32, i32* %6
%27 = load i32, i32* @SHA256_DIGEST_LENGTH
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %42
29: ; preds = %25
%30 = load i8*, i8** %7
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %12, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = call i32 @snprintf(i8* %30, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 2
store i8* %38, i8** %7
br label %39
39: ; preds = %29
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %25
42: ; preds = %25
%43 = load i8*, i8** %8
%44 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
@SHA256_DIGEST_LENGTH = global i32 0
@.str = [5 x i8] c"%.2x\00"
define i8* @get_sha256(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @SHA256_DIGEST_LENGTH
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = load i32, i32* @SHA256_DIGEST_LENGTH
%9 = shl nsw i32 %8, 1
%10 = or i32 %9, 1
%11 = tail call i64 @calloc(i32 %10, i32 1)
%12 = inttoptr i64 %11 to i8*
%13 = call i32 @SHA256_Init(i32* %2)
%14 = call i32 @strlen(i8* %0)
%15 = call i32 @SHA256_Update(i32* %2, i8* %0, i32 %14)
%16 = call i32 @SHA256_Final(i8* %7, i32* %2)
%17 = load i32, i32* @SHA256_DIGEST_LENGTH
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %1, %19
%20 = phi i64 [ %27, %19 ], [ 0, %1 ]
%21 = phi i8* [ %26, %19 ], [ %12, %1 ]
%22 = getelementptr inbounds i8, i8* %7, i64 %20
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = call i32 @snprintf(i8* %21, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds i8, i8* %21, i64 2
%27 = add nuw nsw i64 %20, 1
%28 = load i32, i32* @SHA256_DIGEST_LENGTH
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %19, label %31
31: ; preds = %19, %1
ret i8* %12
}