#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;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char **, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, unsigned long, int /*<<< orphan*/);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int update_crypto_hmac(const char *vmlinux_path, const char *hmac_path,
                       unsigned long offset) {
  FILE *vmlinux_fp = NULL;
  FILE *hmac_fp = NULL;
  int i = 0, j = 0;
  unsigned char hmac[32];
  if (!vmlinux_path || !hmac_path || !offset) {
    printf("FIPS update_crypto_hmac : Invalid Params");
    return -1;
  }
  vmlinux_fp = fopen(vmlinux_path, "r+b");
  if (!vmlinux_fp) {
    printf("Unable to open vmlinux file ");
    return -1;
  }
  hmac_fp = fopen(hmac_path, "rb");
  if (!hmac_fp) {
    printf("Unable to open hmac file ");
    fclose(vmlinux_fp);
    return -1;
  }
  if (32 != fread(&hmac, sizeof(unsigned char), 32, hmac_fp)) {
    printf("Unable to read %d bytes from hmac file", 32);
    fclose(hmac_fp);
    fclose(vmlinux_fp);
    return -1;
  }
  if (fseek(vmlinux_fp, offset, SEEK_SET) != 0) {
    printf("Unable to seek into vmlinux file.");
    fclose(hmac_fp);
    fclose(vmlinux_fp);
    return -1;
  }
  if (32 != fwrite(hmac, sizeof(unsigned char), 32, vmlinux_fp)) {
    printf("Unable to write %d byte into vmlinux", 32);
    fclose(hmac_fp);
    fclose(vmlinux_fp);
    return -1;
  }
  fclose(vmlinux_fp);
  fclose(hmac_fp);
  return 0;
}


x86 assembly (clang, -O0):
<update_crypto_hmac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b3 <update_crypto_hmac+0x53>
cmpq   $0x0,-0x18(%rbp)
je     11b3 <update_crypto_hmac+0x53>
cmpq   $0x0,-0x20(%rbp)
jne    11cd <update_crypto_hmac+0x6d>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <update_crypto_hmac+0x1fb>
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1206 <update_crypto_hmac+0xa6>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <update_crypto_hmac+0x1fb>
mov    -0x18(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1248 <update_crypto_hmac+0xe8>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <update_crypto_hmac+0x1fb>
lea    -0x60(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x20,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
je     129f <update_crypto_hmac+0x13f>
lea    0xdf2(%rip),%rdi
mov    $0x20,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <update_crypto_hmac+0x1fb>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     12eb <update_crypto_hmac+0x18b>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <update_crypto_hmac+0x1fb>
lea    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x20,%edx
call   1080 <fwrite@plt>
mov    %eax,%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
je     1342 <update_crypto_hmac+0x1e2>
lea    0xd98(%rip),%rdi
mov    $0x20,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135b <update_crypto_hmac+0x1fb>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_crypto_hmac>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     11ee <update_crypto_hmac+0x8e>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11ee <update_crypto_hmac+0x8e>
mov    %rdx,%r12
test   %rdx,%rdx
je     11ee <update_crypto_hmac+0x8e>
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f7 <update_crypto_hmac+0x97>
mov    %rax,%r15
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1207 <update_crypto_hmac+0xa7>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x20,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x20,%eax
jne    1217 <update_crypto_hmac+0xb7>
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <fseek@plt>
test   %rax,%rax
je     124b <update_crypto_hmac+0xeb>
lea    0xea9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    122a <update_crypto_hmac+0xca>
lea    0xe0b(%rip),%rdi
jmp    11fe <update_crypto_hmac+0x9e>
lea    0xe2f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123a <update_crypto_hmac+0xda>
lea    0xe3f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1232 <update_crypto_hmac+0xd2>
lea    0xe49(%rip),%rdi
mov    $0x20,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x20,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
cmp    $0x20,%eax
jne    1279 <update_crypto_hmac+0x119>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    123f <update_crypto_hmac+0xdf>
lea    0xe30(%rip),%rdi
jmp    121e <update_crypto_hmac+0xbe>

LLVM IR (-O0):
@.str = [41 x i8] c"FIPS update_crypto_hmac : Invalid Params\00"
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [29 x i8] c"Unable to open vmlinux file \00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [26 x i8] c"Unable to open hmac file \00"
@.str.5 = [39 x i8] c"Unable to read %d bytes from hmac file\00"
@SEEK_SET = global i32 0
@.str.6 = [34 x i8] c"Unable to seek into vmlinux file.\00"
@.str.7 = [37 x i8] c"Unable to write %d byte into vmlinux\00"
define i32 @update_crypto_hmac(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [32 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32* null, i32** %8
store i32* null, i32** %9
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i64, i64* %7
%20 = icmp ne i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18, %15, %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %78
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp ne i32* %26, null
br i1 %27, label %30, label %28
28: ; preds = %23
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %78
30: ; preds = %23
%31 = load i8*, i8** %6
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %32, i32** %9
%33 = load i32*, i32** %9
%34 = icmp ne i32* %33, null
br i1 %34, label %39, label %35
35: ; preds = %30
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32*, i32** %8
%38 = call i32 @fclose(i32* %37)
store i32 -1, i32* %4
br label %78
39: ; preds = %30
%40 = bitcast [32 x i8]* %12 to i8**
%41 = load i32*, i32** %9
%42 = call i32 @fread(i8** %40, i32 1, i32 32, i32* %41)
%43 = icmp ne i32 32, %42
br i1 %43, label %44, label %50
44: ; preds = %39
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 32)
%46 = load i32*, i32** %9
%47 = call i32 @fclose(i32* %46)
%48 = load i32*, i32** %8
%49 = call i32 @fclose(i32* %48)
store i32 -1, i32* %4
br label %78
50: ; preds = %39
%51 = load i32*, i32** %8
%52 = load i64, i64* %7
%53 = load i32, i32* @SEEK_SET
%54 = call i64 @fseek(i32* %51, i64 %52, i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%58 = load i32*, i32** %9
%59 = call i32 @fclose(i32* %58)
%60 = load i32*, i32** %8
%61 = call i32 @fclose(i32* %60)
store i32 -1, i32* %4
br label %78
62: ; preds = %50
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %12, i64 0, i64 0
%64 = load i32*, i32** %8
%65 = call i32 @fwrite(i8* %63, i32 1, i32 32, i32* %64)
%66 = icmp ne i32 32, %65
br i1 %66, label %67, label %73
67: ; preds = %62
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i32 32)
%69 = load i32*, i32** %9
%70 = call i32 @fclose(i32* %69)
%71 = load i32*, i32** %8
%72 = call i32 @fclose(i32* %71)
store i32 -1, i32* %4
br label %78
73: ; preds = %62
%74 = load i32*, i32** %8
%75 = call i32 @fclose(i32* %74)
%76 = load i32*, i32** %9
%77 = call i32 @fclose(i32* %76)
store i32 0, i32* %4
br label %78
78: ; preds = %73, %67, %56, %44, %35, %28, %21
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
@.str = [41 x i8] c"FIPS update_crypto_hmac : Invalid Params\00"
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [29 x i8] c"Unable to open vmlinux file \00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [26 x i8] c"Unable to open hmac file \00"
@.str.5 = [39 x i8] c"Unable to read %d bytes from hmac file\00"
@SEEK_SET = global i32 0
@.str.6 = [34 x i8] c"Unable to seek into vmlinux file.\00"
@.str.7 = [37 x i8] c"Unable to write %d byte into vmlinux\00"
define i32 @update_crypto_hmac(i8* %0, i8* %1, i64 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = icmp ne i8* %0, null
%7 = icmp ne i8* %1, null
%8 = and i1 %6, %7
%9 = icmp ne i64 %2, 0
%10 = and i1 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %50
13: ; preds = %3
%14 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %50
18: ; preds = %13
%19 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 @fclose(i32* %14)
br label %50
24: ; preds = %18
%25 = bitcast [32 x i8]* %4 to i8**
%26 = call i32 @fread(i8** %25, i32 1, i32 32, i32* %19)
%27 = icmp eq i32 %26, 32
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 32)
%30 = tail call i32 @fclose(i32* %19)
%31 = tail call i32 @fclose(i32* %14)
br label %50
32: ; preds = %24
%33 = load i32, i32* @SEEK_SET
%34 = tail call i64 @fseek(i32* %14, i64 %2, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%38 = tail call i32 @fclose(i32* %19)
%39 = tail call i32 @fclose(i32* %14)
br label %50
40: ; preds = %32
%41 = call i32 @fwrite(i8* %5, i32 1, i32 32, i32* %14)
%42 = icmp eq i32 %41, 32
br i1 %42, label %47, label %43
43: ; preds = %40
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i32 32)
%45 = tail call i32 @fclose(i32* %19)
%46 = tail call i32 @fclose(i32* %14)
br label %50
47: ; preds = %40
%48 = tail call i32 @fclose(i32* %14)
%49 = tail call i32 @fclose(i32* %19)
br label %50
50: ; preds = %47, %43, %36, %28, %21, %16, %11
%51 = phi i32 [ -1, %28 ], [ -1, %36 ], [ -1, %43 ], [ 0, %47 ], [ -1, %21 ], [ -1, %16 ], [ -1, %11 ]
ret i32 %51
}