#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 uint32_t;
typedef int /*<<< orphan*/ SHA_CTX;

/* Variables and functions */
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, ...);
int htonl(int);
int diffuse(unsigned char *src, unsigned char *dst, int size) {
  uint32_t i;
  uint32_t IV;
  SHA_CTX ctx;
  int fullblocks = (size) / 20;
  int padding = size % 20;
  for (i = 0; i < fullblocks; i++) {
    IV = htonl(i);
    SHA1_Init(&ctx);
    SHA1_Update(&ctx, &IV, 4);
    SHA1_Update(&ctx, src + 20 * i, 20);
    SHA1_Final(dst + 20 * i, &ctx);
  }
  if (padding) {
    IV = htonl(fullblocks);
    SHA1_Init(&ctx);
    SHA1_Update(&ctx, &IV, 4);
    SHA1_Update(&ctx, src + 20 * fullblocks, padding);
    SHA1_Final(dst + 20 * fullblocks, &ctx);
  }
  return 0;
}


x86 assembly (clang, -O0):
<diffuse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11eb <diffuse+0xab>
mov    -0x18(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <SHA1_Init@plt>
lea    -0x20(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <SHA1_Update@plt>
mov    -0x8(%rbp),%rsi
imul   $0x14,-0x18(%rbp),%eax
cltq
add    %rax,%rsi
lea    -0x20(%rbp),%rdi
mov    $0x14,%edx
mov    $0x0,%al
call   1040 <SHA1_Update@plt>
mov    -0x10(%rbp),%rdi
imul   $0x14,-0x18(%rbp),%eax
cltq
add    %rax,%rdi
lea    -0x20(%rbp),%rsi
call   1060 <SHA1_Final@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1176 <diffuse+0x36>
cmpl   $0x0,-0x28(%rbp)
je     124e <diffuse+0x10e>
mov    -0x24(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <SHA1_Init@plt>
lea    -0x20(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <SHA1_Update@plt>
mov    -0x8(%rbp),%rsi
imul   $0x14,-0x24(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x28(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1040 <SHA1_Update@plt>
mov    -0x10(%rbp),%rdi
imul   $0x14,-0x24(%rbp),%eax
cltq
add    %rax,%rdi
lea    -0x20(%rbp),%rsi
call   1060 <SHA1_Final@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<diffuse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,0x28(%rsp)
mov    %rdi,0x20(%rsp)
movslq %edx,%rax
imul   $0x66666667,%rax,%rdx
mov    %rdx,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rdx
add    %ecx,%edx
mov    %rdx,0x18(%rsp)
lea    0x0(,%rdx,4),%ecx
mov    %rcx,0x30(%rsp)
lea    (%rcx,%rcx,4),%ecx
mov    %eax,%edx
sub    %ecx,%edx
mov    %edx,0x10(%rsp)
cmp    $0x14,%eax
jl     1204 <diffuse+0xc4>
mov    0x18(%rsp),%r13d
lea    0x14(%rsp),%rbp
lea    0xc(%rsp),%rbx
mov    0x20(%rsp),%r15
mov    0x28(%rsp),%r14
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %r12d,%edi
call   1030 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %rbp,%rdi
call   1050 <SHA1_Init@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <SHA1_Update@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x14,%edx
xor    %eax,%eax
call   1040 <SHA1_Update@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1060 <SHA1_Final@plt>
add    $0x1,%r12
add    $0x14,%r14
add    $0x14,%r15
cmp    %r12,%r13
jne    11b0 <diffuse+0x70>
mov    0x10(%rsp),%ebx
test   %ebx,%ebx
je     126a <diffuse+0x12a>
mov    0x18(%rsp),%rdi
call   1030 <htonl@plt>
mov    %eax,0xc(%rsp)
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1050 <SHA1_Init@plt>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <SHA1_Update@plt>
mov    0x30(%rsp),%rax
lea    (%rax,%rax,4),%eax
movslq %eax,%rbp
mov    0x20(%rsp),%rsi
add    %rbp,%rsi
mov    %r14,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <SHA1_Update@plt>
mov    0x28(%rsp),%rdi
add    %rbp,%rdi
mov    %r14,%rsi
call   1060 <SHA1_Final@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @diffuse(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%12 = load i32, i32* %6
%13 = sdiv i32 %12, 20
store i32 %13, i32* %10
%14 = load i32, i32* %6
%15 = srem i32 %14, 20
store i32 %15, i32* %11
store i32 0, i32* %7
br label %16
16: ; preds = %37, %3
%17 = load i32, i32* %7
%18 = load i32, i32* %10
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %40
20: ; preds = %16
%21 = load i32, i32* %7
%22 = call i32 @htonl(i32 %21)
store i32 %22, i32* %8
%23 = call i32 @SHA1_Init(i32* %9)
%24 = call i32 (i32*, ...) @SHA1_Update(i32* %9, i32* %8, i32 4)
%25 = load i8*, i8** %4
%26 = load i32, i32* %7
%27 = mul nsw i32 20, %26
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = call i32 (i32*, ...) @SHA1_Update(i32* %9, i8* %29, i32 20)
%31 = load i8*, i8** %5
%32 = load i32, i32* %7
%33 = mul nsw i32 20, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = call i32 @SHA1_Final(i8* %35, i32* %9)
br label %37
37: ; preds = %20
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %16
40: ; preds = %16
%41 = load i32, i32* %11
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %61
43: ; preds = %40
%44 = load i32, i32* %10
%45 = call i32 @htonl(i32 %44)
store i32 %45, i32* %8
%46 = call i32 @SHA1_Init(i32* %9)
%47 = call i32 (i32*, ...) @SHA1_Update(i32* %9, i32* %8, i32 4)
%48 = load i8*, i8** %4
%49 = load i32, i32* %10
%50 = mul nsw i32 20, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i32, i32* %11
%54 = call i32 (i32*, ...) @SHA1_Update(i32* %9, i8* %52, i32 %53)
%55 = load i8*, i8** %5
%56 = load i32, i32* %10
%57 = mul nsw i32 20, %56
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
%60 = call i32 @SHA1_Final(i8* %59, i32* %9)
br label %61
61: ; preds = %43, %40
ret i32 0
}

LLVM IR (-O2):
define i32 @diffuse(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = sdiv i32 %2, 20
%9 = srem i32 %2, 20
%10 = icmp sgt i32 %2, 19
br i1 %10, label %11, label %26
11: ; preds = %3
%12 = zext i32 %8 to i64
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ 0, %11 ], [ %24, %13 ]
%15 = trunc i64 %14 to i32
%16 = call i32 @htonl(i32 %15)
store i32 %16, i32* %4
%17 = call i32 @SHA1_Init(i32* %5)
%18 = call i32 (i32*, ...) @SHA1_Update(i32* %5, i32* %4, i32 4)
%19 = mul nuw nsw i64 %14, 20
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = call i32 (i32*, ...) @SHA1_Update(i32* %5, i8* %20, i32 20)
%22 = getelementptr inbounds i8, i8* %1, i64 %19
%23 = call i32 @SHA1_Final(i8* %22, i32* %5)
%24 = add nuw nsw i64 %14, 1
%25 = icmp eq i64 %24, %12
br i1 %25, label %26, label %13
26: ; preds = %13, %3
%27 = icmp eq i32 %9, 0
br i1 %27, label %38, label %28
28: ; preds = %26
%29 = call i32 @htonl(i32 %8)
store i32 %29, i32* %4
%30 = call i32 @SHA1_Init(i32* %5)
%31 = call i32 (i32*, ...) @SHA1_Update(i32* %5, i32* %4, i32 4)
%32 = mul nsw i32 %8, 20
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %0, i64 %33
%35 = call i32 (i32*, ...) @SHA1_Update(i32* %5, i8* %34, i32 %9)
%36 = getelementptr inbounds i8, i8* %1, i64 %33
%37 = call i32 @SHA1_Final(i8* %36, i32* %5)
br label %38
38: ; preds = %28, %26
ret i32 0
}