#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;
  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    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a5 <update_crypto_hmac+0x45>
cmpq   $0x0,-0x18(%rbp)
je     11a5 <update_crypto_hmac+0x45>
cmpq   $0x0,-0x20(%rbp)
jne    11bf <update_crypto_hmac+0x5f>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <update_crypto_hmac+0x1ed>
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <update_crypto_hmac+0x98>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <update_crypto_hmac+0x1ed>
mov    -0x18(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123a <update_crypto_hmac+0xda>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <update_crypto_hmac+0x1ed>
lea    -0x50(%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     1291 <update_crypto_hmac+0x131>
lea    0xe00(%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    134d <update_crypto_hmac+0x1ed>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     12dd <update_crypto_hmac+0x17d>
lea    0xdd6(%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    134d <update_crypto_hmac+0x1ed>
lea    -0x50(%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     1334 <update_crypto_hmac+0x1d4>
lea    0xda6(%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    134d <update_crypto_hmac+0x1ed>
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    $0x50,%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 [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
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i64, i64* %7
%18 = icmp ne i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16, %13, %3
%20 = 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 %76
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp ne i32* %24, null
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = 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 %76
28: ; preds = %21
%29 = load i8*, i8** %6
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %30, i32** %9
%31 = load i32*, i32** %9
%32 = icmp ne i32* %31, null
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%35 = load i32*, i32** %8
%36 = call i32 @fclose(i32* %35)
store i32 -1, i32* %4
br label %76
37: ; preds = %28
%38 = bitcast [32 x i8]* %10 to i8**
%39 = load i32*, i32** %9
%40 = call i32 @fread(i8** %38, i32 1, i32 32, i32* %39)
%41 = icmp ne i32 32, %40
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 32)
%44 = load i32*, i32** %9
%45 = call i32 @fclose(i32* %44)
%46 = load i32*, i32** %8
%47 = call i32 @fclose(i32* %46)
store i32 -1, i32* %4
br label %76
48: ; preds = %37
%49 = load i32*, i32** %8
%50 = load i64, i64* %7
%51 = load i32, i32* @SEEK_SET
%52 = call i64 @fseek(i32* %49, i64 %50, i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %48
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%56 = load i32*, i32** %9
%57 = call i32 @fclose(i32* %56)
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
store i32 -1, i32* %4
br label %76
60: ; preds = %48
%61 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%62 = load i32*, i32** %8
%63 = call i32 @fwrite(i8* %61, i32 1, i32 32, i32* %62)
%64 = icmp ne i32 32, %63
br i1 %64, label %65, label %71
65: ; preds = %60
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i32 32)
%67 = load i32*, i32** %9
%68 = call i32 @fclose(i32* %67)
%69 = load i32*, i32** %8
%70 = call i32 @fclose(i32* %69)
store i32 -1, i32* %4
br label %76
71: ; preds = %60
%72 = load i32*, i32** %8
%73 = call i32 @fclose(i32* %72)
%74 = load i32*, i32** %9
%75 = call i32 @fclose(i32* %74)
store i32 0, i32* %4
br label %76
76: ; preds = %71, %65, %54, %42, %33, %26, %19
%77 = load i32, i32* %4
ret i32 %77
}

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
}