#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_data {
  scalar_t__ initialized;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ crypt_r(char *, char *, struct crypt_data *);
int get_salt(unsigned char *, int);
int /*<<< orphan*/ memcpy(unsigned char *, ...);
int /*<<< orphan*/ memset(unsigned char *, char, int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int snis_crypt(unsigned char *shipname, unsigned char *password,
               unsigned char *crypted, int cryptsize, char *insalt,
               int insaltsize) {
  unsigned char salt[] = "$1$........";
  unsigned char buffer[50];
  struct crypt_data data;
  int rc;
  if (insalt) {
    if (insaltsize != 11)
      return -1;
    memcpy(&salt[3], &insalt[3], 8);
  } else {
    rc = get_salt(&salt[3], 8);
    if (rc < 0)
      return rc;
  }
  memset(buffer, 'x', sizeof(buffer));
  if (strlen((char *)password) > 20)
    return -1;
  if (strlen((char *)shipname) > 20)
    return -1;
  buffer[49] = '\0';
  memcpy(buffer, shipname, strlen((char *)shipname));
  memcpy(buffer + 20, password, strlen((char *)password));
  data.initialized = 0;
  password = (unsigned char *)crypt_r((char *)buffer, (char *)salt, &data);
  strncpy((char *)crypted, (char *)password, cryptsize);
  return 0;
}


x86 assembly (clang, -O0):
<snis_crypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe75(%rip),%eax
mov    %eax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d8 <snis_crypt+0x78>
cmpl   $0xb,-0x34(%rbp)
je     11b7 <snis_crypt+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <snis_crypt+0x1ad>
lea    -0x40(%rbp),%rdi
add    $0x3,%rdi
mov    -0x30(%rbp),%rsi
add    $0x3,%rsi
xor    %eax,%eax
mov    $0x8,%edx
call   1070 <memcpy@plt>
jmp    1210 <snis_crypt+0xb0>
lea    -0x40(%rbp),%rdi
add    $0x3,%rdi
mov    $0x8,%esi
call   1080 <get_salt@plt>
mov    %eax,-0x8c(%rbp)
cmpl   $0x0,-0x8c(%rbp)
jge    120b <snis_crypt+0xab>
mov    -0x8c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130d <snis_crypt+0x1ad>
jmp    1210 <snis_crypt+0xb0>
lea    -0x80(%rbp),%rdi
mov    $0x78,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x14,%eax
jle    1241 <snis_crypt+0xe1>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <snis_crypt+0x1ad>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x14,%eax
jle    125f <snis_crypt+0xff>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <snis_crypt+0x1ad>
movb   $0x0,-0x4f(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb0(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
lea    -0x80(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0xa0(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xa0(%rbp),%rdi
mov    -0x98(%rbp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
movq   $0x0,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x88(%rbp),%rdx
call   1050 <crypt_r@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<snis_crypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r12
movabs $0x2e2e2e2e2e243124,%rax
2e 2e 2e
mov    %rax,0x8(%rsp)
movl   $0x2e2e2e,0x10(%rsp)
test   %r8,%r8
je     11bf <snis_crypt+0x5f>
mov    $0xffffffff,%ebx
cmp    $0xb,%r9d
jne    1275 <snis_crypt+0x115>
lea    0xb(%rsp),%rdi
add    $0x3,%r8
mov    %r8,%rsi
mov    $0x8,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
jmp    11d6 <snis_crypt+0x76>
lea    0xb(%rsp),%rdi
mov    $0x8,%esi
call   1080 <get_salt@plt>
test   %eax,%eax
js     1273 <snis_crypt+0x113>
lea    0x20(%rsp),%rdi
mov    $0x78,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    $0xffffffff,%ebx
cmp    $0x14,%eax
jg     1275 <snis_crypt+0x115>
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    $0x14,%eax
jg     1275 <snis_crypt+0x115>
movb   $0x0,0x51(%rsp)
mov    %r12,%rdi
call   1040 <strlen@plt>
xor    %ebx,%ebx
lea    0x20(%rsp),%r13
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
lea    0x34(%rsp),%r12
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
movq   $0x0,0x18(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %r13,%rdi
call   1050 <crypt_r@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
jmp    1275 <snis_crypt+0x115>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.crypt_data = type { i64 }
@__const.snis_crypt.salt = [12 x i8] c"$1$........\00"
define i32 @snis_crypt(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca [12 x i8]
%15 = alloca [50 x i8]
%16 = alloca %struct.crypt_data
%17 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%18 = bitcast [12 x i8]* %14 to i8*
%19 = load i8*, i8** %12
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %30
21: ; preds = %6
%22 = load i32, i32* %13
%23 = icmp ne i32 %22, 11
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %7
br label %72
25: ; preds = %21
%26 = getelementptr inbounds [12 x i8], [12 x i8]* %14, i64 0, i64 3
%27 = load i8*, i8** %12
%28 = getelementptr inbounds i8, i8* %27, i64 3
%29 = call i32 (i8*, ...) @memcpy(i8* %26, i8* %28, i32 8)
br label %38
30: ; preds = %6
%31 = getelementptr inbounds [12 x i8], [12 x i8]* %14, i64 0, i64 3
%32 = call i32 @get_salt(i8* %31, i32 8)
store i32 %32, i32* %17
%33 = load i32, i32* %17
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = load i32, i32* %17
store i32 %36, i32* %7
br label %72
37: ; preds = %30
br label %38
38: ; preds = %37, %25
%39 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%40 = call i32 @memset(i8* %39, i8 signext 120, i32 50)
%41 = load i8*, i8** %9
%42 = call i32 @strlen(i8* %41)
%43 = icmp sgt i32 %42, 20
br i1 %43, label %44, label %45
44: ; preds = %38
store i32 -1, i32* %7
br label %72
45: ; preds = %38
%46 = load i8*, i8** %8
%47 = call i32 @strlen(i8* %46)
%48 = icmp sgt i32 %47, 20
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 -1, i32* %7
br label %72
50: ; preds = %45
%51 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 49
store i8 0, i8* %51
%52 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%53 = load i8*, i8** %8
%54 = load i8*, i8** %8
%55 = call i32 @strlen(i8* %54)
%56 = call i32 (i8*, ...) @memcpy(i8* %52, i8* %53, i32 %55)
%57 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%58 = getelementptr inbounds i8, i8* %57, i64 20
%59 = load i8*, i8** %9
%60 = load i8*, i8** %9
%61 = call i32 @strlen(i8* %60)
%62 = call i32 (i8*, ...) @memcpy(i8* %58, i8* %59, i32 %61)
%63 = getelementptr inbounds %struct.crypt_data, %struct.crypt_data* %16, i32 0, i32 0
store i64 0, i64* %63
%64 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%65 = getelementptr inbounds [12 x i8], [12 x i8]* %14, i64 0, i64 0
%66 = call i64 @crypt_r(i8* %64, i8* %65, %struct.crypt_data* %16)
%67 = inttoptr i64 %66 to i8*
store i8* %67, i8** %9
%68 = load i8*, i8** %10
%69 = load i8*, i8** %9
%70 = load i32, i32* %11
%71 = call i32 @strncpy(i8* %68, i8* %69, i32 %70)
store i32 0, i32* %7
br label %72
72: ; preds = %50, %49, %44, %35, %24
%73 = load i32, i32* %7
ret i32 %73
}

LLVM IR (-O2):
%struct.crypt_data = type { i64 }
@__const.snis_crypt.salt = [12 x i8] c"$1$........\00"
define i32 @snis_crypt(i8* %0, i8* %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca [12 x i8]
%8 = alloca [50 x i8]
%9 = alloca %struct.crypt_data
%10 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%12 = bitcast %struct.crypt_data* %9 to i8*
%13 = icmp eq i8* %4, null
br i1 %13, label %20, label %14
14: ; preds = %6
%15 = icmp eq i32 %5, 11
br i1 %15, label %16, label %42
16: ; preds = %14
%17 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 3
%18 = getelementptr inbounds i8, i8* %4, i64 3
%19 = call i32 (i8*, ...) @memcpy(i8* %17, i8* %18, i32 8)
br label %24
20: ; preds = %6
%21 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 3
%22 = call i32 @get_salt(i8* %21, i32 8)
%23 = icmp slt i32 %22, 0
br i1 %23, label %42, label %24
24: ; preds = %20, %16
%25 = call i32 @memset(i8* %11, i8 signext 120, i32 50)
%26 = call i32 @strlen(i8* %1)
%27 = icmp sgt i32 %26, 20
br i1 %27, label %42, label %28
28: ; preds = %24
%29 = call i32 @strlen(i8* %0)
%30 = icmp sgt i32 %29, 20
br i1 %30, label %42, label %31
31: ; preds = %28
%32 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 49
store i8 0, i8* %32
%33 = call i32 @strlen(i8* %0)
%34 = call i32 (i8*, ...) @memcpy(i8* %11, i8* %0, i32 %33)
%35 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 20
%36 = call i32 @strlen(i8* %1)
%37 = call i32 (i8*, ...) @memcpy(i8* %35, i8* %1, i32 %36)
%38 = getelementptr inbounds %struct.crypt_data, %struct.crypt_data* %9, i64 0, i32 0
store i64 0, i64* %38
%39 = call i64 @crypt_r(i8* %11, i8* %10, %struct.crypt_data* %9)
%40 = inttoptr i64 %39 to i8*
%41 = call i32 @strncpy(i8* %2, i8* %40, i32 %3)
br label %42
42: ; preds = %28, %24, %20, %14, %31
%43 = phi i32 [ 0, %31 ], [ -1, %14 ], [ %22, %20 ], [ -1, %24 ], [ -1, %28 ]
ret i32 %43
}