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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcmp(char *, char *);
int verify_hash(char *, char *);
int verify_password(char *login, char *given_password) {
  FILE *f = fopen("/etc/shadow", "r");
  if (!f)
    return 1;
  char buffer[4096];
  while (fgets(buffer, 4095, f)) {
    char *col = strchr(buffer, ':');
    if (!col)
      continue;
    *col = 0;
    if (!strcmp(buffer, login)) {
      fclose(f);
      if (!given_password)
        return 0;
      char *stored_password = col + 1;
      char *delim = strchr(stored_password, ':');
      if (!delim)
        delim = strchr(stored_password, '\n');
      if (delim)
        *delim = 0;
      return verify_hash(given_password, stored_password);
    }
  }
  fclose(f);
  return 2;
}


x86 assembly (clang, -O0):
<verify_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <verify_password+0x41>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <verify_password+0x168>
jmp    11a6 <verify_password+0x46>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0xfff,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12b8 <verify_password+0x158>
lea    -0x1020(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    11f0 <verify_password+0x90>
jmp    11a6 <verify_password+0x46>
mov    -0x1028(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12b3 <verify_password+0x153>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1233 <verify_password+0xd3>
movl   $0x0,-0x4(%rbp)
jmp    12c8 <verify_password+0x168>
mov    -0x1028(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jne    1283 <verify_password+0x123>
mov    -0x1030(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
je     129b <verify_password+0x13b>
mov    -0x1038(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x1030(%rbp),%rsi
call   1060 <verify_hash@plt>
mov    %eax,-0x4(%rbp)
jmp    12c8 <verify_password+0x168>
jmp    11a6 <verify_password+0x46>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_password>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe83(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1250 <verify_password+0xf0>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0xfff,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1241 <verify_password+0xe1>
mov    %rsp,%r13
jmp    11d5 <verify_password+0x75>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    $0xfff,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1241 <verify_password+0xe1>
mov    %r13,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11c0 <verify_password+0x60>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    11c0 <verify_password+0x60>
mov    %r12,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     1266 <verify_password+0x106>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    1231 <verify_password+0xd1>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1234 <verify_password+0xd4>
movb   $0x0,(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <verify_hash@plt>
jmp    1255 <verify_password+0xf5>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    $0x2,%eax
jmp    1255 <verify_password+0xf5>
mov    $0x1,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %eax,%eax
jmp    1255 <verify_password+0xf5>

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/shadow\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @verify_password(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [4096 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %2
store i32 1, i32* %3
br label %62
15: ; preds = %2
br label %16
16: ; preds = %58, %26, %15
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%18 = load i32*, i32** %6
%19 = call i64 @fgets(i8* %17, i32 4095, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %59
21: ; preds = %16
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%23 = call i8* @strchr(i8* %22, i8 signext 58)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %21
br label %16
27: ; preds = %21
%28 = load i8*, i8** %8
store i8 0, i8* %28
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%30 = load i8*, i8** %4
%31 = call i32 @strcmp(i8* %29, i8* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %58, label %33
33: ; preds = %27
%34 = load i32*, i32** %6
%35 = call i32 @fclose(i32* %34)
%36 = load i8*, i8** %5
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %33
store i32 0, i32* %3
br label %62
39: ; preds = %33
%40 = load i8*, i8** %8
%41 = getelementptr inbounds i8, i8* %40, i64 1
store i8* %41, i8** %9
%42 = load i8*, i8** %9
%43 = call i8* @strchr(i8* %42, i8 signext 58)
store i8* %43, i8** %10
%44 = load i8*, i8** %10
%45 = icmp ne i8* %44, null
br i1 %45, label %49, label %46
46: ; preds = %39
%47 = load i8*, i8** %9
%48 = call i8* @strchr(i8* %47, i8 signext 10)
store i8* %48, i8** %10
br label %49
49: ; preds = %46, %39
%50 = load i8*, i8** %10
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = load i8*, i8** %10
store i8 0, i8* %53
br label %54
54: ; preds = %52, %49
%55 = load i8*, i8** %5
%56 = load i8*, i8** %9
%57 = call i32 @verify_hash(i8* %55, i8* %56)
store i32 %57, i32* %3
br label %62
58: ; preds = %27
br label %16
59: ; preds = %16
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 2, i32* %3
br label %62
62: ; preds = %59, %54, %38, %14
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/shadow\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @verify_password(i8* nocapture %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %37, label %6
6: ; preds = %2
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%8 = call i64 @fgets(i8* %7, i32 4095, i32* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %33, label %10
10: ; preds = %6, %30
%11 = call i8* @strchr(i8* %7, i8 signext 58)
%12 = icmp eq i8* %11, null
br i1 %12, label %30, label %13
13: ; preds = %10
store i8 0, i8* %11
%14 = call i32 @strcmp(i8* %7, i8* dereferenceable(1) %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = call i32 @fclose(i32* %4)
%18 = icmp eq i8* %1, null
br i1 %18, label %35, label %19
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %11, i64 1
%21 = call i8* @strchr(i8* %20, i8 signext 58)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i8* @strchr(i8* %20, i8 signext 10)
%25 = icmp eq i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %19, %23
%27 = phi i8* [ %24, %23 ], [ %21, %19 ]
store i8 0, i8* %27
br label %28
28: ; preds = %26, %23
%29 = call i32 @verify_hash(i8* %1, i8* %20)
br label %35
30: ; preds = %13, %10
%31 = call i64 @fgets(i8* %7, i32 4095, i32* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %10
33: ; preds = %30, %6
%34 = call i32 @fclose(i32* %4)
br label %35
35: ; preds = %16, %28, %33
%36 = phi i32 [ 2, %33 ], [ 0, %16 ], [ %29, %28 ]
br label %37
37: ; preds = %2, %35
%38 = phi i32 [ %36, %35 ], [ 1, %2 ]
ret i32 %38
}