#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 scalar_t__ uint64_t;
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*/ *, ...);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int strlen(char const *);
char toupper(char const);
uint64_t *sha256_hash(int num_hashes, const char *str) {
  char *tmp_str = (char *)calloc(strlen(str) + 1, sizeof(char));
  for (unsigned int i = 0; i < strlen(str); i++) {
    tmp_str[i] = toupper(str[i]);
  }
  uint64_t *results = (uint64_t *)calloc(num_hashes, sizeof(uint64_t));
  unsigned char digest[SHA256_DIGEST_LENGTH];
  for (int i = 0; i < num_hashes; i++) {
    SHA256_CTX sha256_ctx;
    SHA256_Init(&sha256_ctx);
    if (i == 0) {
      SHA256_Update(&sha256_ctx, tmp_str, strlen(tmp_str));
    } else {
      SHA256_Update(&sha256_ctx, digest, SHA256_DIGEST_LENGTH);
    }
    SHA256_Final(digest, &sha256_ctx);
    results[i] = (uint64_t) * (uint64_t *)digest;
  }
  free(tmp_str);
  return results;
}


x86 assembly (clang, -O0):
<sha256_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jae    11e8 <sha256_hash+0x78>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1040 <toupper@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a2 <sha256_hash+0x32>
mov    -0x4(%rbp),%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12b8 <sha256_hash+0x148>
lea    -0x40(%rbp),%rdi
call   1060 <SHA256_Init@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    1272 <sha256_hash+0x102>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
lea    -0x40(%rbp),%rdi
mov    $0x0,%al
call   1090 <SHA256_Update@plt>
jmp    128a <sha256_hash+0x11a>
mov    -0x50(%rbp),%rsi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rdi
mov    $0x0,%al
call   1090 <SHA256_Update@plt>
mov    -0x50(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <SHA256_Final@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    122c <sha256_hash+0xbc>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha256_hash>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     11d1 <sha256_hash+0x61>
xor    %ebx,%ebx
nopl   (%rax)
movsbl (%r15,%rbx,1),%edi
call   1040 <toupper@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     11b0 <sha256_hash+0x40>
mov    %r14d,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %r14d,%r14d
jle    128d <sha256_hash+0x11d>
lea    -0x34(%rbp),%r15
mov    %r15,%rdi
call   1060 <SHA256_Init@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <SHA256_Update@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <SHA256_Final@plt>
mov    (%rbx),%rax
mov    -0x30(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x1,%r14d
je     128d <sha256_hash+0x11d>
mov    %r14d,%r13d
mov    $0x1,%r15d
lea    -0x34(%rbp),%r14
mov    %r14,%rdi
call   1060 <SHA256_Init@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <SHA256_Update@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <SHA256_Final@plt>
mov    (%rbx),%rax
mov    -0x30(%rbp),%rcx
mov    %rax,(%rcx,%r15,8)
add    $0x1,%r15
cmp    %r15,%r13
jne    1250 <sha256_hash+0xe0>
mov    %r12,%rdi
call   1030 <free@plt>
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
define i64* @sha256_hash(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 1
%15 = call i64 @calloc(i32 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %5
store i32 0, i32* %6
br label %17
17: ; preds = %33, %2
%18 = load i32, i32* %6
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = icmp ult i32 %18, %20
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call signext i8 @toupper(i8 signext %27)
%29 = load i8*, i8** %5
%30 = load i32, i32* %6
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %22
%34 = load i32, i32* %6
%35 = add i32 %34, 1
store i32 %35, i32* %6
br label %17
36: ; preds = %17
%37 = load i32, i32* %3
%38 = call i64 @calloc(i32 %37, i32 8)
%39 = inttoptr i64 %38 to i64*
store i64* %39, i64** %7
%40 = load i32, i32* @SHA256_DIGEST_LENGTH
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %8
%43 = alloca i8, i64 %41
store i64 %41, i64* %9
store i32 0, i32* %10
br label %44
44: ; preds = %68, %36
%45 = load i32, i32* %10
%46 = load i32, i32* %3
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %71
48: ; preds = %44
%49 = call i32 @SHA256_Init(i32* %11)
%50 = load i32, i32* %10
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i8*, i8** %5
%54 = load i8*, i8** %5
%55 = call i32 @strlen(i8* %54)
%56 = call i32 (i32*, ...) @SHA256_Update(i32* %11, i8* %53, i32 %55)
br label %60
57: ; preds = %48
%58 = load i32, i32* @SHA256_DIGEST_LENGTH
%59 = call i32 (i32*, ...) @SHA256_Update(i32* %11, i8* %43, i32 %58)
br label %60
60: ; preds = %57, %52
%61 = call i32 @SHA256_Final(i8* %43, i32* %11)
%62 = bitcast i8* %43 to i64*
%63 = load i64, i64* %62
%64 = load i64*, i64** %7
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i64, i64* %64, i64 %66
store i64 %63, i64* %67
br label %68
68: ; preds = %60
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %44
71: ; preds = %44
%72 = load i8*, i8** %5
%73 = call i32 @free(i8* %72)
%74 = load i64*, i64** %7
%75 = load i8*, i8** %8
ret i64* %74
}

LLVM IR (-O2):
@SHA256_DIGEST_LENGTH = global i32 0
define i64* @sha256_hash(i32 %0, i8* %1) {
%3 = alloca i32
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %4, 1
%6 = tail call i64 @calloc(i32 %5, i32 1)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @strlen(i8* %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %27, %2
%11 = tail call i64 @calloc(i32 %0, i32 8)
%12 = inttoptr i64 %11 to i64*
%13 = load i32, i32* @SHA256_DIGEST_LENGTH
%14 = zext i32 %13 to i64
%15 = alloca i8, i64 %14
%16 = icmp sgt i32 %0, 0
br i1 %16, label %17, label %37
17: ; preds = %10
%18 = bitcast i32* %3 to i8*
%19 = bitcast i8* %15 to i64*
%20 = zext i32 %0 to i64
%21 = call i32 @SHA256_Init(i32* %3)
%22 = call i32 @strlen(i8* %7)
%23 = call i32 (i32*, ...) @SHA256_Update(i32* %3, i8* %7, i32 %22)
%24 = call i32 @SHA256_Final(i8* %15, i32* %3)
%25 = load i64, i64* %19
store i64 %25, i64* %12
%26 = icmp eq i32 %0, 1
br i1 %26, label %37, label %39
27: ; preds = %2, %27
%28 = phi i64 [ %33, %27 ], [ 0, %2 ]
%29 = getelementptr inbounds i8, i8* %1, i64 %28
%30 = load i8, i8* %29
%31 = tail call signext i8 @toupper(i8 signext %30)
%32 = getelementptr inbounds i8, i8* %7, i64 %28
store i8 %31, i8* %32
%33 = add nuw nsw i64 %28, 1
%34 = tail call i32 @strlen(i8* %1)
%35 = zext i32 %34 to i64
%36 = icmp ult i64 %33, %35
br i1 %36, label %27, label %10
37: ; preds = %39, %17, %10
%38 = call i32 @free(i8* %7)
ret i64* %12
39: ; preds = %17, %39
%40 = phi i64 [ %47, %39 ], [ 1, %17 ]
%41 = call i32 @SHA256_Init(i32* %3)
%42 = load i32, i32* @SHA256_DIGEST_LENGTH
%43 = call i32 (i32*, ...) @SHA256_Update(i32* %3, i8* %15, i32 %42)
%44 = call i32 @SHA256_Final(i8* %15, i32* %3)
%45 = load i64, i64* %19
%46 = getelementptr inbounds i64, i64* %12, i64 %40
store i64 %45, i64* %46
%47 = add nuw nsw i64 %40, 1
%48 = icmp eq i64 %47, %20
br i1 %48, label %37, label %39
}