#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*/ FILE;
typedef int /*<<< orphan*/ EVP_PKEY;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ exit(int);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *read_private_key(char *);
int /*<<< orphan*/ sign_hashed_data(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  FILE *input;
  unsigned char *buffer;
  int test;
  EVP_PKEY *privkey;
  if (argc > 3 || argc < 2)
    usage();
  if (argc < 3 || strcmp(argv[2], "-") == 0)
    input = stdin;
  else {
    input = fopen(argv[2], "r");
    if (!input)
      usage();
  }
  privkey = read_private_key(argv[1]);
  buffer = malloc(32);
  if (!buffer) {
    fprintf(stderr, "Argh, malloc failed\n");
    exit(1);
  }
  if (fread(buffer, 32, 1, input) != 1) {
    perror("half-sign: Failed to read SHA256 from input\n");
    exit(1);
  }
  test = fgetc(input);
  if (test != EOF && test != '\n') {
    fprintf(stderr, "Error, more than %d bytes fed to half-sign\n", 32);
    fprintf(stderr, "Last byte was :%d\n", (int)test);
    exit(1);
  }
  sign_hashed_data(privkey, buffer, 32);
  return 0;
}


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)
cmpl   $0x3,-0x8(%rbp)
jg     11da <main+0x2a>
cmpl   $0x2,-0x8(%rbp)
jge    11e1 <main+0x31>
mov    $0x0,%al
call   10a0 <usage@plt>
cmpl   $0x3,-0x8(%rbp)
jl     1209 <main+0x59>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe06(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    121c <main+0x6c>
mov    0x2dd8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    124b <main+0x9b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdd7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1246 <main+0x96>
mov    $0x0,%al
call   10a0 <usage@plt>
jmp    124b <main+0x9b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <read_private_key@plt>
mov    %rax,-0x30(%rbp)
mov    $0x20,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1296 <main+0xe6>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x20,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     12cc <main+0x11c>
lea    0xd5c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%eax
je     1334 <main+0x184>
cmpl   $0xa,-0x24(%rbp)
je     1334 <main+0x184>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
lea    0xd42(%rip),%rsi
mov    $0x20,%edx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xd4f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x20,%edx
call   1050 <sign_hashed_data@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
lea    -0x4(%rbx),%eax
cmp    $0xfffffffd,%eax
ja     11cb <main+0x1b>
xor    %eax,%eax
call   10a0 <usage@plt>
cmp    $0x3,%ebx
jl     1209 <main+0x59>
mov    0x10(%r14),%rdi
lea    0xe25(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1209 <main+0x59>
mov    0x10(%r14),%rdi
lea    0xe12(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1213 <main+0x63>
xor    %r15d,%r15d
xor    %eax,%eax
call   10a0 <usage@plt>
jmp    1213 <main+0x63>
mov    0x2dd8(%rip),%rax
mov    (%rax),%r15
mov    0x8(%r14),%rdi
call   1030 <read_private_key@plt>
mov    %rax,%r14
mov    $0x20,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1282 <main+0xd2>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x20,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    12a3 <main+0xf3>
mov    %r15,%rdi
call   1060 <fgetc@plt>
cmp    $0xa,%eax
je     1265 <main+0xb5>
mov    %eax,%ebp
mov    0x2d7f(%rip),%rax
cmp    (%rax),%ebp
jne    12b9 <main+0x109>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1050 <sign_hashed_data@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd6f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d10(%rip),%rbx
mov    (%rbx),%edi
lea    0xd7d(%rip),%rsi
mov    $0x20,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd94(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"Argh, malloc failed\0A\00"
@.str.3 = [45 x i8] c"half-sign: Failed to read SHA256 from input\0A\00"
@EOF = global i32 0
@.str.4 = [44 x i8] c"Error, more than %d bytes fed to half-sign\0A\00"
@.str.5 = [19 x i8] c"Last byte was :%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 3
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %12, %2
%16 = call i32 (...) @usage()
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 3
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20, %17
%27 = load i32*, i32** @stdin
store i32* %27, i32** %6
br label %38
28: ; preds = %20
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %6
%33 = load i32*, i32** %6
%34 = icmp ne i32* %33, null
br i1 %34, label %37, label %35
35: ; preds = %28
%36 = call i32 (...) @usage()
br label %37
37: ; preds = %35, %28
br label %38
38: ; preds = %37, %26
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32* @read_private_key(i8* %41)
store i32* %42, i32** %9
%43 = call i8* @malloc(i32 32)
store i8* %43, i8** %7
%44 = load i8*, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %50, label %46
46: ; preds = %38
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %38
%51 = load i8*, i8** %7
%52 = load i32*, i32** %6
%53 = call i32 @fread(i8* %51, i32 32, i32 1, i32* %52)
%54 = icmp ne i32 %53, 1
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %50
%59 = load i32*, i32** %6
%60 = call i32 @fgetc(i32* %59)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = load i32, i32* @EOF
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %74
64: ; preds = %58
%65 = load i32, i32* %8
%66 = icmp ne i32 %65, 10
br i1 %66, label %67, label %74
67: ; preds = %64
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 32)
%70 = load i32, i32* @stderr
%71 = load i32, i32* %8
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %71)
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %64, %58
%75 = load i32*, i32** %9
%76 = load i8*, i8** %7
%77 = call i32 @sign_hashed_data(i32* %75, i8* %76, i32 32)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"Argh, malloc failed\0A\00"
@.str.3 = [45 x i8] c"half-sign: Failed to read SHA256 from input\0A\00"
@EOF = global i32 0
@.str.4 = [44 x i8] c"Error, more than %d bytes fed to half-sign\0A\00"
@.str.5 = [19 x i8] c"Last byte was :%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -4
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @usage()
br label %7
7: ; preds = %2, %5
%8 = icmp slt i32 %0, 3
br i1 %8, label %14, label %9
9: ; preds = %7
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9, %7
%15 = load i32*, i32** @stdin
br label %22
16: ; preds = %9
%17 = load i8*, i8** %10
%18 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 (...) @usage()
br label %22
22: ; preds = %16, %20, %14
%23 = phi i32* [ %15, %14 ], [ %18, %16 ], [ null, %20 ]
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = tail call i32* @read_private_key(i8* %25)
%27 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %22
%34 = tail call i32 @fread(i8* %27, i32 32, i32 1, i32* %23)
%35 = icmp eq i32 %34, 1
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = tail call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %33
%40 = tail call i32 @fgetc(i32* %23)
%41 = load i32, i32* @EOF
%42 = icmp ne i32 %40, %41
%43 = icmp ne i32 %40, 10
%44 = and i1 %43, %42
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 32)
%48 = load i32, i32* @stderr
%49 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%50 = tail call i32 @exit(i32 1)
unreachable
51: ; preds = %39
%52 = tail call i32 @sign_hashed_data(i32* %26, i8* %27, i32 32)
ret i32 0
}