#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 passwd {
  char *pw_dir;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
int EVP_PKEY_cmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ *ssh1_line_to_key(char *);
int /*<<< orphan*/ *ssh2_line_to_key(char *);
int /*<<< orphan*/ *ssh_nistp_line_to_key(char *);
scalar_t__ strncmp(char *, char *, int);
extern int match_user_openssh(EVP_PKEY *authkey, const char *login) {
  char filename[PATH_MAX];
  char line[16384];
  struct passwd *pw;
  int found;
  FILE *file;
  pw = getpwnam(login);
  if (!pw || !pw->pw_dir)
    return -1;
  snprintf(filename, PATH_MAX, "%s/.ssh/authorized_keys", pw->pw_dir);
  file = fopen(filename, "r");
  if (!file)
    return -1;
  found = 0;
  do {
    EVP_PKEY *key = NULL;
    char *cp;
    if (!fgets(line, sizeof line, file))
      break;
    for (cp = line; *cp == ' ' || *cp == '\t'; cp++) {
    }
    if (!*cp || *cp == '\n' || *cp == '#') {
      continue;
    }
    if (*cp >= '0' && *cp <= '9') {
      key = ssh1_line_to_key(cp);
    } else if (strncmp("ssh-rsa", cp, 7) == 0) {
      key = ssh2_line_to_key(cp);
    } else if (strncmp("ecdsa-sha2-nistp", cp, 16) == 0) {
      key = ssh_nistp_line_to_key(cp);
    }
    if (key == NULL)
      continue;
    if (1 == EVP_PKEY_cmp(authkey, key)) {
      found = 1;
    }
    EVP_PKEY_free(key);
  } while (found == 0);
  fclose(file);
  return found;
}


x86 assembly (clang, -O0):
<match_user_openssh>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4070,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x4068(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <getpwnam@plt>
mov    %rax,-0x4038(%rbp)
cmpq   $0x0,-0x4038(%rbp)
je     1221 <match_user_openssh+0x71>
mov    -0x4038(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1237 <match_user_openssh+0x87>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x404c(%rbp)
jmp    14aa <match_user_openssh+0x2fa>
mov    -0x4068(%rbp),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    -0x4038(%rbp),%rax
mov    (%rax),%rcx
lea    0xda8(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x4068(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x4048(%rbp)
cmpq   $0x0,-0x4048(%rbp)
jne    129b <match_user_openssh+0xeb>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x404c(%rbp)
jmp    14aa <match_user_openssh+0x2fa>
movl   $0x0,-0x403c(%rbp)
movq   $0x0,-0x4058(%rbp)
lea    -0x4030(%rbp),%rdi
mov    -0x4048(%rbp),%rdx
mov    $0x4000,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    12d6 <match_user_openssh+0x126>
jmp    148b <match_user_openssh+0x2db>
lea    -0x4030(%rbp),%rax
mov    %rax,-0x4060(%rbp)
mov    -0x4060(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x4069(%rbp)
je     1315 <match_user_openssh+0x165>
mov    -0x4060(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x4069(%rbp)
mov    -0x4069(%rbp),%al
test   $0x1,%al
jne    1328 <match_user_openssh+0x178>
jmp    1344 <match_user_openssh+0x194>
jmp    132d <match_user_openssh+0x17d>
mov    -0x4060(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x4060(%rbp)
jmp    12e4 <match_user_openssh+0x134>
mov    -0x4060(%rbp),%rax
cmpb   $0x0,(%rax)
je     137a <match_user_openssh+0x1ca>
mov    -0x4060(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     137a <match_user_openssh+0x1ca>
mov    -0x4060(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x23,%eax
jne    137f <match_user_openssh+0x1cf>
jmp    147e <match_user_openssh+0x2ce>
mov    -0x4060(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     13bd <match_user_openssh+0x20d>
mov    -0x4060(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     13bd <match_user_openssh+0x20d>
mov    -0x4060(%rbp),%rdi
call   10d0 <ssh1_line_to_key@plt>
mov    %rax,-0x4058(%rbp)
jmp    1436 <match_user_openssh+0x286>
mov    -0x4060(%rbp),%rsi
lea    0xc4f(%rip),%rdi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    13f7 <match_user_openssh+0x247>
mov    -0x4060(%rbp),%rdi
call   10c0 <ssh2_line_to_key@plt>
mov    %rax,-0x4058(%rbp)
jmp    1431 <match_user_openssh+0x281>
mov    -0x4060(%rbp),%rsi
lea    0xc1d(%rip),%rdi
mov    $0x10,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    142c <match_user_openssh+0x27c>
mov    -0x4060(%rbp),%rdi
call   1090 <ssh_nistp_line_to_key@plt>
mov    %rax,-0x4058(%rbp)
jmp    1431 <match_user_openssh+0x281>
jmp    1436 <match_user_openssh+0x286>
cmpq   $0x0,-0x4058(%rbp)
jne    1449 <match_user_openssh+0x299>
jmp    147e <match_user_openssh+0x2ce>
mov    -0x10(%rbp),%rdi
mov    -0x4058(%rbp),%rsi
call   10a0 <EVP_PKEY_cmp@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1472 <match_user_openssh+0x2c2>
movl   $0x1,-0x403c(%rbp)
mov    -0x4058(%rbp),%rdi
call   1050 <EVP_PKEY_free@plt>
cmpl   $0x0,-0x403c(%rbp)
je     12a5 <match_user_openssh+0xf5>
mov    -0x4048(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x403c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x404c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_user_openssh>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4008,%rsp
mov    %rdi,%r12
mov    0x2e22(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsi,%rdi
call   1080 <getpwnam@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     135f <match_user_openssh+0x1af>
mov    (%rax),%rcx
test   %rcx,%rcx
je     135f <match_user_openssh+0x1af>
mov    %r12,-0x30(%rbp)
mov    (%r14),%esi
lea    0xded(%rip),%rdx
mov    %rbx,%rdi
call   1060 <snprintf@plt>
lea    0xdf6(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     135f <match_user_openssh+0x1af>
mov    %rax,%r12
lea    -0x4030(%rbp),%rbx
movabs $0x100000200,%r14
jmp    126c <match_user_openssh+0xbc>
nopl   0x0(%rax)
movabs $0x800000401,%rcx
bt     %rax,%rcx
jae    12b0 <match_user_openssh+0x100>
xor    %r13d,%r13d
test   %r13d,%r13d
jne    134c <match_user_openssh+0x19c>
mov    %rbx,%rdi
mov    $0x4000,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     1354 <match_user_openssh+0x1a4>
mov    %rbx,%r15
nopw   0x0(%rax,%rax,1)
movzbl (%r15),%eax
cmp    $0x23,%rax
ja     12b0 <match_user_openssh+0x100>
bt     %rax,%r14
jae    1250 <match_user_openssh+0xa0>
add    $0x1,%r15
jmp    1290 <match_user_openssh+0xe0>
cs nopw 0x0(%rax,%rax,1)
add    $0xd0,%al
cmp    $0x9,%al
ja     12c0 <match_user_openssh+0x110>
mov    %r15,%rdi
call   10d0 <ssh1_line_to_key@plt>
jmp    130b <match_user_openssh+0x15b>
lea    0xd53(%rip),%rdi
mov    %r15,%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1303 <match_user_openssh+0x153>
lea    0xd42(%rip),%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1030 <strncmp@plt>
xor    %r13d,%r13d
test   %rax,%rax
jne    1263 <match_user_openssh+0xb3>
mov    %r15,%rdi
call   1090 <ssh_nistp_line_to_key@plt>
jmp    130b <match_user_openssh+0x15b>
mov    %r15,%rdi
call   10c0 <ssh2_line_to_key@plt>
mov    %rax,%rbx
test   %rax,%rax
je     133d <match_user_openssh+0x18d>
mov    -0x30(%rbp),%rdi
mov    %rbx,%rsi
call   10a0 <EVP_PKEY_cmp@plt>
xor    %r13d,%r13d
cmp    $0x1,%eax
sete   %r13b
mov    %rbx,%rdi
call   1050 <EVP_PKEY_free@plt>
lea    -0x4030(%rbp),%rbx
jmp    1263 <match_user_openssh+0xb3>
xor    %r13d,%r13d
lea    -0x4030(%rbp),%rbx
jmp    1263 <match_user_openssh+0xb3>
mov    $0x1,%r15d
jmp    1357 <match_user_openssh+0x1a7>
xor    %r15d,%r15d
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@PATH_MAX = global i32 0
@.str = [24 x i8] c"%s/.ssh/authorized_keys\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"ssh-rsa\00"
@.str.3 = [17 x i8] c"ecdsa-sha2-nistp\00"
define i32 @match_user_openssh(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [16384 x i8]
%9 = alloca %struct.passwd*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %6
%18 = alloca i8, i64 %16
store i64 %16, i64* %7
%19 = load i8*, i8** %5
%20 = call %struct.passwd* @getpwnam(i8* %19)
store %struct.passwd* %20, %struct.passwd** %9
%21 = load %struct.passwd*, %struct.passwd** %9
%22 = icmp ne %struct.passwd* %21, null
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load %struct.passwd*, %struct.passwd** %9
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %29, label %28
28: ; preds = %23, %2
store i32 -1, i32* %3
store i32 1, i32* %12
br label %128
29: ; preds = %23
%30 = load i32, i32* @PATH_MAX
%31 = load %struct.passwd*, %struct.passwd** %9
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %18, i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %33)
%35 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %11
%36 = load i32*, i32** %11
%37 = icmp ne i32* %36, null
br i1 %37, label %39, label %38
38: ; preds = %29
store i32 -1, i32* %3
store i32 1, i32* %12
br label %128
39: ; preds = %29
store i32 0, i32* %10
br label %40
40: ; preds = %121, %39
store i32* null, i32** %13
%41 = getelementptr inbounds [16384 x i8], [16384 x i8]* %8, i64 0, i64 0
%42 = load i32*, i32** %11
%43 = call i32 @fgets(i8* %41, i32 16384, i32* %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %40
br label %124
46: ; preds = %40
%47 = getelementptr inbounds [16384 x i8], [16384 x i8]* %8, i64 0, i64 0
store i8* %47, i8** %14
br label %48
48: ; preds = %61, %46
%49 = load i8*, i8** %14
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 32
br i1 %52, label %58, label %53
53: ; preds = %48
%54 = load i8*, i8** %14
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 9
br label %58
58: ; preds = %53, %48
%59 = phi i1 [ true, %48 ], [ %57, %53 ]
br i1 %59, label %60, label %64
60: ; preds = %58
br label %61
61: ; preds = %60
%62 = load i8*, i8** %14
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %14
br label %48
64: ; preds = %58
%65 = load i8*, i8** %14
%66 = load i8, i8* %65
%67 = icmp ne i8 %66, 0
br i1 %67, label %68, label %78
68: ; preds = %64
%69 = load i8*, i8** %14
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp eq i32 %71, 10
br i1 %72, label %78, label %73
73: ; preds = %68
%74 = load i8*, i8** %14
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 35
br i1 %77, label %78, label %79
78: ; preds = %73, %68, %64
br label %121
79: ; preds = %73
%80 = load i8*, i8** %14
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = icmp sge i32 %82, 48
br i1 %83, label %84, label %92
84: ; preds = %79
%85 = load i8*, i8** %14
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp sle i32 %87, 57
br i1 %88, label %89, label %92
89: ; preds = %84
%90 = load i8*, i8** %14
%91 = call i32* @ssh1_line_to_key(i8* %90)
store i32* %91, i32** %13
br label %108
92: ; preds = %84, %79
%93 = load i8*, i8** %14
%94 = call i64 @strncmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %93, i32 7)
%95 = icmp eq i64 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %92
%97 = load i8*, i8** %14
%98 = call i32* @ssh2_line_to_key(i8* %97)
store i32* %98, i32** %13
br label %107
99: ; preds = %92
%100 = load i8*, i8** %14
%101 = call i64 @strncmp(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %100, i32 16)
%102 = icmp eq i64 %101, 0
br i1 %102, label %103, label %106
103: ; preds = %99
%104 = load i8*, i8** %14
%105 = call i32* @ssh_nistp_line_to_key(i8* %104)
store i32* %105, i32** %13
br label %106
106: ; preds = %103, %99
br label %107
107: ; preds = %106, %96
br label %108
108: ; preds = %107, %89
%109 = load i32*, i32** %13
%110 = icmp eq i32* %109, null
br i1 %110, label %111, label %112
111: ; preds = %108
br label %121
112: ; preds = %108
%113 = load i32*, i32** %4
%114 = load i32*, i32** %13
%115 = call i32 @EVP_PKEY_cmp(i32* %113, i32* %114)
%116 = icmp eq i32 1, %115
br i1 %116, label %117, label %118
117: ; preds = %112
store i32 1, i32* %10
br label %118
118: ; preds = %117, %112
%119 = load i32*, i32** %13
%120 = call i32 @EVP_PKEY_free(i32* %119)
br label %121
121: ; preds = %118, %111, %78
%122 = load i32, i32* %10
%123 = icmp eq i32 %122, 0
br i1 %123, label %40, label %124
124: ; preds = %121, %45
%125 = load i32*, i32** %11
%126 = call i32 @fclose(i32* %125)
%127 = load i32, i32* %10
store i32 %127, i32* %3
store i32 1, i32* %12
br label %128
128: ; preds = %124, %38, %28
%129 = load i8*, i8** %6
%130 = load i32, i32* %3
ret i32 %130
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@PATH_MAX = global i32 0
@.str = [24 x i8] c"%s/.ssh/authorized_keys\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"ssh-rsa\00"
@.str.3 = [17 x i8] c"ecdsa-sha2-nistp\00"
define i32 @match_user_openssh(i32* %0, i8* nocapture %1) {
%3 = alloca [16384 x i8]
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%8 = tail call %struct.passwd* @getpwnam(i8* %1)
%9 = icmp eq %struct.passwd* %8, null
br i1 %9, label %56, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = icmp eq i8* %12, null
br i1 %13, label %56, label %14
14: ; preds = %10
%15 = load i32, i32* @PATH_MAX
%16 = call i32 @snprintf(i8* %6, i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
%17 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %56, label %19
19: ; preds = %14, %50
%20 = call i32 @fgets(i8* %7, i32 16384, i32* %17)
%21 = icmp eq i32 %20, 0
br i1 %21, label %53, label %22
22: ; preds = %19, %25
%23 = phi i8* [ %26, %25 ], [ %7, %19 ]
%24 = load i8, i8* %23
switch i8 %24, label %27 [
i8 32, label %25
i8 9, label %25
i8 0, label %50
i8 10, label %50
i8 35, label %50
]
25: ; preds = %22, %22
%26 = getelementptr inbounds i8, i8* %23, i64 1
br label %22
27: ; preds = %22
%28 = add i8 %24, -48
%29 = icmp ult i8 %28, 10
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32* @ssh1_line_to_key(i8* %23)
br label %42
32: ; preds = %27
%33 = call i64 @strncmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %23, i32 7)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32* @ssh2_line_to_key(i8* %23)
br label %42
37: ; preds = %32
%38 = call i64 @strncmp(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %23, i32 16)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %50
40: ; preds = %37
%41 = call i32* @ssh_nistp_line_to_key(i8* %23)
br label %42
42: ; preds = %35, %40, %30
%43 = phi i32* [ %31, %30 ], [ %36, %35 ], [ %41, %40 ]
%44 = icmp eq i32* %43, null
br i1 %44, label %50, label %45
45: ; preds = %42
%46 = call i32 @EVP_PKEY_cmp(i32* %0, i32* %43)
%47 = icmp eq i32 %46, 1
%48 = zext i1 %47 to i32
%49 = call i32 @EVP_PKEY_free(i32* %43)
br label %50
50: ; preds = %22, %22, %22, %37, %42, %45
%51 = phi i32 [ %48, %45 ], [ 0, %42 ], [ 0, %37 ], [ 0, %22 ], [ 0, %22 ], [ 0, %22 ]
%52 = icmp eq i32 %51, 0
br i1 %52, label %19, label %53
53: ; preds = %19, %50
%54 = phi i32 [ 1, %50 ], [ 0, %19 ]
%55 = call i32 @fclose(i32* %17)
br label %56
56: ; preds = %14, %2, %10, %53
%57 = phi i32 [ %54, %53 ], [ -1, %10 ], [ -1, %2 ], [ -1, %14 ]
ret i32 %57
}