#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 */
struct crypt_hash {
  int dummy;
};

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ crypt_hash_destroy(struct crypt_hash *);
int crypt_hash_final(struct crypt_hash *, char *, size_t);
scalar_t__ crypt_hash_init(struct crypt_hash **, char const *);
int crypt_hash_write(struct crypt_hash *, char const *, size_t);
int hash_buf(const char *src, size_t src_len, char *dst, size_t dst_len,
             const char *hash_name) {
  struct crypt_hash *hd = NULL;
  int r;
  if (crypt_hash_init(&hd, hash_name))
    return -EINVAL;
  r = crypt_hash_write(hd, src, src_len);
  if (!r)
    r = crypt_hash_final(hd, dst, dst_len);
  crypt_hash_destroy(hd);
  return r;
}


x86 assembly (clang, -O0):
<hash_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdi
call   1050 <crypt_hash_init@plt>
cmp    $0x0,%rax
je     118e <hash_buf+0x4e>
xor    %eax,%eax
mov    0x2e5c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <hash_buf+0x8f>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <crypt_hash_write@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11c0 <hash_buf+0x80>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <crypt_hash_final@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <crypt_hash_destroy@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    %rsp,%rdi
mov    %r8,%rsi
call   1050 <crypt_hash_init@plt>
test   %rax,%rax
je     1178 <hash_buf+0x38>
mov    0x2e6e(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    11a7 <hash_buf+0x67>
mov    (%rsp),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1060 <crypt_hash_write@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    119e <hash_buf+0x5e>
mov    (%rsp),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <crypt_hash_final@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1030 <crypt_hash_destroy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.crypt_hash = type { i32 }
@EINVAL = global i32 0
define i32 @hash_buf(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca %struct.crypt_hash*
%13 = alloca i32
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i8* %4, i8** %11
store %struct.crypt_hash* null, %struct.crypt_hash** %12
%14 = load i8*, i8** %11
%15 = call i64 @crypt_hash_init(%struct.crypt_hash** %12, i8* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = load i32, i32* @EINVAL
%19 = sub nsw i32 0, %18
store i32 %19, i32* %6
br label %36
20: ; preds = %5
%21 = load %struct.crypt_hash*, %struct.crypt_hash** %12
%22 = load i8*, i8** %7
%23 = load i64, i64* %8
%24 = call i32 @crypt_hash_write(%struct.crypt_hash* %21, i8* %22, i64 %23)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = icmp ne i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %20
%28 = load %struct.crypt_hash*, %struct.crypt_hash** %12
%29 = load i8*, i8** %9
%30 = load i64, i64* %10
%31 = call i32 @crypt_hash_final(%struct.crypt_hash* %28, i8* %29, i64 %30)
store i32 %31, i32* %13
br label %32
32: ; preds = %27, %20
%33 = load %struct.crypt_hash*, %struct.crypt_hash** %12
%34 = call i32 @crypt_hash_destroy(%struct.crypt_hash* %33)
%35 = load i32, i32* %13
store i32 %35, i32* %6
br label %36
36: ; preds = %32, %17
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
%struct.crypt_hash = type { i32 }
@EINVAL = global i32 0
define i32 @hash_buf(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4) {
%6 = alloca %struct.crypt_hash*
%7 = bitcast %struct.crypt_hash** %6 to i8*
store %struct.crypt_hash* null, %struct.crypt_hash** %6
%8 = call i64 @crypt_hash_init(%struct.crypt_hash** %6, i8* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = load i32, i32* @EINVAL
%12 = sub nsw i32 0, %11
br label %24
13: ; preds = %5
%14 = load %struct.crypt_hash*, %struct.crypt_hash** %6
%15 = call i32 @crypt_hash_write(%struct.crypt_hash* %14, i8* %0, i64 %1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load %struct.crypt_hash*, %struct.crypt_hash** %6
%19 = call i32 @crypt_hash_final(%struct.crypt_hash* %18, i8* %2, i64 %3)
br label %20
20: ; preds = %17, %13
%21 = phi i32 [ %15, %13 ], [ %19, %17 ]
%22 = load %struct.crypt_hash*, %struct.crypt_hash** %6
%23 = call i32 @crypt_hash_destroy(%struct.crypt_hash* %22)
br label %24
24: ; preds = %20, %10
%25 = phi i32 [ %12, %10 ], [ %21, %20 ]
ret i32 %25
}