#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*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int check_pubkey(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ getMPI(int, int, char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  int pkey_file, module_file;
  int ret = -1;
  if (argc <= 1)
    usage();
  pkey_file = open(argv[1], O_RDONLY);
  if (pkey_file < 0) {
    printf("Unable to open pkey_file %s %s\n", argv[1], strerror(errno));
    return -1;
  }
  module_file = open("/sys/digsig/key", O_WRONLY);
  if (module_file < 0) {
    printf("Unable to open module char device %s\n", strerror(errno));
    close(pkey_file);
    return -1;
  }
  if (check_pubkey(pkey_file) == -1) {
    goto do_end;
  }
  if (getMPI(pkey_file, module_file, 'n') != 0) {
    goto do_end;
  }
  if (getMPI(pkey_file, module_file, 'e') != 0) {
    goto do_end;
  }
  ret = 0;
do_end:
  close(module_file);
  close(pkey_file);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     119e <main+0x2e>
mov    $0x0,%al
call   1060 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fc <main+0x8c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <main+0x152>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1251 <main+0xe1>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <main+0x152>
mov    -0x14(%rbp),%edi
call   1050 <check_pubkey@plt>
cmp    $0xffffffff,%eax
jne    1267 <main+0xf7>
jmp    12ac <main+0x13c>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x6e,%edx
call   1080 <getMPI@plt>
cmp    $0x0,%rax
je     1286 <main+0x116>
jmp    12ac <main+0x13c>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x65,%edx
call   1080 <getMPI@plt>
cmp    $0x0,%rax
je     12a5 <main+0x135>
jmp    12ac <main+0x13c>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
cmp    $0x1,%edi
jg     1183 <main+0x13>
xor    %eax,%eax
call   1060 <usage@plt>
mov    0x8(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1201 <main+0x91>
mov    %eax,%ebx
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1229 <main+0xb9>
mov    %eax,%r14d
mov    %ebx,%edi
call   1050 <check_pubkey@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11f0 <main+0x80>
mov    %ebx,%edi
mov    %r14d,%esi
mov    $0x6e,%edx
call   1080 <getMPI@plt>
test   %rax,%rax
jne    11f0 <main+0x80>
mov    %ebx,%edi
mov    %r14d,%esi
mov    $0x65,%edx
call   1080 <getMPI@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1254 <main+0xe4>
mov    0x8(%rbp),%rbx
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde6(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    124f <main+0xdf>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf2(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"Unable to open pkey_file %s %s\0A\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"/sys/digsig/key\00"
@O_WRONLY = global i32 0
@.str.2 = [38 x i8] c"Unable to open module char device %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %8
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (...) @usage()
br label %13
13: ; preds = %11, %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %13
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %26)
store i32 -1, i32* %3
br label %63
28: ; preds = %13
%29 = load i32, i32* @O_WRONLY
%30 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %3
br label %63
39: ; preds = %28
%40 = load i32, i32* %6
%41 = call i32 @check_pubkey(i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %39
br label %57
44: ; preds = %39
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = call i64 @getMPI(i32 %45, i32 %46, i8 signext 110)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
br label %57
50: ; preds = %44
%51 = load i32, i32* %6
%52 = load i32, i32* %7
%53 = call i64 @getMPI(i32 %51, i32 %52, i8 signext 101)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %57
56: ; preds = %50
store i32 0, i32* %8
br label %57
57: ; preds = %56, %55, %49, %43
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %8
store i32 %62, i32* %3
br label %63
63: ; preds = %57, %33, %21
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"Unable to open pkey_file %s %s\0A\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"/sys/digsig/key\00"
@O_WRONLY = global i32 0
@.str.2 = [38 x i8] c"Unable to open module char device %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (...) @usage()
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %6
%13 = load i8*, i8** %7
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %15)
br label %40
17: ; preds = %6
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = tail call i32 @close(i32 %10)
br label %40
26: ; preds = %17
%27 = tail call i32 @check_pubkey(i32 %10)
%28 = icmp eq i32 %27, -1
br i1 %28, label %36, label %29
29: ; preds = %26
%30 = tail call i64 @getMPI(i32 %10, i32 %19, i8 signext 110)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = tail call i64 @getMPI(i32 %10, i32 %19, i8 signext 101)
%34 = icmp ne i64 %33, 0
%35 = sext i1 %34 to i32
br label %36
36: ; preds = %32, %29, %26
%37 = phi i32 [ -1, %26 ], [ -1, %29 ], [ %35, %32 ]
%38 = tail call i32 @close(i32 %19)
%39 = tail call i32 @close(i32 %10)
br label %40
40: ; preds = %36, %21, %12
%41 = phi i32 [ -1, %12 ], [ -1, %21 ], [ %37, %36 ]
ret i32 %41
}