#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 int /*<<< orphan*/ tok;

/* Variables and functions */
int /*<<< orphan*/ get_totp_sha512(int /*<<< orphan*/ const *, size_t,
                                   unsigned int, char *, int);
int /*<<< orphan*/ strcmp(char const *, char *);
int is_valid(const uint8_t *key, size_t keylen, unsigned time,
             const char *token) {
  char tok[9];
  int i;
  for (i = -1; i <= 0; ++i) {
    get_totp_sha512(key, keylen, time + i, tok, sizeof(tok));
    if (!strcmp(token, tok))
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_valid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jg     119a <is_valid+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    -0x38(%rbp),%edx
lea    -0x31(%rbp),%rcx
mov    $0x9,%r8d
call   1040 <get_totp_sha512@plt>
mov    -0x28(%rbp),%rdi
lea    -0x31(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1187 <is_valid+0x67>
movl   $0x1,-0x4(%rbp)
jmp    11a1 <is_valid+0x81>
jmp    118c <is_valid+0x6c>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    113e <is_valid+0x1e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbx
lea    -0x1(%r15),%edx
lea    0x7(%rsp),%r13
mov    %r13,%rcx
mov    $0x9,%r8d
call   1040 <get_totp_sha512@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1030 <strcmp@plt>
test   %eax,%eax
je     1191 <is_valid+0x71>
lea    0x7(%rsp),%r13
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %r13,%rcx
mov    $0x9,%r8d
call   1040 <get_totp_sha512@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1030 <strcmp@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
sete   %al
jmp    1196 <is_valid+0x76>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @is_valid(i32* %0, i64 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [9 x i8]
%11 = alloca i32
store i32* %0, i32** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 -1, i32* %11
br label %12
12: ; preds = %29, %4
%13 = load i32, i32* %11
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %12
%16 = load i32*, i32** %6
%17 = load i64, i64* %7
%18 = load i32, i32* %8
%19 = load i32, i32* %11
%20 = add i32 %18, %19
%21 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%22 = call i32 @get_totp_sha512(i32* %16, i64 %17, i32 %20, i8* %21, i32 9)
%23 = load i8*, i8** %9
%24 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%25 = call i32 @strcmp(i8* %23, i8* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %15
store i32 1, i32* %5
br label %33
28: ; preds = %15
br label %29
29: ; preds = %28
%30 = load i32, i32* %11
%31 = add nsw i32 %30, 1
store i32 %31, i32* %11
br label %12
32: ; preds = %12
store i32 0, i32* %5
br label %33
33: ; preds = %32, %27
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
define i32 @is_valid(i32* %0, i64 %1, i32 %2, i8* nocapture %3) {
%5 = alloca [9 x i8]
%6 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%7 = add i32 %2, -1
%8 = call i32 @get_totp_sha512(i32* %0, i64 %1, i32 %7, i8* %6, i32 9)
%9 = call i32 @strcmp(i8* dereferenceable(1) %3, i8* %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = call i32 @get_totp_sha512(i32* %0, i64 %1, i32 %2, i8* %6, i32 9)
%13 = call i32 @strcmp(i8* dereferenceable(1) %3, i8* %6)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %11, %4
%17 = phi i32 [ 1, %4 ], [ %15, %11 ]
ret i32 %17
}