#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*/ uint8_t;
typedef scalar_t__ uint32_t;
struct macan_key {
  int dummy;
};
struct macan_ctx {
  int dummy;
};

/* Variables and functions */
scalar_t__ htole32(scalar_t__);
int /*<<< orphan*/ macan_aes_cmac(struct macan_key *, unsigned int,
                                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ macan_get_time(struct macan_ctx *);
int memchk(int /*<<< orphan*/ const *, int /*<<< orphan*/ *, int);
scalar_t__ memcmp(int /*<<< orphan*/ const *, int /*<<< orphan*/ *, int);
int macan_check_cmac(struct macan_ctx *ctx, struct macan_key *skey,
                     const uint8_t *cmac4, uint8_t *plain, int time_index,
                     unsigned len) {
  uint8_t cmac[16];
  uint32_t *time_ptr;
  if (time_index < 0 || (unsigned)time_index > len - sizeof(*time_ptr)) {
    macan_aes_cmac(skey, len, cmac, plain);
    return memchk(cmac4, cmac, 4);
  }
  uint32_t time = (uint32_t)macan_get_time(ctx);
  int delta_t;
  time_ptr = (uint32_t *)&plain[time_index];
  for (delta_t = -1; delta_t <= 1; delta_t++) {
    *time_ptr = htole32(time + (uint32_t)delta_t);
    macan_aes_cmac(skey, len, cmac, plain);
    if (memcmp(cmac4, cmac, 4) == 0) {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<macan_check_cmac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9d,-0x30(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1190 <macan_check_cmac+0x40>
mov    -0x2c(%rbp),%eax
mov    -0x30(%rbp),%ecx
sub    $0x8,%rcx
cmp    %rcx,%rax
jbe    11be <macan_check_cmac+0x6e>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%esi
lea    -0x70(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <macan_aes_cmac@plt>
mov    -0x20(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memchk@plt>
mov    %eax,-0x4(%rbp)
jmp    126e <macan_check_cmac+0x11e>
mov    -0x10(%rbp),%rdi
call   1040 <macan_get_time@plt>
mov    %rax,-0x80(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x78(%rbp)
movl   $0xffffffff,-0x84(%rbp)
ff ff ff
cmpl   $0x1,-0x84(%rbp)
jg     1267 <macan_check_cmac+0x117>
mov    -0x80(%rbp),%rdi
movslq -0x84(%rbp),%rax
add    %rax,%rdi
call   1030 <htole32@plt>
mov    %rax,%rcx
mov    -0x78(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%esi
lea    -0x70(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <macan_aes_cmac@plt>
mov    -0x20(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
jne    124e <macan_check_cmac+0xfe>
movl   $0x1,-0x4(%rbp)
jmp    126e <macan_check_cmac+0x11e>
jmp    1253 <macan_check_cmac+0x103>
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11e8 <macan_check_cmac+0x98>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<macan_check_cmac>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9d,%r13d
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rsi,%r15
test   %r8d,%r8d
js     124e <macan_check_cmac+0xfe>
mov    %r13d,%eax
add    $0xfffffffffffffff8,%rax
mov    %r8d,%r14d
cmp    %r14,%rax
jb     124e <macan_check_cmac+0xfe>
call   1040 <macan_get_time@plt>
mov    %rax,%r12
lea    -0x1(%rax),%rdi
call   1030 <htole32@plt>
mov    %rax,(%rbx,%r14,4)
lea    0x10(%rsp),%rdx
mov    %r15,%rdi
mov    %r13d,%esi
mov    %rbx,%rcx
call   1050 <macan_aes_cmac@plt>
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdi
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1276 <macan_check_cmac+0x126>
mov    %r12,%rdi
call   1030 <htole32@plt>
mov    %rax,(%rbx,%r14,4)
lea    0x10(%rsp),%rdx
mov    %r15,%rdi
mov    %r13d,%esi
mov    %rbx,%rcx
call   1050 <macan_aes_cmac@plt>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     1276 <macan_check_cmac+0x126>
add    $0x1,%r12
mov    %r12,%rdi
call   1030 <htole32@plt>
mov    %rax,(%rbx,%r14,4)
lea    0x10(%rsp),%rbp
mov    %r15,%rdi
mov    %r13d,%esi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1050 <macan_aes_cmac@plt>
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
jmp    1276 <macan_check_cmac+0x126>
lea    0x10(%rsp),%r14
mov    %r15,%rdi
mov    %r13d,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <macan_aes_cmac@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <memchk@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.macan_ctx = type { i32 }
%struct.macan_key = type { i32 }
define i32 @macan_check_cmac(%struct.macan_ctx* %0, %struct.macan_key* %1, i32* %2, i32* %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca %struct.macan_ctx*
%9 = alloca %struct.macan_key*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca [16 x i32]
%15 = alloca i64*
%16 = alloca i64
%17 = alloca i32
store %struct.macan_ctx* %0, %struct.macan_ctx** %8
store %struct.macan_key* %1, %struct.macan_key** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%18 = load i32, i32* %12
%19 = icmp slt i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %6
%21 = load i32, i32* %12
%22 = zext i32 %21 to i64
%23 = load i32, i32* %13
%24 = zext i32 %23 to i64
%25 = sub i64 %24, 8
%26 = icmp ugt i64 %22, %25
br i1 %26, label %27, label %36
27: ; preds = %20, %6
%28 = load %struct.macan_key*, %struct.macan_key** %9
%29 = load i32, i32* %13
%30 = getelementptr inbounds [16 x i32], [16 x i32]* %14, i64 0, i64 0
%31 = load i32*, i32** %11
%32 = call i32 @macan_aes_cmac(%struct.macan_key* %28, i32 %29, i32* %30, i32* %31)
%33 = load i32*, i32** %10
%34 = getelementptr inbounds [16 x i32], [16 x i32]* %14, i64 0, i64 0
%35 = call i32 @memchk(i32* %33, i32* %34, i32 4)
store i32 %35, i32* %7
br label %69
36: ; preds = %20
%37 = load %struct.macan_ctx*, %struct.macan_ctx** %8
%38 = call i64 @macan_get_time(%struct.macan_ctx* %37)
store i64 %38, i64* %16
%39 = load i32*, i32** %11
%40 = load i32, i32* %12
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = bitcast i32* %42 to i64*
store i64* %43, i64** %15
store i32 -1, i32* %17
br label %44
44: ; preds = %65, %36
%45 = load i32, i32* %17
%46 = icmp sle i32 %45, 1
br i1 %46, label %47, label %68
47: ; preds = %44
%48 = load i64, i64* %16
%49 = load i32, i32* %17
%50 = sext i32 %49 to i64
%51 = add nsw i64 %48, %50
%52 = call i64 @htole32(i64 %51)
%53 = load i64*, i64** %15
store i64 %52, i64* %53
%54 = load %struct.macan_key*, %struct.macan_key** %9
%55 = load i32, i32* %13
%56 = getelementptr inbounds [16 x i32], [16 x i32]* %14, i64 0, i64 0
%57 = load i32*, i32** %11
%58 = call i32 @macan_aes_cmac(%struct.macan_key* %54, i32 %55, i32* %56, i32* %57)
%59 = load i32*, i32** %10
%60 = getelementptr inbounds [16 x i32], [16 x i32]* %14, i64 0, i64 0
%61 = call i64 @memcmp(i32* %59, i32* %60, i32 4)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %47
store i32 1, i32* %7
br label %69
64: ; preds = %47
br label %65
65: ; preds = %64
%66 = load i32, i32* %17
%67 = add nsw i32 %66, 1
store i32 %67, i32* %17
br label %44
68: ; preds = %44
store i32 0, i32* %7
br label %69
69: ; preds = %68, %63, %27
%70 = load i32, i32* %7
ret i32 %70
}

LLVM IR (-O2):
%struct.macan_ctx = type { i32 }
%struct.macan_key = type { i32 }
define i32 @macan_check_cmac(%struct.macan_ctx* %0, %struct.macan_key* %1, i32* %2, i32* %3, i32 %4, i32 %5) {
%7 = alloca [16 x i32]
%8 = bitcast [16 x i32]* %7 to i8*
%9 = icmp slt i32 %4, 0
%10 = zext i32 %4 to i64
%11 = zext i32 %5 to i64
%12 = add nsw i64 %11, -8
%13 = icmp ult i64 %12, %10
%14 = select i1 %9, i1 true, i1 %13
br i1 %14, label %15, label %19
15: ; preds = %6
%16 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%17 = call i32 @macan_aes_cmac(%struct.macan_key* %1, i32 %5, i32* %16, i32* %3)
%18 = call i32 @memchk(i32* %2, i32* %16, i32 4)
br label %41
19: ; preds = %6
%20 = tail call i64 @macan_get_time(%struct.macan_ctx* %0)
%21 = getelementptr inbounds i32, i32* %3, i64 %10
%22 = bitcast i32* %21 to i64*
%23 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%24 = add nsw i64 %20, -1
%25 = call i64 @htole32(i64 %24)
store i64 %25, i64* %22
%26 = call i32 @macan_aes_cmac(%struct.macan_key* %1, i32 %5, i32* %23, i32* %3)
%27 = call i64 @memcmp(i32* %2, i32* %23, i32 4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %41, label %29
29: ; preds = %19
%30 = call i64 @htole32(i64 %20)
store i64 %30, i64* %22
%31 = call i32 @macan_aes_cmac(%struct.macan_key* %1, i32 %5, i32* %23, i32* %3)
%32 = call i64 @memcmp(i32* %2, i32* %23, i32 4)
%33 = icmp eq i64 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %29
%35 = add nsw i64 %20, 1
%36 = call i64 @htole32(i64 %35)
store i64 %36, i64* %22
%37 = call i32 @macan_aes_cmac(%struct.macan_key* %1, i32 %5, i32* %23, i32* %3)
%38 = call i64 @memcmp(i32* %2, i32* %23, i32 4)
%39 = icmp eq i64 %38, 0
%40 = zext i1 %39 to i32
br label %41
41: ; preds = %34, %19, %29, %15
%42 = phi i32 [ %18, %15 ], [ 1, %19 ], [ 1, %29 ], [ %40, %34 ]
ret i32 %42
}