#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 /*<<< orphan*/ prefix;

/* Variables and functions */
int /*<<< orphan*/ ENOTSUP;
int /*<<< orphan*/ _error(char *, int);
char *_makepasswd_password(char const *, int, int);
char *crypt(char const *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char const *);
char *strdup(char *);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
char *_hash_sha512(char const *password, char const *salt) {
  const char prefix[] = "$6$";
  const char characters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                            "abcdefghijklmnopqrstuvwxyz0123456789./";
  char *ret;
  char *p = NULL;
  size_t len;
  char *s;
  if (salt == NULL) {
    if ((p = _makepasswd_password(characters, 16, 16)) == NULL)
      return NULL;
    salt = p;
  }
  len = sizeof(prefix) + strlen(salt);
  if ((s = malloc(len)) == NULL) {
    _error("malloc", 1);
    free(p);
    return NULL;
  }
  snprintf(s, len, "%s%s", prefix, salt);
  ret = crypt(password, s);
  free(p);
  free(s);
  if (ret == NULL)
    _error("crypt", 1);
  else if (strncmp(ret, prefix, sizeof(prefix) - 1) != 0) {
    errno = ENOTSUP;
    _error("sha512", 1);
    ret = NULL;
  } else if ((ret = strdup(ret)) == NULL)
    _error("malloc", 1);
  return ret;
}


x86 assembly (clang, -O0):
<_hash_sha512>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xe47(%rip),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe59(%rip),%rsi
mov    $0x41,%edx
call   1090 <memcpy@plt>
movq   $0x0,-0x70(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1217 <_hash_sha512+0x77>
lea    -0x60(%rbp),%rdi
mov    $0x10,%edx
mov    %edx,%esi
call   1040 <_makepasswd_password@plt>
mov    %rax,-0x70(%rbp)
cmp    $0x0,%rax
jne    120f <_hash_sha512+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    134e <_hash_sha512+0x1ae>
mov    -0x70(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strlen@plt>
cltq
add    $0x4,%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x80(%rbp)
cmp    $0x0,%rax
jne    1268 <_hash_sha512+0xc8>
lea    0xdbc(%rip),%rdi
mov    $0x1,%esi
call   10b0 <_error@plt>
mov    -0x70(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    134e <_hash_sha512+0x1ae>
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%rsi
lea    -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xd8c(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x80(%rbp),%rsi
call   1060 <crypt@plt>
mov    %rax,-0x68(%rbp)
mov    -0x70(%rbp),%rdi
call   1030 <free@plt>
mov    -0x80(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x68(%rbp)
jne    12c8 <_hash_sha512+0x128>
lea    0xd57(%rip),%rdi
mov    $0x1,%esi
call   10b0 <_error@plt>
jmp    1346 <_hash_sha512+0x1a6>
mov    -0x68(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x3,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     1314 <_hash_sha512+0x174>
mov    0x2cfd(%rip),%rax
mov    (%rax),%ecx
mov    0x2ce4(%rip),%rax
mov    %ecx,(%rax)
lea    0xd19(%rip),%rdi
mov    $0x1,%esi
call   10b0 <_error@plt>
movq   $0x0,-0x68(%rbp)
jmp    1341 <_hash_sha512+0x1a1>
mov    -0x68(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jne    133c <_hash_sha512+0x19c>
lea    0xcd2(%rip),%rdi
mov    $0x1,%esi
call   10b0 <_error@plt>
jmp    1341 <_hash_sha512+0x1a1>
jmp    1346 <_hash_sha512+0x1a6>
mov    -0x68(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_hash_sha512>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    %rdi,%r14
movl   $0x243624,0xc(%rsp)
movaps 0xe81(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe65(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe49(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe2d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x50(%rsp)
test   %rsi,%rsi
je     11ea <_hash_sha512+0x5a>
mov    %rsi,%r12
xor    %r15d,%r15d
jmp    120d <_hash_sha512+0x7d>
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    $0x10,%edx
call   1040 <_makepasswd_password@plt>
mov    %rax,%r15
mov    %rax,%r12
test   %rax,%rax
je     12dd <_hash_sha512+0x14d>
mov    %r12,%rdi
call   1070 <strlen@plt>
movslq %eax,%rbx
add    $0x4,%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     129b <_hash_sha512+0x10b>
mov    %rax,%r13
lea    0xe15(%rip),%rdx
lea    0xc(%rsp),%rcx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12,%r8
call   1080 <snprintf@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1060 <crypt@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     12b6 <_hash_sha512+0x126>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
mov    $0x3,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     12bf <_hash_sha512+0x12f>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    0x2d48(%rip),%rcx
mov    %eax,(%rcx)
lea    0xdba(%rip),%rdi
jmp    12d3 <_hash_sha512+0x143>
lea    0xd9f(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12dd <_hash_sha512+0x14d>
lea    0xd90(%rip),%rdi
jmp    12d3 <_hash_sha512+0x143>
mov    %r14,%rdi
call   10b0 <strdup@plt>
test   %rax,%rax
jne    12df <_hash_sha512+0x14f>
lea    0xd6e(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const._hash_sha512.prefix = [4 x i8] c"$6$\00"
@__const._hash_sha512.characters = [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./\00"
@.str = [7 x i8] c"malloc\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [6 x i8] c"crypt\00"
@ENOTSUP = global i32 0
@errno = global i32 0
@.str.3 = [7 x i8] c"sha512\00"
define i8* @_hash_sha512(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [4 x i8]
%7 = alloca [65 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = bitcast [4 x i8]* %6 to i8*
%13 = bitcast [65 x i8]* %7 to i8*
store i8* null, i8** %9
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = getelementptr inbounds [65 x i8], [65 x i8]* %7, i64 0, i64 0
%18 = call i8* @_makepasswd_password(i8* %17, i32 16, i32 16)
store i8* %18, i8** %9
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %16
store i8* null, i8** %3
br label %70
21: ; preds = %16
%22 = load i8*, i8** %9
store i8* %22, i8** %5
br label %23
23: ; preds = %21, %2
%24 = load i8*, i8** %5
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = add i64 4, %26
store i64 %27, i64* %10
%28 = load i64, i64* %10
%29 = call i8* @malloc(i64 %28)
store i8* %29, i8** %11
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %23
%32 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
%33 = load i8*, i8** %9
%34 = call i32 @free(i8* %33)
store i8* null, i8** %3
br label %70
35: ; preds = %23
%36 = load i8*, i8** %11
%37 = load i64, i64* %10
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%39 = load i8*, i8** %5
%40 = call i32 @snprintf(i8* %36, i64 %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %39)
%41 = load i8*, i8** %4
%42 = load i8*, i8** %11
%43 = call i8* @crypt(i8* %41, i8* %42)
store i8* %43, i8** %8
%44 = load i8*, i8** %9
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %11
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %8
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %35
%51 = call i32 @_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %68
52: ; preds = %35
%53 = load i8*, i8** %8
%54 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%55 = call i64 @strncmp(i8* %53, i8* %54, i32 3)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i32, i32* @ENOTSUP
store i32 %58, i32* @errno
%59 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 1)
store i8* null, i8** %8
br label %67
60: ; preds = %52
%61 = load i8*, i8** %8
%62 = call i8* @strdup(i8* %61)
store i8* %62, i8** %8
%63 = icmp eq i8* %62, null
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %66
66: ; preds = %64, %60
br label %67
67: ; preds = %66, %57
br label %68
68: ; preds = %67, %50
%69 = load i8*, i8** %8
store i8* %69, i8** %3
br label %70
70: ; preds = %68, %31, %20
%71 = load i8*, i8** %3
ret i8* %71
}

LLVM IR (-O2):
@__const._hash_sha512.characters = [65 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./\00"
@.str = [7 x i8] c"malloc\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [6 x i8] c"crypt\00"
@ENOTSUP = global i32 0
@errno = global i32 0
@.str.3 = [7 x i8] c"sha512\00"
define noalias i8* @_hash_sha512(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca [65 x i8]
%5 = bitcast i32* %3 to i8*
store i32 2373156, i32* %3
%6 = getelementptr inbounds [65 x i8], [65 x i8]* %4, i64 0, i64 0
%7 = icmp eq i8* %1, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = call i8* @_makepasswd_password(i8* %6, i32 16, i32 16)
%10 = icmp eq i8* %9, null
br i1 %10, label %41, label %11
11: ; preds = %8, %2
%12 = phi i8* [ null, %2 ], [ %9, %8 ]
%13 = phi i8* [ %1, %2 ], [ %9, %8 ]
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = add nsw i64 %15, 4
%17 = call i8* @malloc(i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
%21 = call i32 @free(i8* %12)
br label %41
22: ; preds = %11
%23 = call i32 @snprintf(i8* %17, i64 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %13)
%24 = call i8* @crypt(i8* %0, i8* %17)
%25 = call i32 @free(i8* %12)
%26 = call i32 @free(i8* %17)
%27 = icmp eq i8* %24, null
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %41
30: ; preds = %22
%31 = call i64 @strncmp(i8* %24, i8* %5, i32 3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i32, i32* @ENOTSUP
store i32 %34, i32* @errno
%35 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %41
36: ; preds = %30
%37 = call i8* @strdup(i8* %24)
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %41
41: ; preds = %28, %36, %39, %33, %8, %19
%42 = phi i8* [ null, %19 ], [ null, %8 ], [ null, %28 ], [ null, %33 ], [ null, %39 ], [ %37, %36 ]
ret i8* %42
}