#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 /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char const *, char *, int /*<<< orphan*/);
int check_deref_cast(const char *cast, int *bits) {
  if (!strncmp(cast, "*(u8 *)", strlen("*(u8 *)")))
    *bits = 8;
  else if (!strncmp(cast, "*(u16 *)", strlen("*(u16 *)")))
    *bits = 16;
  else if (!strncmp(cast, "*(u32 *)", strlen("*(u32 *)")))
    *bits = 32;
  else if (!strncmp(cast, "*(u64 *)", strlen("*(u64 *)")))
    *bits = 64;
  else
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<check_deref_cast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xec1(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xeaf(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    116e <check_deref_cast+0x4e>
mov    -0x18(%rbp),%rax
movl   $0x8,(%rax)
jmp    1243 <check_deref_cast+0x123>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0xe8b(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe79(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11ac <check_deref_cast+0x8c>
mov    -0x18(%rbp),%rax
movl   $0x10,(%rax)
jmp    123e <check_deref_cast+0x11e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe56(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe44(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11ea <check_deref_cast+0xca>
mov    -0x18(%rbp),%rax
movl   $0x20,(%rax)
jmp    1239 <check_deref_cast+0x119>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe21(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe0f(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1228 <check_deref_cast+0x108>
mov    -0x18(%rbp),%rax
movl   $0x40,(%rax)
jmp    1234 <check_deref_cast+0x114>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <check_deref_cast+0x12a>
jmp    1239 <check_deref_cast+0x119>
jmp    123e <check_deref_cast+0x11e>
jmp    1243 <check_deref_cast+0x123>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_deref_cast>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xece(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11b9 <check_deref_cast+0x99>
lea    0xeb6(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11c0 <check_deref_cast+0xa0>
lea    0xe9f(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11c7 <check_deref_cast+0xa7>
lea    0xe88(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    11d1 <check_deref_cast+0xb1>
mov    $0x40,%eax
jmp    11cc <check_deref_cast+0xac>
mov    $0x8,%eax
jmp    11cc <check_deref_cast+0xac>
mov    $0x10,%eax
jmp    11cc <check_deref_cast+0xac>
mov    $0x20,%eax
mov    %eax,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"*(u8 *)\00"
@.str.1 = [9 x i8] c"*(u16 *)\00"
@.str.2 = [9 x i8] c"*(u32 *)\00"
@.str.3 = [9 x i8] c"*(u64 *)\00"
define i32 @check_deref_cast(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
%6 = load i8*, i8** %4
%7 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strncmp(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i32*, i32** %5
store i32 8, i32* %11
br label %37
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @strncmp(i8* %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = load i32*, i32** %5
store i32 16, i32* %18
br label %36
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @strncmp(i8* %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = load i32*, i32** %5
store i32 32, i32* %25
br label %35
26: ; preds = %19
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @strncmp(i8* %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = load i32*, i32** %5
store i32 64, i32* %32
br label %34
33: ; preds = %26
store i32 -1, i32* %3
br label %38
34: ; preds = %31
br label %35
35: ; preds = %34, %24
br label %36
36: ; preds = %35, %17
br label %37
37: ; preds = %36, %10
store i32 0, i32* %3
br label %38
38: ; preds = %37, %33
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [8 x i8] c"*(u8 *)\00"
@.str.1 = [9 x i8] c"*(u16 *)\00"
@.str.2 = [9 x i8] c"*(u32 *)\00"
@.str.3 = [9 x i8] c"*(u64 *)\00"
define i32 @check_deref_cast(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @strncmp(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14, %10, %6, %2
%19 = phi i32 [ 8, %2 ], [ 16, %6 ], [ 32, %10 ], [ 64, %14 ]
store i32 %19, i32* %1
br label %20
20: ; preds = %18, %14
%21 = phi i32 [ -1, %14 ], [ 0, %18 ]
ret i32 %21
}