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

/* Variables and functions */
int asn1_get_int(unsigned char **, unsigned char const *, int *);
int asn1_get_tag(unsigned char **, unsigned char const *, int *, int);
int x509_get_version(unsigned char **p, const unsigned char *end, int *ver) {
  int ret, len;
  if ((ret = asn1_get_tag(p, end, &len, 0x80 | 0x20 | 0)) != 0) {
    if (ret == 0x0016)
      return (*ver = 0);
    return (ret);
  }
  end = *p + len;
  if ((ret = asn1_get_int(p, end, ver)) != 0)
    return (-0x0080 | ret);
  if (*p != end)
    return (-0x0080 | 0x001A);
  return (0);
}


x86 assembly (clang, -O0):
<x509_get_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
mov    $0xa0,%ecx
call   1040 <asn1_get_tag@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1181 <x509_get_version+0x61>
cmpl   $0x16,-0x24(%rbp)
jne    1176 <x509_get_version+0x56>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11e4 <x509_get_version+0xc4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <x509_get_version+0xc4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <asn1_get_int@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11c0 <x509_get_version+0xa0>
mov    -0x24(%rbp),%eax
or     $0xffffff80,%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <x509_get_version+0xc4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x18(%rbp),%rax
je     11dd <x509_get_version+0xbd>
movl   $0xffffff9a,-0x4(%rbp)
jmp    11e4 <x509_get_version+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x509_get_version>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rdi,%rbx
lea    0xc(%rsp),%rdx
mov    $0xa0,%ecx
call   1040 <asn1_get_tag@plt>
test   %eax,%eax
je     1152 <x509_get_version+0x32>
cmp    $0x16,%eax
jne    117e <x509_get_version+0x5e>
movl   $0x0,(%r14)
xor    %eax,%eax
jmp    117e <x509_get_version+0x5e>
movslq 0xc(%rsp),%r15
add    (%rbx),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <asn1_get_int@plt>
test   %eax,%eax
je     1171 <x509_get_version+0x51>
or     $0xffffff80,%eax
jmp    117e <x509_get_version+0x5e>
xor    %ecx,%ecx
cmp    %r15,(%rbx)
mov    $0xffffff9a,%eax
cmove  %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @x509_get_version(i8** %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%10 = load i8**, i8*** %5
%11 = load i8*, i8** %6
%12 = call i32 @asn1_get_tag(i8** %10, i8* %11, i32* %9, i32 160)
store i32 %12, i32* %8
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, 22
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i32*, i32** %7
store i32 0, i32* %18
store i32 0, i32* %4
br label %42
19: ; preds = %14
%20 = load i32, i32* %8
store i32 %20, i32* %4
br label %42
21: ; preds = %3
%22 = load i8**, i8*** %5
%23 = load i8*, i8** %22
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8* %26, i8** %6
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %6
%29 = load i32*, i32** %7
%30 = call i32 @asn1_get_int(i8** %27, i8* %28, i32* %29)
store i32 %30, i32* %8
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %21
%33 = load i32, i32* %8
%34 = or i32 -128, %33
store i32 %34, i32* %4
br label %42
35: ; preds = %21
%36 = load i8**, i8*** %5
%37 = load i8*, i8** %36
%38 = load i8*, i8** %6
%39 = icmp ne i8* %37, %38
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 -102, i32* %4
br label %42
41: ; preds = %35
store i32 0, i32* %4
br label %42
42: ; preds = %41, %40, %32, %19, %17
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
define i32 @x509_get_version(i8** %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @asn1_get_tag(i8** %0, i8* %1, i32* %4, i32 160)
switch i32 %6, label %21 [
i32 0, label %8
i32 22, label %7
]
7: ; preds = %3
store i32 0, i32* %2
br label %21
8: ; preds = %3
%9 = load i8*, i8** %0
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = call i32 @asn1_get_int(i8** %0, i8* %12, i32* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = or i32 %13, -128
br label %21
17: ; preds = %8
%18 = load i8*, i8** %0
%19 = icmp eq i8* %18, %12
%20 = select i1 %19, i32 0, i32 -102
br label %21
21: ; preds = %17, %3, %15, %7
%22 = phi i32 [ 0, %7 ], [ %16, %15 ], [ %6, %3 ], [ %20, %17 ]
ret i32 %22
}