#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*/ _error(char *, int);
char *_makepasswd_password(char const *, int, int);
char *crypt(char const *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int rand();
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, int, char,
                            char const *);
char *strdup(char *);
int strlen(char const *);
char *_hash_sha1(char const *password, char const *salt, int iterations) {
  const char prefix[] = "$sha1$";
  const char characters[] =
      "!\"%&'()*+,-./ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      "abcdefghijklmnopqrstuvwxyz0123456789:;<=>?@[\\]^_`";
  char *ret;
  char *p = NULL;
  size_t len;
  char *s;
  if (iterations <= 0)
    iterations = rand() % (1 << 18);
  if (salt == NULL) {
    if ((p = _makepasswd_password(characters, 8, 64)) == NULL)
      return NULL;
    salt = p;
  }
  len = sizeof(prefix) + 11 + strlen(salt) + 1;
  if ((s = malloc(len)) == NULL) {
    _error("malloc", 1);
    free(p);
    return NULL;
  }
  snprintf(s, len, "%s%u%c%s", prefix, iterations, '$', salt);
  ret = crypt(password, s);
  free(p);
  free(s);
  if (ret == NULL)
    _error("crypt", 1);
  else if ((ret = strdup(ret)) == NULL)
    _error("malloc", 1);
  return ret;
}


x86 assembly (clang, -O0):
<_hash_sha1>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0xe44(%rip),%eax
mov    %eax,-0x23(%rbp)
mov    0xe3e(%rip),%ax
mov    %ax,-0x1f(%rbp)
mov    0xe36(%rip),%al
mov    %al,-0x1d(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe42(%rip),%rsi
mov    $0x59,%edx
call   1080 <memcpy@plt>
movq   $0x0,-0x90(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     120f <_hash_sha1+0x6f>
mov    $0x0,%al
call   10c0 <rand@plt>
mov    $0x40000,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1256 <_hash_sha1+0xb6>
lea    -0x80(%rbp),%rdi
mov    $0x8,%esi
mov    $0x40,%edx
call   1040 <_makepasswd_password@plt>
mov    %rax,-0x90(%rbp)
cmp    $0x0,%rax
jne    124b <_hash_sha1+0xab>
movq   $0x0,-0x8(%rbp)
jmp    1378 <_hash_sha1+0x1d8>
mov    -0x90(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
cltq
add    $0x12,%rax
add    $0x1,%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0xa0(%rbp)
cmp    $0x0,%rax
jne    12b7 <_hash_sha1+0x117>
lea    0xd73(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
mov    -0x90(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1378 <_hash_sha1+0x1d8>
mov    -0xa0(%rbp),%rdi
mov    -0x98(%rbp),%rsi
lea    -0x23(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    -0x18(%rbp),%rax
lea    0xd36(%rip),%rdx
mov    $0x24,%r9d
mov    %rax,(%rsp)
call   1070 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0xa0(%rbp),%rsi
call   1050 <crypt@plt>
mov    %rax,-0x88(%rbp)
mov    -0x90(%rbp),%rdi
call   1030 <free@plt>
mov    -0xa0(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x88(%rbp)
jne    133a <_hash_sha1+0x19a>
lea    0xcec(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
jmp    136d <_hash_sha1+0x1cd>
mov    -0x88(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1368 <_hash_sha1+0x1c8>
lea    0xca9(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
jmp    136d <_hash_sha1+0x1cd>
mov    -0x88(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_hash_sha1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%r14
movl   $0x243161,0xb(%rsp)
movl   $0x61687324,0x8(%rsp)
61
movups 0xeab(%rip),%xmm0
movups %xmm0,0x59(%rsp)
movaps 0xe96(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xe7a(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe5e(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe42(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe26(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
test   %edx,%edx
jle    120d <_hash_sha1+0x7d>
test   %r15,%r15
je     1232 <_hash_sha1+0xa2>
xor    %r12d,%r12d
jmp    1255 <_hash_sha1+0xc5>
xor    %eax,%eax
call   10b0 <rand@plt>
mov    %eax,%r13d
lea    0x3ffff(%r13),%eax
test   %r13d,%r13d
cmovns %r13d,%eax
and    $0xfffc0000,%eax
sub    %eax,%r13d
test   %r15,%r15
jne    1208 <_hash_sha1+0x78>
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
mov    $0x40,%edx
call   1040 <_makepasswd_password@plt>
mov    %rax,%r12
mov    %rax,%r15
test   %rax,%rax
je     12fd <_hash_sha1+0x16d>
mov    %r15,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbp
add    $0x13,%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     12d1 <_hash_sha1+0x141>
mov    %rax,%rbx
mov    %r15,(%rsp)
lea    0xd8f(%rip),%rdx
lea    0x8(%rsp),%rcx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r13d,%r8d
mov    $0x24,%r9d
call   1070 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <crypt@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %rbp,%rbp
je     12ec <_hash_sha1+0x15c>
mov    %rbp,%rdi
call   10a0 <strdup@plt>
test   %rax,%rax
jne    12ff <_hash_sha1+0x16f>
lea    0xd38(%rip),%rdi
jmp    12f3 <_hash_sha1+0x163>
lea    0xd2f(%rip),%rdi
mov    $0x1,%esi
call   1090 <_error@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    12fd <_hash_sha1+0x16d>
lea    0xd24(%rip),%rdi
mov    $0x1,%esi
call   1090 <_error@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@__const._hash_sha1.prefix = [7 x i8] c"$sha1$\00"
@__const._hash_sha1.characters = [89 x i8] c"!\22%&'()*+,-./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:;<=>?@[\\]^_`\00"
@.str = [7 x i8] c"malloc\00"
@.str.1 = [9 x i8] c"%s%u%c%s\00"
@.str.2 = [6 x i8] c"crypt\00"
define noalias i8* @_hash_sha1(i8* %0, i8* %1, i32 %2) {
%4 = alloca [7 x i8]
%5 = alloca [89 x i8]
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [89 x i8], [89 x i8]* %5, i64 0, i64 0
%8 = icmp slt i32 %2, 1
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 (...) @rand()
%11 = srem i32 %10, 262144
br label %12
12: ; preds = %9, %3
%13 = phi i32 [ %11, %9 ], [ %2, %3 ]
%14 = icmp eq i8* %1, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i8* @_makepasswd_password(i8* %7, i32 8, i32 64)
%17 = icmp eq i8* %16, null
br i1 %17, label %42, label %18
18: ; preds = %15, %12
%19 = phi i8* [ null, %12 ], [ %16, %15 ]
%20 = phi i8* [ %1, %12 ], [ %16, %15 ]
%21 = call i32 @strlen(i8* %20)
%22 = sext i32 %21 to i64
%23 = add nsw i64 %22, 19
%24 = call i8* @malloc(i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
%28 = call i32 @free(i8* %19)
br label %42
29: ; preds = %18
%30 = call i32 @snprintf(i8* %24, i64 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %6, i32 %13, i8 signext 36, i8* %20)
%31 = call i8* @crypt(i8* %0, i8* %24)
%32 = call i32 @free(i8* %19)
%33 = call i32 @free(i8* %24)
%34 = icmp eq i8* %31, null
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %42
37: ; preds = %29
%38 = call i8* @strdup(i8* %31)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %42
42: ; preds = %35, %40, %37, %15, %26
%43 = phi i8* [ null, %26 ], [ null, %15 ], [ null, %35 ], [ null, %40 ], [ %38, %37 ]
ret i8* %43
}