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


x86 assembly (clang, -O0):
<_hash_shmd5>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xe57(%rip),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe69(%rip),%rsi
mov    $0x59,%edx
call   1080 <memcpy@plt>
movq   $0x0,-0x90(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1210 <_hash_shmd5+0x80>
lea    -0x80(%rbp),%rdi
mov    $0x8,%edx
mov    %edx,%esi
call   1040 <_makepasswd_password@plt>
mov    %rax,-0x90(%rbp)
cmp    $0x0,%rax
jne    1205 <_hash_shmd5+0x75>
movq   $0x0,-0x8(%rbp)
jmp    1320 <_hash_shmd5+0x190>
mov    -0x90(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
cltq
add    $0x4,%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0xa0(%rbp)
cmp    $0x0,%rax
jne    126d <_hash_shmd5+0xdd>
lea    0xdba(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
mov    -0x90(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1320 <_hash_shmd5+0x190>
mov    -0xa0(%rbp),%rdi
mov    -0x98(%rbp),%rsi
lea    -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xd81(%rip),%rdx
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    12e2 <_hash_shmd5+0x152>
lea    0xd3d(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
jmp    1315 <_hash_shmd5+0x185>
mov    -0x88(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1310 <_hash_shmd5+0x180>
lea    0xcfe(%rip),%rdi
mov    $0x1,%esi
call   10a0 <_error@plt>
jmp    1315 <_hash_shmd5+0x185>
mov    -0x88(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_hash_shmd5>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %rdi,%r14
movl   $0x243124,0xc(%rsp)
movups 0xeaa(%rip),%xmm0
movups %xmm0,0x59(%rsp)
movaps 0xe95(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xe79(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe5d(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe41(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe25(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
test   %rsi,%rsi
je     11ed <_hash_shmd5+0x6d>
mov    %rsi,%r12
xor    %r15d,%r15d
jmp    1210 <_hash_shmd5+0x90>
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
mov    $0x8,%edx
call   1040 <_makepasswd_password@plt>
mov    %rax,%r15
mov    %rax,%r12
test   %rax,%rax
je     12ae <_hash_shmd5+0x12e>
mov    %r12,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbx
add    $0x4,%rbx
mov    %rbx,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1282 <_hash_shmd5+0x102>
mov    %rax,%r13
lea    0xe2a(%rip),%rdx
lea    0xc(%rsp),%rcx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12,%r8
call   1070 <snprintf@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1050 <crypt@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     129d <_hash_shmd5+0x11d>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
test   %rax,%rax
jne    12b0 <_hash_shmd5+0x130>
lea    0xdd9(%rip),%rdi
jmp    12a4 <_hash_shmd5+0x124>
lea    0xdd0(%rip),%rdi
mov    $0x1,%esi
call   1090 <_error@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12ae <_hash_shmd5+0x12e>
lea    0xdc1(%rip),%rdi
mov    $0x1,%esi
call   1090 <_error@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const._hash_shmd5.prefix = [4 x i8] c"$1$\00"
@__const._hash_shmd5.characters = [89 x i8] c"!\22%&'()*+,-./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"
define i8* @_hash_shmd5(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [4 x i8]
%7 = alloca [89 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 [89 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 [89 x i8], [89 x i8]* %7, i64 0, i64 0
%18 = call i8* @_makepasswd_password(i8* %17, i32 8, i32 8)
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 %61
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 %61
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 %59
52: ; preds = %35
%53 = load i8*, i8** %8
%54 = call i8* @strdup(i8* %53)
store i8* %54, i8** %8
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %58
58: ; preds = %56, %52
br label %59
59: ; preds = %58, %50
%60 = load i8*, i8** %8
store i8* %60, i8** %3
br label %61
61: ; preds = %59, %31, %20
%62 = load i8*, i8** %3
ret i8* %62
}

LLVM IR (-O2):
@__const._hash_shmd5.characters = [89 x i8] c"!\22%&'()*+,-./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"
define noalias i8* @_hash_shmd5(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca [89 x i8]
%5 = bitcast i32* %3 to i8*
store i32 2371876, i32* %3
%6 = getelementptr inbounds [89 x i8], [89 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 8, i32 8)
%10 = icmp eq i8* %9, null
br i1 %10, label %35, 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 %35
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 %35
30: ; preds = %22
%31 = call i8* @strdup(i8* %24)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 1)
br label %35
35: ; preds = %28, %33, %30, %8, %19
%36 = phi i8* [ null, %19 ], [ null, %8 ], [ null, %28 ], [ null, %33 ], [ %31, %30 ]
ret i8* %36
}