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

/* Variables and functions */
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sha_hash(int *, int *);
int /*<<< orphan*/ sha_init(int *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int /*<<< orphan*/ strprintsha(char *, int *);
char *shahash(const char *str) {
  char read_buffer[65];
  int c = 1, i;
  long long length = 0;
  int strsz;
  static char final[40];
  int *hashval;
  hashval = (int *)malloc(20);
  sha_init(hashval);
  strsz = strlen(str);
  if (strsz == 0) {
    memset(read_buffer, 0, 65);
    read_buffer[0] = 0x80;
    sha_hash((int *)read_buffer, hashval);
  }
  while (strsz > 0) {
    memset(read_buffer, 0, 65);
    strncpy((char *)read_buffer, str, 64);
    c = strlen((char *)read_buffer);
    length += c;
    strsz -= c;
    if (strsz <= 0) {
      length <<= 3;
      read_buffer[c] = (char)0x80;
      for (i = c + 1; i < 64; i++)
        read_buffer[i] = 0;
      if (c > 55) {
        sha_hash((int *)read_buffer, hashval);
        for (i = 0; i < 14; i++)
          ((int *)read_buffer)[i] = 0;
      }
      for (i = 0; i < 8; i++) {
        read_buffer[56 + i] = (char)(length >> (56 - (i * 8))) & 0xff;
      }
    }
    sha_hash((int *)read_buffer, hashval);
    str += 64;
  }
  strprintsha((char *) final, hashval);
  free(hashval);
  return (char *) final;
}


x86 assembly (clang, -O0):
<shahash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x54(%rbp)
movq   $0x0,-0x60(%rbp)
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rdi
call   1090 <sha_init@plt>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jne    11e9 <shahash+0x69>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
movb   $0x80,-0x50(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x70(%rbp),%rsi
call   10a0 <sha_hash@plt>
jmp    11ee <shahash+0x6e>
cmpl   $0x0,-0x64(%rbp)
jle    133b <shahash+0x1bb>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x40,%edx
call   1040 <strncpy@plt>
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x54(%rbp)
movslq -0x54(%rbp),%rax
add    -0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x54(%rbp),%ecx
mov    -0x64(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jg     131d <shahash+0x19d>
mov    -0x60(%rbp),%rax
shl    $0x3,%rax
mov    %rax,-0x60(%rbp)
movslq -0x54(%rbp),%rax
movb   $0x80,-0x50(%rbp,%rax,1)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
cmpl   $0x40,-0x58(%rbp)
jge    1286 <shahash+0x106>
movslq -0x58(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    1265 <shahash+0xe5>
cmpl   $0x37,-0x54(%rbp)
jle    12cd <shahash+0x14d>
lea    -0x50(%rbp),%rdi
mov    -0x70(%rbp),%rsi
call   10a0 <sha_hash@plt>
movl   $0x0,-0x58(%rbp)
cmpl   $0xe,-0x58(%rbp)
jge    12c8 <shahash+0x148>
movslq -0x58(%rbp),%rax
movl   $0x0,-0x50(%rbp,%rax,4)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    12a4 <shahash+0x124>
jmp    12cd <shahash+0x14d>
movl   $0x0,-0x58(%rbp)
cmpl   $0x8,-0x58(%rbp)
jge    1318 <shahash+0x198>
mov    -0x60(%rbp),%rax
mov    -0x58(%rbp),%edx
shl    $0x3,%edx
mov    $0x38,%ecx
sub    %edx,%ecx
mov    %ecx,%ecx
sar    %cl,%rax
movsbl %al,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x58(%rbp),%eax
add    $0x38,%eax
cltq
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    12d4 <shahash+0x154>
jmp    131d <shahash+0x19d>
lea    -0x50(%rbp),%rdi
mov    -0x70(%rbp),%rsi
call   10a0 <sha_hash@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <shahash+0x6e>
mov    -0x70(%rbp),%rsi
lea    0x2d2a(%rip),%rdi
call   1080 <strprintsha@plt>
mov    -0x70(%rbp),%rdi
call   1030 <free@plt>
lea    0x2d15(%rip),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shahash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <sha_init@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     1244 <shahash+0xc4>
jle    12c0 <shahash+0x140>
mov    %eax,%r15d
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1040 <strncpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
cltq
sub    %ebp,%r15d
jle    1259 <shahash+0xd9>
add    $0x40,%rbx
mov    %rsp,%r12
mov    %rax,%r13
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r14,%rsi
call   10a0 <sha_hash@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1040 <strncpy@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
cltq
add    %rax,%r13
add    $0x40,%rbx
sub    %ebp,%r15d
jg     1200 <shahash+0x80>
jmp    125c <shahash+0xdc>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x41,%edx
call   1060 <memset@plt>
movb   $0x80,(%rsp)
jmp    12b5 <shahash+0x135>
mov    %rax,%r13
shl    $0x3,%r13
movb   $0x80,(%rsp,%rax,1)
cmp    $0x3e,%ebp
jg     1288 <shahash+0x108>
lea    0x1(%rsp),%rdi
add    %rax,%rdi
mov    $0x3e,%edx
sub    %ebp,%edx
add    $0x1,%rdx
xor    %esi,%esi
call   1060 <memset@plt>
cmp    $0x38,%ebp
jl     12ad <shahash+0x12d>
mov    %rsp,%rdi
mov    %r14,%rsi
call   10a0 <sha_hash@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
bswap  %r13
mov    %r13,0x38(%rsp)
mov    %rsp,%rdi
mov    %r14,%rsi
call   10a0 <sha_hash@plt>
lea    0x2da9(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strprintsha@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@shahash.final = internal global [40 x i8] zeroinitializer
define i8* @shahash(i8* %0) {
%2 = alloca i8*
%3 = alloca [65 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %2
store i32 1, i32* %4
store i64 0, i64* %6
%9 = call i64 @malloc(i32 20)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = call i32 @sha_init(i32* %11)
%13 = load i8*, i8** %2
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %1
%18 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 0, i32 65)
%20 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
store i8 -128, i8* %20
%21 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%22 = bitcast i8* %21 to i32*
%23 = load i32*, i32** %8
%24 = call i32 @sha_hash(i32* %22, i32* %23)
br label %25
25: ; preds = %17, %1
br label %26
26: ; preds = %108, %25
%27 = load i32, i32* %7
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %115
29: ; preds = %26
%30 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 65)
%32 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%33 = load i8*, i8** %2
%34 = call i32 @strncpy(i8* %32, i8* %33, i32 64)
%35 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = load i64, i64* %6
%40 = add nsw i64 %39, %38
store i64 %40, i64* %6
%41 = load i32, i32* %4
%42 = load i32, i32* %7
%43 = sub nsw i32 %42, %41
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp sle i32 %44, 0
br i1 %45, label %46, label %108
46: ; preds = %29
%47 = load i64, i64* %6
%48 = shl i64 %47, 3
store i64 %48, i64* %6
%49 = load i32, i32* %4
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 %50
store i8 -128, i8* %51
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %5
br label %54
54: ; preds = %61, %46
%55 = load i32, i32* %5
%56 = icmp slt i32 %55, 64
br i1 %56, label %57, label %64
57: ; preds = %54
%58 = load i32, i32* %5
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 %59
store i8 0, i8* %60
br label %61
61: ; preds = %57
%62 = load i32, i32* %5
%63 = add nsw i32 %62, 1
store i32 %63, i32* %5
br label %54
64: ; preds = %54
%65 = load i32, i32* %4
%66 = icmp sgt i32 %65, 55
br i1 %66, label %67, label %85
67: ; preds = %64
%68 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%69 = bitcast i8* %68 to i32*
%70 = load i32*, i32** %8
%71 = call i32 @sha_hash(i32* %69, i32* %70)
store i32 0, i32* %5
br label %72
72: ; preds = %81, %67
%73 = load i32, i32* %5
%74 = icmp slt i32 %73, 14
br i1 %74, label %75, label %84
75: ; preds = %72
%76 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%77 = bitcast i8* %76 to i32*
%78 = load i32, i32* %5
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
store i32 0, i32* %80
br label %81
81: ; preds = %75
%82 = load i32, i32* %5
%83 = add nsw i32 %82, 1
store i32 %83, i32* %5
br label %72
84: ; preds = %72
br label %85
85: ; preds = %84, %64
store i32 0, i32* %5
br label %86
86: ; preds = %104, %85
%87 = load i32, i32* %5
%88 = icmp slt i32 %87, 8
br i1 %88, label %89, label %107
89: ; preds = %86
%90 = load i64, i64* %6
%91 = load i32, i32* %5
%92 = mul nsw i32 %91, 8
%93 = sub nsw i32 56, %92
%94 = zext i32 %93 to i64
%95 = ashr i64 %90, %94
%96 = trunc i64 %95 to i8
%97 = sext i8 %96 to i32
%98 = and i32 %97, 255
%99 = trunc i32 %98 to i8
%100 = load i32, i32* %5
%101 = add nsw i32 56, %100
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 %102
store i8 %99, i8* %103
br label %104
104: ; preds = %89
%105 = load i32, i32* %5
%106 = add nsw i32 %105, 1
store i32 %106, i32* %5
br label %86
107: ; preds = %86
br label %108
108: ; preds = %107, %29
%109 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%110 = bitcast i8* %109 to i32*
%111 = load i32*, i32** %8
%112 = call i32 @sha_hash(i32* %110, i32* %111)
%113 = load i8*, i8** %2
%114 = getelementptr inbounds i8, i8* %113, i64 64
store i8* %114, i8** %2
br label %26
115: ; preds = %26
%116 = load i32*, i32** %8
%117 = call i32 @strprintsha(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @shahash.final, i64 0, i64 0), i32* %116)
%118 = load i32*, i32** %8
%119 = call i32 @free(i32* %118)
ret i8* getelementptr inbounds ([40 x i8], [40 x i8]* @shahash.final, i64 0, i64 0)
}

LLVM IR (-O2):
@shahash.final = internal global [40 x i8] zeroinitializer
define i8* @shahash(i8* %0) {
%2 = alloca [65 x i8]
%3 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0
%4 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0
%5 = tail call i64 @malloc(i32 20)
%6 = inttoptr i64 %5 to i32*
%7 = tail call i32 @sha_init(i32* %6)
%8 = tail call i32 @strlen(i8* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @memset(i8* %4, i32 0, i32 65)
store i8 -128, i8* %4
br label %75
12: ; preds = %1
%13 = icmp sgt i32 %8, 0
br i1 %13, label %14, label %78
14: ; preds = %12
%15 = bitcast [65 x i8]* %2 to i32*
%16 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 1
%17 = call i32 @memset(i8* %4, i32 0, i32 65)
%18 = call i32 @strncpy(i8* %4, i8* %0, i32 64)
%19 = call i32 @strlen(i8* %4)
%20 = sext i32 %19 to i64
%21 = sub nsw i32 %8, %19
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %38
23: ; preds = %38, %14
%24 = phi i32 [ %19, %14 ], [ %46, %38 ]
%25 = phi i64 [ %20, %14 ], [ %47, %38 ]
%26 = phi i64 [ %20, %14 ], [ %48, %38 ]
%27 = shl i64 %26, 3
%28 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 %25
store i8 -128, i8* %28
%29 = icmp slt i32 %24, 63
br i1 %29, label %30, label %36
30: ; preds = %23
%31 = getelementptr i8, i8* %16, i64 %25
%32 = sub i32 62, %24
%33 = zext i32 %32 to i64
%34 = add nuw nsw i64 %33, 1
%35 = icmp sgt i32 %24, 55
br i1 %35, label %36, label %51
36: ; preds = %23, %30
%37 = call i32 @sha_hash(i32* %15, i32* %6)
br label %51
38: ; preds = %14, %38
%39 = phi i32 [ %49, %38 ], [ %21, %14 ]
%40 = phi i64 [ %48, %38 ], [ %20, %14 ]
%41 = phi i8* [ %43, %38 ], [ %0, %14 ]
%42 = call i32 @sha_hash(i32* %15, i32* %6)
%43 = getelementptr inbounds i8, i8* %41, i64 64
%44 = call i32 @memset(i8* %4, i32 0, i32 65)
%45 = call i32 @strncpy(i8* %4, i8* %43, i32 64)
%46 = call i32 @strlen(i8* %4)
%47 = sext i32 %46 to i64
%48 = add nsw i64 %40, %47
%49 = sub nsw i32 %39, %46
%50 = icmp slt i32 %49, 1
br i1 %50, label %23, label %38
51: ; preds = %30, %36
%52 = lshr i64 %27, 56
%53 = trunc i64 %52 to i8
%54 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 56
store i8 %53, i8* %54
%55 = lshr i64 %27, 48
%56 = trunc i64 %55 to i8
%57 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 57
store i8 %56, i8* %57
%58 = lshr i64 %27, 40
%59 = trunc i64 %58 to i8
%60 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 58
store i8 %59, i8* %60
%61 = lshr i64 %27, 32
%62 = trunc i64 %61 to i8
%63 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 59
store i8 %62, i8* %63
%64 = lshr i64 %27, 24
%65 = trunc i64 %64 to i8
%66 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 60
store i8 %65, i8* %66
%67 = lshr i64 %27, 16
%68 = trunc i64 %67 to i8
%69 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 61
store i8 %68, i8* %69
%70 = lshr i64 %27, 8
%71 = trunc i64 %70 to i8
%72 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 62
store i8 %71, i8* %72
%73 = trunc i64 %27 to i8
%74 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 63
store i8 %73, i8* %74
br label %75
75: ; preds = %51, %10
%76 = bitcast [65 x i8]* %2 to i32*
%77 = call i32 @sha_hash(i32* %76, i32* %6)
br label %78
78: ; preds = %75, %12
%79 = call i32 @strprintsha(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @shahash.final, i64 0, i64 0), i32* %6)
%80 = call i32 @free(i32* %6)
ret i8* getelementptr inbounds ([40 x i8], [40 x i8]* @shahash.final, i64 0, i64 0)
}