#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*/ X509;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int BIO_read_filename(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ BIO_s_file();
int EVP_PKEY_cmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ LOG_ERR;
int PATH_MAX;
int /*<<< orphan*/ *PEM_read_bio_X509(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ **, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_get_pubkey(int /*<<< orphan*/ *);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
extern int match_user_opensc(EVP_PKEY *authkey, const char *login) {
  char filename[PATH_MAX];
  struct passwd *pw;
  int found;
  BIO *in;
  X509 *cert = NULL;
  if (NULL == authkey || NULL == login)
    return -1;
  pw = getpwnam(login);
  if (!pw || !pw->pw_dir)
    return -1;
  snprintf(filename, PATH_MAX, "%s/.eid/authorized_certificates", pw->pw_dir);
  in = BIO_new(BIO_s_file());
  if (!in)
    return -1;
  if (BIO_read_filename(in, filename) != 1) {
    syslog(LOG_ERR, "BIO_read_filename from %s failed\n", filename);
    return -1;
  }
  found = 0;
  do {
    EVP_PKEY *key;
    if (NULL == PEM_read_bio_X509(in, &cert, 0, NULL)) {
      break;
    }
    key = X509_get_pubkey(cert);
    if (key == NULL)
      continue;
    if (1 == EVP_PKEY_cmp(authkey, key)) {
      found = 1;
    }
    EVP_PKEY_free(key);
  } while (found == 0);
  if (cert) {
    X509_free(cert);
  }
  BIO_free(in);
  return found;
}


x86 assembly (clang, -O0):
<match_user_opensc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e19(%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,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     121c <match_user_opensc+0x5c>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    122f <match_user_opensc+0x6f>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    13a4 <match_user_opensc+0x1e4>
mov    -0x18(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1255 <match_user_opensc+0x95>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1268 <match_user_opensc+0xa8>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    13a4 <match_user_opensc+0x1e4>
mov    -0x60(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xd7d(%rip),%rdx
call   1040 <snprintf@plt>
mov    $0x0,%al
call   10e0 <BIO_s_file@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12b8 <match_user_opensc+0xf8>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    13a4 <match_user_opensc+0x1e4>
mov    -0x60(%rbp),%rsi
mov    -0x40(%rbp),%rdi
call   10b0 <BIO_read_filename@plt>
cmp    $0x1,%eax
je     12fa <match_user_opensc+0x13a>
mov    -0x60(%rbp),%rdx
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
call   1090 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    13a4 <match_user_opensc+0x1e4>
movl   $0x0,-0x34(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   10d0 <PEM_read_bio_X509@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1327 <match_user_opensc+0x167>
jmp    137a <match_user_opensc+0x1ba>
mov    -0x48(%rbp),%rdi
call   1060 <X509_get_pubkey@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1344 <match_user_opensc+0x184>
jmp    1370 <match_user_opensc+0x1b0>
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1070 <EVP_PKEY_cmp@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1367 <match_user_opensc+0x1a7>
movl   $0x1,-0x34(%rbp)
mov    -0x58(%rbp),%rdi
call   1030 <EVP_PKEY_free@plt>
cmpl   $0x0,-0x34(%rbp)
je     1301 <match_user_opensc+0x141>
cmpq   $0x0,-0x48(%rbp)
je     138e <match_user_opensc+0x1ce>
mov    -0x48(%rbp),%rdi
call   10c0 <X509_free@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <BIO_free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_user_opensc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e1b(%rip),%r15
mov    (%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movq   $0x0,-0x30(%rbp)
mov    $0xffffffff,%r12d
test   %rdi,%rdi
je     12f3 <match_user_opensc+0x133>
test   %rsi,%rsi
je     12f3 <match_user_opensc+0x133>
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     12f3 <match_user_opensc+0x133>
mov    (%rax),%rcx
test   %rcx,%rcx
je     12f3 <match_user_opensc+0x133>
mov    (%r15),%esi
lea    0xdcd(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
xor    %eax,%eax
call   10e0 <BIO_s_file@plt>
mov    %eax,%edi
call   1080 <BIO_new@plt>
test   %rax,%rax
je     12f3 <match_user_opensc+0x133>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   10b0 <BIO_read_filename@plt>
cmp    $0x1,%eax
jne    12db <match_user_opensc+0x11b>
lea    -0x30(%rbp),%r13
jmp    1292 <match_user_opensc+0xd2>
nopl   0x0(%rax,%rax,1)
mov    %rax,%r12
mov    %r14,%rdi
mov    %rax,%rsi
call   1070 <EVP_PKEY_cmp@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
sete   %bl
mov    %r12,%rdi
call   1030 <EVP_PKEY_free@plt>
test   %ebx,%ebx
jne    12bd <match_user_opensc+0xfd>
xor    %r12d,%r12d
mov    %r15,%rdi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   10d0 <PEM_read_bio_X509@plt>
test   %rax,%rax
je     12c3 <match_user_opensc+0x103>
mov    -0x30(%rbp),%rdi
call   1060 <X509_get_pubkey@plt>
test   %rax,%rax
jne    1270 <match_user_opensc+0xb0>
xor    %ebx,%ebx
test   %ebx,%ebx
je     1292 <match_user_opensc+0xd2>
mov    $0x1,%r12d
mov    -0x30(%rbp),%rdi
test   %rdi,%rdi
je     12d1 <match_user_opensc+0x111>
call   10c0 <X509_free@plt>
mov    %r15,%rdi
call   10a0 <BIO_free@plt>
jmp    12f3 <match_user_opensc+0x133>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
lea    0xd35(%rip),%rsi
mov    %rbx,%rdx
call   1090 <syslog@plt>
mov    %r12d,%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 = [32 x i8] c"%s/.eid/authorized_certificates\00"
@LOG_ERR = global i32 0
@.str.1 = [34 x i8] c"BIO_read_filename from %s failed\0A\00"
define i32 @match_user_opensc(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.passwd*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32*
store i32* %0, i32** %4
store i8* %1, i8** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
store i32* null, i32** %11
%18 = load i32*, i32** %4
%19 = icmp eq i32* null, %18
br i1 %19, label %23, label %20
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = icmp eq i8* null, %21
br i1 %22, label %23, label %24
23: ; preds = %20, %2
store i32 -1, i32* %3
store i32 1, i32* %12
br label %87
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = call %struct.passwd* @getpwnam(i8* %25)
store %struct.passwd* %26, %struct.passwd** %8
%27 = load %struct.passwd*, %struct.passwd** %8
%28 = icmp ne %struct.passwd* %27, null
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load %struct.passwd*, %struct.passwd** %8
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %29, %24
store i32 -1, i32* %3
store i32 1, i32* %12
br label %87
35: ; preds = %29
%36 = load i32, i32* @PATH_MAX
%37 = load %struct.passwd*, %struct.passwd** %8
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @snprintf(i8* %17, i32 %36, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %39)
%41 = call i32 (...) @BIO_s_file()
%42 = call i32* @BIO_new(i32 %41)
store i32* %42, i32** %10
%43 = load i32*, i32** %10
%44 = icmp ne i32* %43, null
br i1 %44, label %46, label %45
45: ; preds = %35
store i32 -1, i32* %3
store i32 1, i32* %12
br label %87
46: ; preds = %35
%47 = load i32*, i32** %10
%48 = call i32 @BIO_read_filename(i32* %47, i8* %17)
%49 = icmp ne i32 %48, 1
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* @LOG_ERR
%52 = call i32 @syslog(i32 %51, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %87
53: ; preds = %46
store i32 0, i32* %9
br label %54
54: ; preds = %74, %53
%55 = load i32*, i32** %10
%56 = call i32* @PEM_read_bio_X509(i32* %55, i32** %11, i32 0, i32* null)
%57 = icmp eq i32* null, %56
br i1 %57, label %58, label %59
58: ; preds = %54
br label %77
59: ; preds = %54
%60 = load i32*, i32** %11
%61 = call i32* @X509_get_pubkey(i32* %60)
store i32* %61, i32** %13
%62 = load i32*, i32** %13
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %65
64: ; preds = %59
br label %74
65: ; preds = %59
%66 = load i32*, i32** %4
%67 = load i32*, i32** %13
%68 = call i32 @EVP_PKEY_cmp(i32* %66, i32* %67)
%69 = icmp eq i32 1, %68
br i1 %69, label %70, label %71
70: ; preds = %65
store i32 1, i32* %9
br label %71
71: ; preds = %70, %65
%72 = load i32*, i32** %13
%73 = call i32 @EVP_PKEY_free(i32* %72)
br label %74
74: ; preds = %71, %64
%75 = load i32, i32* %9
%76 = icmp eq i32 %75, 0
br i1 %76, label %54, label %77
77: ; preds = %74, %58
%78 = load i32*, i32** %11
%79 = icmp ne i32* %78, null
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = load i32*, i32** %11
%82 = call i32 @X509_free(i32* %81)
br label %83
83: ; preds = %80, %77
%84 = load i32*, i32** %10
%85 = call i32 @BIO_free(i32* %84)
%86 = load i32, i32* %9
store i32 %86, i32* %3
store i32 1, i32* %12
br label %87
87: ; preds = %83, %50, %45, %34, %23
%88 = load i8*, i8** %6
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@PATH_MAX = global i32 0
@.str = [32 x i8] c"%s/.eid/authorized_certificates\00"
@LOG_ERR = global i32 0
@.str.1 = [34 x i8] c"BIO_read_filename from %s failed\0A\00"
define i32 @match_user_opensc(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%8 = icmp eq i32* %0, null
%9 = icmp eq i8* %1, null
%10 = or i1 %8, %9
br i1 %10, label %53, label %11
11: ; preds = %2
%12 = tail call %struct.passwd* @getpwnam(i8* %1)
%13 = icmp eq %struct.passwd* %12, null
br i1 %13, label %53, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %53, label %18
18: ; preds = %14
%19 = load i32, i32* @PATH_MAX
%20 = call i32 @snprintf(i8* %6, i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %16)
%21 = call i32 (...) @BIO_s_file()
%22 = call i32* @BIO_new(i32 %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %53, label %24
24: ; preds = %18
%25 = call i32 @BIO_read_filename(i32* %22, i8* %6)
%26 = icmp eq i32 %25, 1
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* @LOG_ERR
%29 = call i32 @syslog(i32 %28, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %53
30: ; preds = %24, %42
%31 = call i32* @PEM_read_bio_X509(i32* %22, i32** %3, i32 0, i32* null)
%32 = icmp eq i32* %31, null
br i1 %32, label %45, label %33
33: ; preds = %30
%34 = load i32*, i32** %3
%35 = call i32* @X509_get_pubkey(i32* %34)
%36 = icmp eq i32* %35, null
br i1 %36, label %42, label %37
37: ; preds = %33
%38 = call i32 @EVP_PKEY_cmp(i32* %0, i32* %35)
%39 = icmp eq i32 %38, 1
%40 = zext i1 %39 to i32
%41 = call i32 @EVP_PKEY_free(i32* %35)
br label %42
42: ; preds = %33, %37
%43 = phi i32 [ %40, %37 ], [ 0, %33 ]
%44 = icmp eq i32 %43, 0
br i1 %44, label %30, label %45
45: ; preds = %30, %42
%46 = phi i32 [ 1, %42 ], [ 0, %30 ]
%47 = load i32*, i32** %3
%48 = icmp eq i32* %47, null
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = call i32 @X509_free(i32* %47)
br label %51
51: ; preds = %49, %45
%52 = call i32 @BIO_free(i32* %22)
br label %53
53: ; preds = %18, %11, %14, %2, %51, %27
%54 = phi i32 [ -1, %27 ], [ %46, %51 ], [ -1, %2 ], [ -1, %14 ], [ -1, %11 ], [ -1, %18 ]
ret i32 %54
}