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

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_file(char const *, char *);
int BIO_read(int /*<<< orphan*/ *, unsigned char *, int);
scalar_t__ BIO_reset(int /*<<< orphan*/ *);
scalar_t__ BIO_should_retry(int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_bio_X509(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_X509_bio(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ display_openssl_errors(int);
int /*<<< orphan*/ err(int, char *, char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
X509 *read_x509(const char *x509_name) {
  unsigned char buf[2];
  X509 *x509;
  BIO *b;
  int n;
  b = BIO_new_file(x509_name, "rb");
  do {
    bool __cond = (!b);
    display_openssl_errors(199);
    if (__cond) {
      err(1, "%s", x509_name);
    }
  } while (0);
  n = BIO_read(b, buf, 2);
  if (n != 2) {
    if (BIO_should_retry(b)) {
      fprintf(stderr, "%s: Read wanted retry\n", x509_name);
      exit(1);
    }
    if (n >= 0) {
      fprintf(stderr, "%s: Short read\n", x509_name);
      exit(1);
    }
    do {
      bool __cond = (1);
      display_openssl_errors(218);
      if (__cond) {
        err(1, "%s", x509_name);
      }
    } while (0);
  }
  do {
    bool __cond = (BIO_reset(b) != 0);
    display_openssl_errors(221);
    if (__cond) {
      err(1, "%s", x509_name);
    }
  } while (0);
  if (buf[0] == 0x30 && buf[1] >= 0x81 && buf[1] <= 0x84) {
    x509 = d2i_X509_bio(b, NULL);
  } else {
    x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);
  }
  BIO_free(b);
  do {
    bool __cond = (!x509);
    display_openssl_errors(235);
    if (__cond) {
      err(1, "%s", x509_name);
    }
  } while (0);
  return x509;
}


x86 assembly (clang, -O0):
<read_x509>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   10d0 <BIO_new_file@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x28(%rbp)
mov    $0xc7,%edi
call   1080 <display_openssl_errors@plt>
cmpl   $0x0,-0x28(%rbp)
je     120b <read_x509+0x5b>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xdfd(%rip),%rsi
call   1060 <err@plt>
jmp    1210 <read_x509+0x60>
mov    -0x20(%rbp),%rdi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1040 <BIO_read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
je     12d1 <read_x509+0x121>
mov    -0x20(%rbp),%rdi
call   10b0 <BIO_should_retry@plt>
cmp    $0x0,%rax
je     1265 <read_x509+0xb5>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb0(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jl     1292 <read_x509+0xe2>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd9a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1297 <read_x509+0xe7>
movl   $0x1,-0x2c(%rbp)
mov    $0xda,%edi
call   1080 <display_openssl_errors@plt>
cmpl   $0x0,-0x2c(%rbp)
je     12c7 <read_x509+0x117>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd41(%rip),%rsi
call   1060 <err@plt>
jmp    12cc <read_x509+0x11c>
jmp    12d1 <read_x509+0x121>
jmp    12d6 <read_x509+0x126>
mov    -0x20(%rbp),%rdi
call   1090 <BIO_reset@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x30(%rbp)
mov    $0xdd,%edi
call   1080 <display_openssl_errors@plt>
cmpl   $0x0,-0x30(%rbp)
je     1317 <read_x509+0x167>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xcf1(%rip),%rsi
call   1060 <err@plt>
jmp    131c <read_x509+0x16c>
movzbl -0xa(%rbp),%eax
cmp    $0x30,%eax
jne    135d <read_x509+0x1ad>
movzbl -0x9(%rbp),%eax
cmp    $0x81,%eax
jl     135d <read_x509+0x1ad>
movzbl -0x9(%rbp),%eax
cmp    $0x84,%eax
jg     135d <read_x509+0x1ad>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <d2i_X509_bio@plt>
mov    %rax,-0x18(%rbp)
jmp    1374 <read_x509+0x1c4>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10c0 <PEM_read_bio_X509@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <BIO_free@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    $0xeb,%edi
call   1080 <display_openssl_errors@plt>
cmpl   $0x0,-0x34(%rbp)
je     13b8 <read_x509+0x208>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xc50(%rip),%rsi
call   1060 <err@plt>
jmp    13bd <read_x509+0x20d>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_x509>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe3d(%rip),%rsi
call   10d0 <BIO_new_file@plt>
mov    %rax,%r15
mov    $0xc7,%edi
call   1080 <display_openssl_errors@plt>
test   %r15,%r15
jne    11ee <read_x509+0x3e>
lea    0xe22(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <err@plt>
lea    0xe(%rsp),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
call   1040 <BIO_read@plt>
cmp    $0x2,%eax
je     123e <read_x509+0x8e>
mov    %eax,%ebx
mov    %r15,%rdi
call   10b0 <BIO_should_retry@plt>
test   %rax,%rax
jne    12d4 <read_x509+0x124>
test   %ebx,%ebx
jns    12e6 <read_x509+0x136>
mov    $0xda,%edi
call   1080 <display_openssl_errors@plt>
lea    0xdd2(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <err@plt>
mov    %r15,%rdi
call   1090 <BIO_reset@plt>
mov    %rax,%rbx
mov    $0xdd,%edi
call   1080 <display_openssl_errors@plt>
test   %rbx,%rbx
je     126c <read_x509+0xbc>
lea    0xda4(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <err@plt>
cmpb   $0x30,0xe(%rsp)
jne    128b <read_x509+0xdb>
mov    0xf(%rsp),%al
cmp    $0x81,%al
jb     128b <read_x509+0xdb>
cmp    $0x84,%al
ja     128b <read_x509+0xdb>
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <d2i_X509_bio@plt>
jmp    1299 <read_x509+0xe9>
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10c0 <PEM_read_bio_X509@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1070 <BIO_free@plt>
mov    $0xeb,%edi
call   1080 <display_openssl_errors@plt>
test   %rbx,%rbx
jne    12c7 <read_x509+0x117>
lea    0xd49(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <err@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
jmp    12f6 <read_x509+0x146>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"%s: Read wanted retry\0A\00"
@.str.3 = [16 x i8] c"%s: Short read\0A\00"
define i32* @read_x509(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i8]
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %2
%11 = load i8*, i8** %2
%12 = call i32* @BIO_new_file(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
br label %13
13: ; preds = %1
%14 = load i32*, i32** %5
%15 = icmp ne i32* %14, null
%16 = xor i1 %15, true
%17 = zext i1 %16 to i32
store i32 %17, i32* %7
%18 = call i32 @display_openssl_errors(i32 199)
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i8*, i8** %2
%23 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %13
br label %25
25: ; preds = %24
%26 = load i32*, i32** %5
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%28 = call i32 @BIO_read(i32* %26, i8* %27, i32 2)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 2
br i1 %30, label %31, label %58
31: ; preds = %25
%32 = load i32*, i32** %5
%33 = call i64 @BIO_should_retry(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %2
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %31
%41 = load i32, i32* %6
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %2
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
br label %49
49: ; preds = %48
store i32 1, i32* %8
%50 = call i32 @display_openssl_errors(i32 218)
%51 = load i32, i32* %8
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i8*, i8** %2
%55 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %53, %49
br label %57
57: ; preds = %56
br label %58
58: ; preds = %57, %25
br label %59
59: ; preds = %58
%60 = load i32*, i32** %5
%61 = call i64 @BIO_reset(i32* %60)
%62 = icmp ne i64 %61, 0
%63 = zext i1 %62 to i32
store i32 %63, i32* %9
%64 = call i32 @display_openssl_errors(i32 221)
%65 = load i32, i32* %9
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %59
%68 = load i8*, i8** %2
%69 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %67, %59
br label %71
71: ; preds = %70
%72 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%73 = load i8, i8* %72
%74 = zext i8 %73 to i32
%75 = icmp eq i32 %74, 48
br i1 %75, label %76, label %89
76: ; preds = %71
%77 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%78 = load i8, i8* %77
%79 = zext i8 %78 to i32
%80 = icmp sge i32 %79, 129
br i1 %80, label %81, label %89
81: ; preds = %76
%82 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
%83 = load i8, i8* %82
%84 = zext i8 %83 to i32
%85 = icmp sle i32 %84, 132
br i1 %85, label %86, label %89
86: ; preds = %81
%87 = load i32*, i32** %5
%88 = call i32* @d2i_X509_bio(i32* %87, i32* null)
store i32* %88, i32** %4
br label %92
89: ; preds = %81, %76, %71
%90 = load i32*, i32** %5
%91 = call i32* @PEM_read_bio_X509(i32* %90, i32* null, i32* null, i32* null)
store i32* %91, i32** %4
br label %92
92: ; preds = %89, %86
%93 = load i32*, i32** %5
%94 = call i32 @BIO_free(i32* %93)
br label %95
95: ; preds = %92
%96 = load i32*, i32** %4
%97 = icmp ne i32* %96, null
%98 = xor i1 %97, true
%99 = zext i1 %98 to i32
store i32 %99, i32* %10
%100 = call i32 @display_openssl_errors(i32 235)
%101 = load i32, i32* %10
%102 = icmp ne i32 %101, 0
br i1 %102, label %103, label %106
103: ; preds = %95
%104 = load i8*, i8** %2
%105 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %104)
br label %106
106: ; preds = %103, %95
br label %107
107: ; preds = %106
%108 = load i32*, i32** %4
ret i32* %108
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"%s: Read wanted retry\0A\00"
@.str.3 = [16 x i8] c"%s: Short read\0A\00"
define i32* @read_x509(i8* %0) {
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @BIO_new_file(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
%6 = tail call i32 @display_openssl_errors(i32 199)
br i1 %5, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %1
%10 = call i32 @BIO_read(i32* %4, i8* %3, i32 2)
%11 = icmp eq i32 %10, 2
br i1 %11, label %28, label %12
12: ; preds = %9
%13 = call i64 @BIO_should_retry(i32* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = icmp sgt i32 %10, -1
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = call i32 @display_openssl_errors(i32 218)
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %28
28: ; preds = %9, %25
%29 = call i64 @BIO_reset(i32* %4)
%30 = icmp eq i64 %29, 0
%31 = call i32 @display_openssl_errors(i32 221)
br i1 %30, label %34, label %32
32: ; preds = %28
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %34
34: ; preds = %32, %28
%35 = load i8, i8* %3
%36 = icmp eq i8 %35, 48
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%38 = load i8, i8* %37
%39 = icmp ugt i8 %38, -128
%40 = select i1 %36, i1 %39, i1 false
%41 = icmp ult i8 %38, -123
%42 = select i1 %40, i1 %41, i1 false
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32* @d2i_X509_bio(i32* %4, i32* null)
br label %47
45: ; preds = %34
%46 = call i32* @PEM_read_bio_X509(i32* %4, i32* null, i32* null, i32* null)
br label %47
47: ; preds = %45, %43
%48 = phi i32* [ %44, %43 ], [ %46, %45 ]
%49 = call i32 @BIO_free(i32* %4)
%50 = icmp eq i32* %48, null
%51 = call i32 @display_openssl_errors(i32 235)
br i1 %50, label %52, label %54
52: ; preds = %47
%53 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %54
54: ; preds = %52, %47
ret i32* %48
}