#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*/ 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 /*<<< orphan*/ LOG_ERR;
int PATH_MAX;
int /*<<< orphan*/ *PEM_read_bio_X509(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *);
scalar_t__ X509_cmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ add_cert(int /*<<< orphan*/ *, int /*<<< orphan*/ ***,
                            int *);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
extern int match_user(X509 *x509, const char *login) {
  char filename[PATH_MAX];
  struct passwd *pw;
  X509 **certs;
  int ncerts, i, rc;
  BIO *in;
  if (!x509)
    return -1;
  if (!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;
  rc = BIO_read_filename(in, filename);
  if (rc != 1) {
    syslog(LOG_ERR, "BIO_read_filename from %s failed\n", filename);
    return -1;
  }
  ncerts = 0;
  certs = NULL;
  for (;;) {
    X509 *cert = PEM_read_bio_X509(in, NULL, 0, NULL);
    if (cert)
      add_cert(cert, &certs, &ncerts);
    else
      break;
  }
  BIO_free(in);
  for (i = 0; i < ncerts; i++) {
    if (X509_cmp(certs[i], x509) == 0)
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<match_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e39(%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,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fa <match_user+0x5a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13a6 <match_user+0x206>
cmpq   $0x0,-0x18(%rbp)
jne    1218 <match_user+0x78>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13a6 <match_user+0x206>
mov    -0x18(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     123e <match_user+0x9e>
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1251 <match_user+0xb1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13a6 <match_user+0x206>
mov    -0x68(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xd94(%rip),%rdx
call   1040 <snprintf@plt>
mov    $0x0,%al
call   10c0 <BIO_s_file@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    12a1 <match_user+0x101>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13a6 <match_user+0x206>
mov    -0x68(%rbp),%rsi
mov    -0x50(%rbp),%rdi
call   1090 <BIO_read_filename@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
je     12e7 <match_user+0x147>
mov    -0x68(%rbp),%rdx
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13a6 <match_user+0x206>
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   10b0 <PEM_read_bio_X509@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     132d <match_user+0x18d>
mov    -0x60(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10a0 <add_cert@plt>
jmp    1332 <match_user+0x192>
jmp    1337 <match_user+0x197>
jmp    12f6 <match_user+0x156>
mov    -0x50(%rbp),%rdi
call   1080 <BIO_free@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1398 <match_user+0x1f8>
mov    -0x38(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <X509_cmp@plt>
cmp    $0x0,%rax
jne    1385 <match_user+0x1e5>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13a6 <match_user+0x206>
jmp    138a <match_user+0x1ea>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1347 <match_user+0x1a7>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match_user>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e38(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     1307 <match_user+0x167>
test   %rsi,%rsi
je     1307 <match_user+0x167>
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     1307 <match_user+0x167>
mov    (%rax),%rcx
test   %rcx,%rcx
je     1307 <match_user+0x167>
mov    (%r12),%esi
lea    0xdf0(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
xor    %eax,%eax
call   10c0 <BIO_s_file@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
test   %rax,%rax
je     1307 <match_user+0x167>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
call   1090 <BIO_read_filename@plt>
cmp    $0x1,%eax
jne    12e7 <match_user+0x147>
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <PEM_read_bio_X509@plt>
test   %rax,%rax
je     12a1 <match_user+0x101>
lea    -0x38(%rbp),%r13
lea    -0x2c(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10a0 <add_cert@plt>
mov    %r12,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <PEM_read_bio_X509@plt>
test   %rax,%rax
jne    1280 <match_user+0xe0>
mov    %r12,%rdi
call   1080 <BIO_free@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    1307 <match_user+0x167>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    -0x38(%rbp),%rax
mov    (%rax,%rbx,8),%rdi
mov    %r14,%rsi
call   1030 <X509_cmp@plt>
test   %rax,%rax
je     1301 <match_user+0x161>
add    $0x1,%rbx
movslq -0x2c(%rbp),%rax
xor    %r15d,%r15d
cmp    %rax,%rbx
jl     12c0 <match_user+0x120>
jmp    1307 <match_user+0x167>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
lea    0xd29(%rip),%rsi
mov    %rbx,%rdx
call   1070 <syslog@plt>
jmp    1307 <match_user+0x167>
mov    $0x1,%r15d
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 = [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(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*
%14 = alloca i32
%15 = alloca i32*
store i32* %0, i32** %4
store i8* %1, i8** %5
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %6
%19 = alloca i8, i64 %17
store i64 %17, i64* %7
%20 = load i32*, i32** %4
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %14
br label %90
23: ; preds = %2
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %23
store i32 -1, i32* %3
store i32 1, i32* %14
br label %90
27: ; preds = %23
%28 = load i8*, i8** %5
%29 = call %struct.passwd* @getpwnam(i8* %28)
store %struct.passwd* %29, %struct.passwd** %8
%30 = load %struct.passwd*, %struct.passwd** %8
%31 = icmp ne %struct.passwd* %30, null
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load %struct.passwd*, %struct.passwd** %8
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = icmp ne i8* %35, null
br i1 %36, label %38, label %37
37: ; preds = %32, %27
store i32 -1, i32* %3
store i32 1, i32* %14
br label %90
38: ; preds = %32
%39 = load i32, i32* @PATH_MAX
%40 = load %struct.passwd*, %struct.passwd** %8
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 @snprintf(i8* %19, i32 %39, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %42)
%44 = call i32 (...) @BIO_s_file()
%45 = call i32* @BIO_new(i32 %44)
store i32* %45, i32** %13
%46 = load i32*, i32** %13
%47 = icmp ne i32* %46, null
br i1 %47, label %49, label %48
48: ; preds = %38
store i32 -1, i32* %3
store i32 1, i32* %14
br label %90
49: ; preds = %38
%50 = load i32*, i32** %13
%51 = call i32 @BIO_read_filename(i32* %50, i8* %19)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = load i32, i32* @LOG_ERR
%56 = call i32 @syslog(i32 %55, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
store i32 1, i32* %14
br label %90
57: ; preds = %49
store i32 0, i32* %10
store i32** null, i32*** %9
br label %58
58: ; preds = %67, %57
%59 = load i32*, i32** %13
%60 = call i32* @PEM_read_bio_X509(i32* %59, i32* null, i32 0, i32* null)
store i32* %60, i32** %15
%61 = load i32*, i32** %15
%62 = icmp ne i32* %61, null
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = load i32*, i32** %15
%65 = call i32 @add_cert(i32* %64, i32*** %9, i32* %10)
br label %67
66: ; preds = %58
br label %68
67: ; preds = %63
br label %58
68: ; preds = %66
%69 = load i32*, i32** %13
%70 = call i32 @BIO_free(i32* %69)
store i32 0, i32* %11
br label %71
71: ; preds = %86, %68
%72 = load i32, i32* %11
%73 = load i32, i32* %10
%74 = icmp slt i32 %72, %73
br i1 %74, label %75, label %89
75: ; preds = %71
%76 = load i32**, i32*** %9
%77 = load i32, i32* %11
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32*, i32** %76, i64 %78
%80 = load i32*, i32** %79
%81 = load i32*, i32** %4
%82 = call i64 @X509_cmp(i32* %80, i32* %81)
%83 = icmp eq i64 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %75
store i32 1, i32* %3
store i32 1, i32* %14
br label %90
85: ; preds = %75
br label %86
86: ; preds = %85
%87 = load i32, i32* %11
%88 = add nsw i32 %87, 1
store i32 %88, i32* %11
br label %71
89: ; preds = %71
store i32 0, i32* %3
store i32 1, i32* %14
br label %90
90: ; preds = %89, %84, %54, %48, %37, %26, %22
%91 = load i8*, i8** %6
%92 = load i32, i32* %3
ret i32 %92
}

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(i32* %0, i8* %1) {
%3 = alloca i32**
%4 = alloca i32
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i32*** %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = icmp eq i32* %0, null
%11 = icmp eq i8* %1, null
%12 = or i1 %10, %11
br i1 %12, label %56, label %13
13: ; preds = %2
%14 = tail call %struct.passwd* @getpwnam(i8* %1)
%15 = icmp eq %struct.passwd* %14, null
br i1 %15, label %56, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %56, label %20
20: ; preds = %16
%21 = load i32, i32* @PATH_MAX
%22 = call i32 @snprintf(i8* %7, i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %18)
%23 = call i32 (...) @BIO_s_file()
%24 = call i32* @BIO_new(i32 %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %56, label %26
26: ; preds = %20
%27 = call i32 @BIO_read_filename(i32* %24, i8* %7)
%28 = icmp eq i32 %27, 1
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* @LOG_ERR
%31 = call i32 @syslog(i32 %30, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %56
32: ; preds = %26
store i32 0, i32* %4
store i32** null, i32*** %3
%33 = call i32* @PEM_read_bio_X509(i32* %24, i32* null, i32 0, i32* null)
%34 = icmp eq i32* %33, null
br i1 %34, label %40, label %35
35: ; preds = %32, %35
%36 = phi i32* [ %38, %35 ], [ %33, %32 ]
%37 = call i32 @add_cert(i32* %36, i32*** %3, i32* %4)
%38 = call i32* @PEM_read_bio_X509(i32* %24, i32* null, i32 0, i32* null)
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %35
40: ; preds = %35, %32
%41 = call i32 @BIO_free(i32* %24)
%42 = load i32, i32* %4
%43 = icmp sgt i32 %42, 0
br i1 %43, label %48, label %56
44: ; preds = %48
%45 = load i32, i32* %4
%46 = sext i32 %45 to i64
%47 = icmp slt i64 %55, %46
br i1 %47, label %48, label %56
48: ; preds = %40, %44
%49 = phi i64 [ %55, %44 ], [ 0, %40 ]
%50 = load i32**, i32*** %3
%51 = getelementptr inbounds i32*, i32** %50, i64 %49
%52 = load i32*, i32** %51
%53 = call i64 @X509_cmp(i32* %52, i32* %0)
%54 = icmp eq i64 %53, 0
%55 = add nuw nsw i64 %49, 1
br i1 %54, label %56, label %44
56: ; preds = %48, %44, %40, %20, %13, %16, %2, %29
%57 = phi i32 [ -1, %29 ], [ -1, %2 ], [ -1, %16 ], [ -1, %13 ], [ -1, %20 ], [ 0, %40 ], [ 1, %48 ], [ 0, %44 ]
ret i32 %57
}