#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 */
unsigned int AES_BLOCK_SIZE;
int /*<<< orphan*/ SHA1(unsigned char *, unsigned int, unsigned char *);
int SHA_DIGEST_LENGTH;
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, unsigned int);
void crypt_aescbc_getivec(unsigned char *ivec, unsigned int iveclen,
                          unsigned char *ivecbuf) {
  unsigned char hashbuf[SHA_DIGEST_LENGTH];
  if (!ivec || iveclen < 1)
    memset(ivecbuf, 0, AES_BLOCK_SIZE);
  else {
    if (iveclen >= AES_BLOCK_SIZE) {
      memcpy(ivecbuf, ivec, AES_BLOCK_SIZE);
    } else {
      SHA1(ivec, iveclen, hashbuf);
      memset(ivecbuf, 0, AES_BLOCK_SIZE);
      memcpy(ivecbuf, hashbuf,
             (SHA_DIGEST_LENGTH < AES_BLOCK_SIZE) ? SHA_DIGEST_LENGTH
                                                  : AES_BLOCK_SIZE);
    }
  }
}


x86 assembly (clang, -O0):
<crypt_aescbc_getivec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1184 <crypt_aescbc_getivec+0x54>
cmpl   $0x1,-0xc(%rbp)
jae    119d <crypt_aescbc_getivec+0x6d>
mov    -0x18(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <memset@plt>
jmp    1240 <crypt_aescbc_getivec+0x110>
mov    -0xc(%rbp),%eax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%eax
jb     11ca <crypt_aescbc_getivec+0x9a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
call   1040 <memcpy@plt>
jmp    123b <crypt_aescbc_getivec+0x10b>
mov    -0x30(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <SHA1@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%eax
jae    121f <crypt_aescbc_getivec+0xef>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    122b <crypt_aescbc_getivec+0xfb>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
call   1040 <memcpy@plt>
jmp    1240 <crypt_aescbc_getivec+0x110>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_aescbc_getivec>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    0x2e9b(%rip),%r15
mov    (%r15),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    0x2e88(%rip),%r12
mov    (%r12),%edx
test   %rdi,%rdi
je     11b1 <crypt_aescbc_getivec+0x81>
test   %esi,%esi
je     11b1 <crypt_aescbc_getivec+0x81>
mov    %rdi,%rax
cmp    %esi,%edx
jbe    11c7 <crypt_aescbc_getivec+0x97>
mov    %rax,%rdi
mov    %rbx,%rdx
call   1050 <SHA1@plt>
mov    (%r12),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    (%r15),%eax
mov    (%r12),%edx
cmp    %edx,%eax
cmovb  %eax,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <memcpy@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
xor    %esi,%esi
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <memset@plt>
mov    %r14,%rdi
mov    %rax,%rsi
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <memcpy@plt>

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
@AES_BLOCK_SIZE = global i32 0
define void @crypt_aescbc_getivec(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i32, i32* @SHA_DIGEST_LENGTH
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %7
%12 = alloca i8, i64 %10
store i64 %10, i64* %8
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* %5
%17 = icmp ult i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %15, %3
%19 = load i8*, i8** %6
%20 = load i32, i32* @AES_BLOCK_SIZE
%21 = call i32 @memset(i8* %19, i32 0, i32 %20)
br label %50
22: ; preds = %15
%23 = load i32, i32* %5
%24 = load i32, i32* @AES_BLOCK_SIZE
%25 = icmp uge i32 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = load i32, i32* @AES_BLOCK_SIZE
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
br label %49
31: ; preds = %22
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = call i32 @SHA1(i8* %32, i32 %33, i8* %12)
%35 = load i8*, i8** %6
%36 = load i32, i32* @AES_BLOCK_SIZE
%37 = call i32 @memset(i8* %35, i32 0, i32 %36)
%38 = load i8*, i8** %6
%39 = load i32, i32* @SHA_DIGEST_LENGTH
%40 = load i32, i32* @AES_BLOCK_SIZE
%41 = icmp ult i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %31
%43 = load i32, i32* @SHA_DIGEST_LENGTH
br label %46
44: ; preds = %31
%45 = load i32, i32* @AES_BLOCK_SIZE
br label %46
46: ; preds = %44, %42
%47 = phi i32 [ %43, %42 ], [ %45, %44 ]
%48 = call i32 @memcpy(i8* %38, i8* %12, i32 %47)
br label %49
49: ; preds = %46, %26
br label %50
50: ; preds = %49, %18
%51 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@AES_BLOCK_SIZE = global i32 0
define void @crypt_aescbc_getivec(i8* %0, i32 %1, i8* %2) {
%4 = load i32, i32* @SHA_DIGEST_LENGTH
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp eq i8* %0, null
%8 = icmp eq i32 %1, 0
%9 = or i1 %7, %8
%10 = load i32, i32* @AES_BLOCK_SIZE
br i1 %9, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @memset(i8* %2, i32 0, i32 %10)
br label %26
13: ; preds = %3
%14 = icmp ugt i32 %10, %1
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = tail call i32 @memcpy(i8* %2, i8* %0, i32 %10)
br label %26
17: ; preds = %13
%18 = call i32 @SHA1(i8* %0, i32 %1, i8* %6)
%19 = load i32, i32* @AES_BLOCK_SIZE
%20 = call i32 @memset(i8* %2, i32 0, i32 %19)
%21 = load i32, i32* @SHA_DIGEST_LENGTH
%22 = load i32, i32* @AES_BLOCK_SIZE
%23 = icmp ult i32 %21, %22
%24 = select i1 %23, i32 %21, i32 %22
%25 = call i32 @memcpy(i8* %2, i8* %6, i32 %24)
br label %26
26: ; preds = %15, %17, %11
ret void
}