#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*/ *, char *, size_t);
int /*<<< orphan*/ assert(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
size_t strnlen(char *, int);
void string_to_sha256(char *input, char *output) {
  assert(input);
  assert(output);
  unsigned char hash[SHA256_DIGEST_LENGTH];
  size_t len = strnlen(input, 128);
  SHA256_CTX context;
  SHA256_Init(&context);
  SHA256_Update(&context, input, len);
  SHA256_Final(hash, &context);
  int i = 0;
  for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
    sprintf(output + (i * 2), "%02x", hash[i]);
  }
  output[65 - 1] = '\0';
}


x86 assembly (clang, -O0):
<string_to_sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    0x2e57(%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,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x80,%esi
call   1030 <strnlen@plt>
mov    %rax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <SHA256_Init@plt>
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rdi
call   1070 <SHA256_Update@plt>
mov    -0x38(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1060 <SHA256_Final@plt>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%eax
jge    123d <string_to_sha256+0xdd>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
movslq -0x30(%rbp),%rcx
lea    0xdda(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1080 <sprintf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11f5 <string_to_sha256+0x95>
mov    -0x10(%rbp),%rax
movb   $0x0,0x40(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

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

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