#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 spwd {
  char *sp_pwdp;
};
typedef int /*<<< orphan*/ password;

/* Variables and functions */
char *crypt(char *, char *);
char *fgets(char *, int, int /*<<< orphan*/);
struct spwd *getspnam(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int /*<<< orphan*/ strcmp(char *, char *);
char *strdup(char *);
int main(int argc, char *argv[]) {
  char password[100];
  struct spwd *shadow_entry;
  char *p, *correct, *supplied, *salt;
  if (argc < 2)
    return 2;
  p = fgets(password, sizeof(password), stdin);
  if (p == NULL)
    return 2;
  *p = 0;
  char username[] = "shareuser";
  shadow_entry = getspnam(username);
  if (shadow_entry == NULL)
    return 1;
  correct = shadow_entry->sp_pwdp;
  salt = strdup(correct);
  if (salt == NULL)
    return 2;
  p = strchr(salt + 1, '$');
  if (p == NULL)
    return 2;
  p = strchr(salt + 1, '$');
  if (p == NULL)
    return 2;
  p[1] = 0;
  printf("salt:%s", salt);
  supplied = crypt(password, salt);
  if (supplied == NULL)
    return 2;
  return !!strcmp(supplied, correct);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119f <main+0x2f>
movl   $0x2,-0x4(%rbp)
jmp    1348 <main+0x1d8>
lea    -0x80(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
jne    11d7 <main+0x67>
movl   $0x2,-0x4(%rbp)
jmp    1348 <main+0x1d8>
mov    -0x90(%rbp),%rax
movb   $0x0,(%rax)
mov    0xe18(%rip),%rax
mov    %rax,-0xb2(%rbp)
mov    0xe12(%rip),%ax
mov    %ax,-0xaa(%rbp)
lea    -0xb2(%rbp),%rdi
call   1080 <getspnam@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    122a <main+0xba>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x1d8>
mov    -0x88(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jne    1268 <main+0xf8>
movl   $0x2,-0x4(%rbp)
jmp    1348 <main+0x1d8>
mov    -0xa8(%rbp),%rdi
add    $0x1,%rdi
mov    $0x24,%esi
call   1040 <strchr@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
jne    129e <main+0x12e>
movl   $0x2,-0x4(%rbp)
jmp    1348 <main+0x1d8>
mov    -0xa8(%rbp),%rdi
add    $0x1,%rdi
mov    $0x24,%esi
call   1040 <strchr@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
jne    12d4 <main+0x164>
movl   $0x2,-0x4(%rbp)
jmp    1348 <main+0x1d8>
mov    -0x90(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0xa8(%rbp),%rsi
lea    0xd1d(%rip),%rdi
call   1050 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
call   1030 <crypt@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jne    1323 <main+0x1b3>
movl   $0x2,-0x4(%rbp)
jmp    1348 <main+0x1d8>
mov    -0xa0(%rbp),%rdi
mov    -0x98(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    $0x2,%ebx
cmp    $0x2,%edi
jl     1239 <main+0xc9>
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1239 <main+0xc9>
movb   $0x0,(%rax)
movabs $0x6573756572616873,%rax
75 73 65
mov    %rax,(%rsp)
movw   $0x72,0x8(%rsp)
mov    %rsp,%rdi
call   1080 <getspnam@plt>
test   %rax,%rax
je     1234 <main+0xc4>
mov    (%rax),%r14
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    $0x2,%ebx
test   %rax,%rax
je     1239 <main+0xc9>
mov    %rax,%r15
lea    0x1(%rax),%rdi
mov    $0x24,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1239 <main+0xc9>
movb   $0x0,0x1(%rax)
lea    0xe04(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
mov    %r15,%rsi
call   1030 <crypt@plt>
test   %rax,%rax
je     1239 <main+0xc9>
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
xor    %ebx,%ebx
test   %eax,%eax
setne  %bl
jmp    1239 <main+0xc9>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.spwd = type { i8* }
@stdin = global i32 0
@__const.main.username = [10 x i8] c"shareuser\00"
@.str = [8 x i8] c"salt:%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [100 x i8]
%7 = alloca %struct.spwd*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [10 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 2, i32* %3
br label %73
16: ; preds = %2
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* @stdin
%19 = call i8* @fgets(i8* %17, i32 100, i32 %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 2, i32* %3
br label %73
23: ; preds = %16
%24 = load i8*, i8** %8
store i8 0, i8* %24
%25 = bitcast [10 x i8]* %12 to i8*
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %12, i64 0, i64 0
%27 = call %struct.spwd* @getspnam(i8* %26)
store %struct.spwd* %27, %struct.spwd** %7
%28 = load %struct.spwd*, %struct.spwd** %7
%29 = icmp eq %struct.spwd* %28, null
br i1 %29, label %30, label %31
30: ; preds = %23
store i32 1, i32* %3
br label %73
31: ; preds = %23
%32 = load %struct.spwd*, %struct.spwd** %7
%33 = getelementptr inbounds %struct.spwd, %struct.spwd* %32, i32 0, i32 0
%34 = load i8*, i8** %33
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = call i8* @strdup(i8* %35)
store i8* %36, i8** %11
%37 = load i8*, i8** %11
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %31
store i32 2, i32* %3
br label %73
40: ; preds = %31
%41 = load i8*, i8** %11
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = call i8* @strchr(i8* %42, i8 signext 36)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 2, i32* %3
br label %73
47: ; preds = %40
%48 = load i8*, i8** %11
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = call i8* @strchr(i8* %49, i8 signext 36)
store i8* %50, i8** %8
%51 = load i8*, i8** %8
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %54
53: ; preds = %47
store i32 2, i32* %3
br label %73
54: ; preds = %47
%55 = load i8*, i8** %8
%56 = getelementptr inbounds i8, i8* %55, i64 1
store i8 0, i8* %56
%57 = load i8*, i8** %11
%58 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %57)
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%60 = load i8*, i8** %11
%61 = call i8* @crypt(i8* %59, i8* %60)
store i8* %61, i8** %10
%62 = load i8*, i8** %10
%63 = icmp eq i8* %62, null
br i1 %63, label %64, label %65
64: ; preds = %54
store i32 2, i32* %3
br label %73
65: ; preds = %54
%66 = load i8*, i8** %10
%67 = load i8*, i8** %9
%68 = call i32 @strcmp(i8* %66, i8* %67)
%69 = icmp ne i32 %68, 0
%70 = xor i1 %69, true
%71 = xor i1 %70, true
%72 = zext i1 %71 to i32
store i32 %72, i32* %3
br label %73
73: ; preds = %65, %64, %53, %46, %39, %30, %22, %15
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.spwd = type { i8* }
@stdin = global i32 0
@__const.main.username = [10 x i8] c"shareuser\00"
@.str = [8 x i8] c"salt:%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = alloca [10 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = icmp slt i32 %0, 2
br i1 %6, label %35, label %7
7: ; preds = %2
%8 = load i32, i32* @stdin
%9 = call i8* @fgets(i8* %5, i32 100, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %35, label %11
11: ; preds = %7
store i8 0, i8* %9
%12 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%13 = call %struct.spwd* @getspnam(i8* %12)
%14 = icmp eq %struct.spwd* %13, null
br i1 %14, label %33, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.spwd, %struct.spwd* %13, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = call i8* @strdup(i8* %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %33, label %20
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %18, i64 1
%22 = call i8* @strchr(i8* %21, i8 signext 36)
%23 = icmp eq i8* %22, null
br i1 %23, label %33, label %24
24: ; preds = %20
%25 = getelementptr inbounds i8, i8* %22, i64 1
store i8 0, i8* %25
%26 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %18)
%27 = call i8* @crypt(i8* %5, i8* %18)
%28 = icmp eq i8* %27, null
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = call i32 @strcmp(i8* %27, i8* dereferenceable(1) %17)
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i32
br label %33
33: ; preds = %24, %20, %15, %11, %29
%34 = phi i32 [ %32, %29 ], [ 1, %11 ], [ 2, %15 ], [ 2, %20 ], [ 2, %24 ]
br label %35
35: ; preds = %7, %2, %33
%36 = phi i32 [ %34, %33 ], [ 2, %2 ], [ 2, %7 ]
ret i32 %36
}