#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 /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ lseek(int, int const, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, unsigned char *, int);
int check_pubkey(int pkey_file) {
  unsigned char c;
  unsigned int header_len;
  const int RSA_ENCRYPT_OR_SIGN = 1;
  const int RSA_SIGN = 3;
  const int CREATION_TIME_LENGTH = 4;
  read(pkey_file, &c, 1);
  if (!(c & 0x80)) {
    printf("Bad packet tag byte: this is not an OpenPGP message.\n");
    return -1;
  }
  if (c & 0x40) {
    printf("Packet tag: new packet headers are not supported\n");
    return -1;
  }
  if (!(c & 0x18)) {
    printf("Packet tag: this is not a public key\n");
    return -1;
  }
  switch (c & 0x03) {
  case 0:
    header_len = 2;
    break;
  case 1:
    header_len = 3;
    break;
  case 2:
    header_len = 5;
    break;
  case 3:
  default:
    printf("Packet tag: indefinite length headers are not supported\n");
    return -1;
  }
  header_len--;
  lseek(pkey_file, header_len, SEEK_CUR);
  read(pkey_file, &c, 1);
  if (c != 4) {
    printf("Public key message: unsupported version\n");
    return -1;
  }
  lseek(pkey_file, CREATION_TIME_LENGTH, SEEK_CUR);
  read(pkey_file, &c, 1);
  if (c != RSA_ENCRYPT_OR_SIGN && c != RSA_SIGN) {
    printf("Public key message: this is not an RSA key, or signatures not "
           "allowed\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_pubkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x3,-0x18(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0x9(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    118b <check_pubkey+0x5b>
lea    0xe96(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <check_pubkey+0x1c9>
movzbl -0x9(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     11b3 <check_pubkey+0x83>
lea    0xea4(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <check_pubkey+0x1c9>
movzbl -0x9(%rbp),%eax
and    $0x18,%eax
cmp    $0x0,%eax
jne    11db <check_pubkey+0xab>
lea    0xeae(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <check_pubkey+0x1c9>
movzbl -0x9(%rbp),%eax
and    $0x3,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x3,%eax
ja     122e <check_pubkey+0xfe>
mov    -0x28(%rbp),%rax
lea    0xe04(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x2,-0x10(%rbp)
jmp    1246 <check_pubkey+0x116>
movl   $0x3,-0x10(%rbp)
jmp    1246 <check_pubkey+0x116>
movl   $0x5,-0x10(%rbp)
jmp    1246 <check_pubkey+0x116>
jmp    122e <check_pubkey+0xfe>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <check_pubkey+0x1c9>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0x9(%rbp),%eax
cmp    $0x4,%eax
je     1299 <check_pubkey+0x169>
lea    0xe4f(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <check_pubkey+0x1c9>
mov    -0x8(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1040 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0x9(%rbp),%eax
cmp    $0x1,%eax
je     12f2 <check_pubkey+0x1c2>
movzbl -0x9(%rbp),%eax
cmp    $0x3,%eax
je     12f2 <check_pubkey+0x1c2>
lea    0xe1f(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <check_pubkey+0x1c9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pubkey>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl 0x7(%rsp),%eax
test   $0x80,%al
jne    115a <check_pubkey+0x2a>
lea    0xebb(%rip),%rdi
jmp    1206 <check_pubkey+0xd6>
test   $0x40,%al
jne    1188 <check_pubkey+0x58>
test   $0x18,%al
je     1191 <check_pubkey+0x61>
and    $0x3,%eax
lea    0xefe(%rip),%rdi
mov    $0x1,%esi
lea    0xe88(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x2,%esi
jmp    119f <check_pubkey+0x6f>
lea    0xf81(%rip),%rdi
jmp    1206 <check_pubkey+0xd6>
lea    0xead(%rip),%rdi
jmp    1206 <check_pubkey+0xd6>
mov    $0x4,%esi
mov    0x2e42(%rip),%r14
mov    (%r14),%edx
mov    %ebx,%edi
call   1040 <lseek@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmpb   $0x4,0x7(%rsp)
jne    11ff <check_pubkey+0xcf>
mov    (%r14),%edx
mov    %ebx,%edi
mov    $0x4,%esi
call   1040 <lseek@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    0x7(%rsp),%cl
and    $0xfd,%cl
xor    %eax,%eax
cmp    $0x1,%cl
je     1210 <check_pubkey+0xe0>
lea    0xea5(%rip),%rdi
jmp    1206 <check_pubkey+0xd6>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [54 x i8] c"Bad packet tag byte: this is not an OpenPGP message.\0A\00"
@.str.1 = [50 x i8] c"Packet tag: new packet headers are not supported\0A\00"
@.str.2 = [38 x i8] c"Packet tag: this is not a public key\0A\00"
@.str.3 = [57 x i8] c"Packet tag: indefinite length headers are not supported\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [41 x i8] c"Public key message: unsupported version\0A\00"
@.str.5 = [71 x i8] c"Public key message: this is not an RSA key, or signatures not allowed\0A\00"
define i32 @check_pubkey(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %6
store i32 3, i32* %7
store i32 4, i32* %8
%9 = load i32, i32* %3
%10 = call i32 @read(i32 %9, i8* %4, i32 1)
%11 = load i8, i8* %4
%12 = zext i8 %11 to i32
%13 = and i32 %12, 128
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %71
17: ; preds = %1
%18 = load i8, i8* %4
%19 = zext i8 %18 to i32
%20 = and i32 %19, 64
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %71
24: ; preds = %17
%25 = load i8, i8* %4
%26 = zext i8 %25 to i32
%27 = and i32 %26, 24
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %71
31: ; preds = %24
%32 = load i8, i8* %4
%33 = zext i8 %32 to i32
%34 = and i32 %33, 3
switch i32 %34, label %39 [
i32 0, label %35
i32 1, label %36
i32 2, label %37
i32 3, label %38
]
35: ; preds = %31
store i32 2, i32* %5
br label %41
36: ; preds = %31
store i32 3, i32* %5
br label %41
37: ; preds = %31
store i32 5, i32* %5
br label %41
38: ; preds = %31
br label %39
39: ; preds = %31, %38
%40 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %71
41: ; preds = %37, %36, %35
%42 = load i32, i32* %5
%43 = add i32 %42, -1
store i32 %43, i32* %5
%44 = load i32, i32* %3
%45 = load i32, i32* %5
%46 = load i32, i32* @SEEK_CUR
%47 = call i32 @lseek(i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* %3
%49 = call i32 @read(i32 %48, i8* %4, i32 1)
%50 = load i8, i8* %4
%51 = zext i8 %50 to i32
%52 = icmp ne i32 %51, 4
br i1 %52, label %53, label %55
53: ; preds = %41
%54 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %71
55: ; preds = %41
%56 = load i32, i32* %3
%57 = load i32, i32* @SEEK_CUR
%58 = call i32 @lseek(i32 %56, i32 4, i32 %57)
%59 = load i32, i32* %3
%60 = call i32 @read(i32 %59, i8* %4, i32 1)
%61 = load i8, i8* %4
%62 = zext i8 %61 to i32
%63 = icmp ne i32 %62, 1
br i1 %63, label %64, label %70
64: ; preds = %55
%65 = load i8, i8* %4
%66 = zext i8 %65 to i32
%67 = icmp ne i32 %66, 3
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %2
br label %71
70: ; preds = %64, %55
store i32 0, i32* %2
br label %71
71: ; preds = %70, %68, %53, %39, %29, %22, %15
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@str = [53 x i8] c"Bad packet tag byte: this is not an OpenPGP message.\00"
@str.6 = [37 x i8] c"Packet tag: this is not a public key\00"
@str.7 = [56 x i8] c"Packet tag: indefinite length headers are not supported\00"
@str.8 = [70 x i8] c"Public key message: this is not an RSA key, or signatures not allowed\00"
@str.9 = [40 x i8] c"Public key message: unsupported version\00"
@str.10 = [49 x i8] c"Packet tag: new packet headers are not supported\00"
define i32 @check_pubkey(i32 %0) {
%2 = alloca i8
%3 = call i32 @read(i32 %0, i8* %2, i32 1)
%4 = load i8, i8* %2
%5 = zext i8 %4 to i32
%6 = and i32 %5, 128
%7 = icmp eq i32 %6, 0
br i1 %7, label %33, label %8
8: ; preds = %1
%9 = and i32 %5, 64
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %8
%12 = and i32 %5, 24
%13 = icmp eq i32 %12, 0
br i1 %13, label %33, label %14
14: ; preds = %11
%15 = and i32 %5, 3
switch i32 %15, label %18 [
i32 0, label %19
i32 1, label %16
i32 2, label %17
i32 3, label %33
]
16: ; preds = %14
br label %19
17: ; preds = %14
br label %19
18: ; preds = %14
unreachable
19: ; preds = %14, %17, %16
%20 = phi i32 [ 4, %17 ], [ 2, %16 ], [ 1, %14 ]
%21 = load i32, i32* @SEEK_CUR
%22 = tail call i32 @lseek(i32 %0, i32 %20, i32 %21)
%23 = call i32 @read(i32 %0, i8* %2, i32 1)
%24 = load i8, i8* %2
%25 = icmp eq i8 %24, 4
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = load i32, i32* @SEEK_CUR
%28 = tail call i32 @lseek(i32 %0, i32 4, i32 %27)
%29 = call i32 @read(i32 %0, i8* %2, i32 1)
%30 = load i8, i8* %2
%31 = and i8 %30, -3
%32 = icmp eq i8 %31, 1
br i1 %32, label %36, label %33
33: ; preds = %26, %19, %14, %11, %8, %1
%34 = phi i8* [ getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([49 x i8], [49 x i8]* @str.10, i64 0, i64 0), %8 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0), %11 ], [ getelementptr inbounds ([56 x i8], [56 x i8]* @str.7, i64 0, i64 0), %14 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @str.9, i64 0, i64 0), %19 ], [ getelementptr inbounds ([70 x i8], [70 x i8]* @str.8, i64 0, i64 0), %26 ]
%35 = tail call i32 @puts(i8* dereferenceable(1) %34)
br label %36
36: ; preds = %33, %26
%37 = phi i32 [ 0, %26 ], [ -1, %33 ]
ret i32 %37
}