#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 errno;
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, unsigned char *, unsigned int);
char *strerror(int);
scalar_t__ write(int, unsigned char *, int);
int getMPI(int pkey_file, int module_file, char tag) {
  unsigned char c;
  unsigned int len;
  unsigned char *key;
  int i;
  int key_offset = 0;
  key = (unsigned char *)malloc(512 + 1);
  if (key == NULL) {
    printf("Cannot allocate key buffer\n");
    return -1;
  }
  key[key_offset++] = tag;
  read(pkey_file, &c, 1);
  key[key_offset++] = c;
  len = c << 8;
  read(pkey_file, &c, 1);
  key[key_offset++] = c;
  len |= c;
  len = (len + 7) / 8;
  read(pkey_file, &key[key_offset], len);
  key_offset += len;
  if (write(module_file, key, key_offset) < 0) {
    printf("Write problem %i %s\n", errno, strerror(errno));
    return -1;
  }
  printf("MPI: %c (len=%d)\n{ ", tag, len);
  for (i = 0; i < key_offset; i++) {
    printf("0x%02X, ", key[i] & 0xff);
  }
  printf("}\n");
  return 0;
}


x86 assembly (clang, -O0):
<getMPI>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x201,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119d <getMPI+0x4d>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <getMPI+0x1b4>
mov    -0xd(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    -0xe(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movzbl -0xe(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    -0xe(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movzbl -0xe(%rbp),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x7,%eax
shr    $0x3,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1050 <read@plt>
mov    -0x14(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1299 <getMPI+0x149>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%esi
mov    %rax,%rdx
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <getMPI+0x1b4>
movsbl -0xd(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12ef <getMPI+0x19f>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
and    $0xff,%esi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12b5 <getMPI+0x165>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMPI>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x201,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1249 <getMPI+0xe9>
mov    %rax,%r13
mov    %r15b,(%rax)
lea    0x7(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movzbl 0x7(%rsp),%ebx
mov    %bl,0x1(%r13)
shl    $0x8,%rbx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movzbl 0x7(%rsp),%eax
mov    %al,0x2(%r13)
lea    (%rbx,%rax,1),%r12
add    $0x7,%r12
shr    $0x3,%r12
mov    %r13,%rsi
add    $0x3,%rsi
mov    %ebp,%edi
mov    %r12d,%edx
call   1060 <read@plt>
mov    %r12,%rbp
add    $0x3,%rbp
mov    %r14d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
test   %rax,%rax
js     1257 <getMPI+0xf7>
movsbl %r15b,%esi
lea    0xe0f(%rip),%rdi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe12(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
movzbl 0x0(%r13,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1220 <getMPI+0xc0>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    127f <getMPI+0x11f>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
jmp    127a <getMPI+0x11a>
mov    0x2d82(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xd92(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Cannot allocate key buffer\0A\00"
@.str.1 = [21 x i8] c"Write problem %i %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"MPI: %c (len=%d)\0A{ \00"
@.str.3 = [9 x i8] c"0x%02X, \00"
@.str.4 = [3 x i8] c"}\0A\00"
define i32 @getMPI(i32 %0, i32 %1, i8 signext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8 %2, i8* %7
store i32 0, i32* %12
%13 = call i64 @malloc(i32 513)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %95
19: ; preds = %3
%20 = load i8, i8* %7
%21 = load i8*, i8** %10
%22 = load i32, i32* %12
%23 = add nsw i32 %22, 1
store i32 %23, i32* %12
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
%26 = load i32, i32* %5
%27 = call i32 @read(i32 %26, i8* %8, i32 1)
%28 = load i8, i8* %8
%29 = load i8*, i8** %10
%30 = load i32, i32* %12
%31 = add nsw i32 %30, 1
store i32 %31, i32* %12
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 %28, i8* %33
%34 = load i8, i8* %8
%35 = zext i8 %34 to i32
%36 = shl i32 %35, 8
store i32 %36, i32* %9
%37 = load i32, i32* %5
%38 = call i32 @read(i32 %37, i8* %8, i32 1)
%39 = load i8, i8* %8
%40 = load i8*, i8** %10
%41 = load i32, i32* %12
%42 = add nsw i32 %41, 1
store i32 %42, i32* %12
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 %39, i8* %44
%45 = load i8, i8* %8
%46 = zext i8 %45 to i32
%47 = load i32, i32* %9
%48 = or i32 %47, %46
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = add i32 %49, 7
%51 = udiv i32 %50, 8
store i32 %51, i32* %9
%52 = load i32, i32* %5
%53 = load i8*, i8** %10
%54 = load i32, i32* %12
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i32, i32* %9
%58 = call i32 @read(i32 %52, i8* %56, i32 %57)
%59 = load i32, i32* %9
%60 = load i32, i32* %12
%61 = add i32 %60, %59
store i32 %61, i32* %12
%62 = load i32, i32* %6
%63 = load i8*, i8** %10
%64 = load i32, i32* %12
%65 = call i64 @write(i32 %62, i8* %63, i32 %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %19
%68 = load i32, i32* @errno
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %68, i8* %70)
store i32 -1, i32* %4
br label %95
72: ; preds = %19
%73 = load i8, i8* %7
%74 = sext i8 %73 to i32
%75 = load i32, i32* %9
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %74, i32 %75)
store i32 0, i32* %11
br label %77
77: ; preds = %90, %72
%78 = load i32, i32* %11
%79 = load i32, i32* %12
%80 = icmp slt i32 %78, %79
br i1 %80, label %81, label %93
81: ; preds = %77
%82 = load i8*, i8** %10
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = load i8, i8* %85
%87 = zext i8 %86 to i32
%88 = and i32 %87, 255
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %88)
br label %90
90: ; preds = %81
%91 = load i32, i32* %11
%92 = add nsw i32 %91, 1
store i32 %92, i32* %11
br label %77
93: ; preds = %77
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %4
br label %95
95: ; preds = %93, %67, %17
%96 = load i32, i32* %4
ret i32 %96
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"Write problem %i %s\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"MPI: %c (len=%d)\0A{ \00"
@.str.3 = [9 x i8] c"0x%02X, \00"
@str = [2 x i8] c"}\00"
@str.5 = [27 x i8] c"Cannot allocate key buffer\00"
define i32 @getMPI(i32 %0, i32 %1, i8 signext %2) {
%4 = alloca i8
%5 = tail call i64 @malloc(i32 513)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
br label %46
10: ; preds = %3
store i8 %2, i8* %6
%11 = call i32 @read(i32 %0, i8* %4, i32 1)
%12 = load i8, i8* %4
%13 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %12, i8* %13
%14 = zext i8 %12 to i32
%15 = shl nuw nsw i32 %14, 8
%16 = call i32 @read(i32 %0, i8* %4, i32 1)
%17 = load i8, i8* %4
%18 = getelementptr inbounds i8, i8* %6, i64 2
store i8 %17, i8* %18
%19 = zext i8 %17 to i32
%20 = or i32 %15, %19
%21 = add nuw nsw i32 %20, 7
%22 = lshr i32 %21, 3
%23 = getelementptr inbounds i8, i8* %6, i64 3
%24 = tail call i32 @read(i32 %0, i8* %23, i32 %22)
%25 = add nuw nsw i32 %22, 3
%26 = tail call i64 @write(i32 %1, i8* %6, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %10
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* %30)
br label %46
32: ; preds = %10
%33 = sext i8 %2 to i32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %22)
%35 = zext i32 %25 to i64
br label %36
36: ; preds = %32, %36
%37 = phi i64 [ 0, %32 ], [ %42, %36 ]
%38 = getelementptr inbounds i8, i8* %6, i64 %37
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = add nuw nsw i64 %37, 1
%43 = icmp eq i64 %42, %35
br i1 %43, label %44, label %36
44: ; preds = %36
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %46
46: ; preds = %44, %28, %8
%47 = phi i32 [ -1, %8 ], [ -1, %28 ], [ 0, %44 ]
ret i32 %47
}