#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*/ uint32_t;
typedef int uint16_t;

/* Variables and functions */
int ntohl(int /*<<< orphan*/ const);
int ntohs(int const);
int stun_is_command_message_str(const unsigned char *buf, size_t blen) {
  if (buf && blen >= (20)) {
    if (!((ntohs(((const uint16_t *)buf)[0])) >= 0x4000 &&
          (ntohs(((const uint16_t *)buf)[0])) <= 0x7FFF)) {
      if ((((unsigned char)buf[0]) & ((unsigned char)(0xC0))) == 0) {
        if (ntohl(((const uint32_t *)(buf))[1]) == (0x2112A442)) {
          uint16_t len = ntohs(((const uint16_t *)(buf))[1]);
          if ((len & 0x0003) == 0) {
            if ((size_t)(len + (20)) == blen) {
              return 1;
            }
          }
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<stun_is_command_message_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f3 <stun_is_command_message_str+0xd3>
cmpq   $0x14,-0x18(%rbp)
jb     11f3 <stun_is_command_message_str+0xd3>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
cmp    $0x4000,%eax
jl     1172 <stun_is_command_message_str+0x52>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
cmp    $0x7fff,%eax
jle    11ee <stun_is_command_message_str+0xce>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xc0,%eax
cmp    $0x0,%eax
jne    11e9 <stun_is_command_message_str+0xc9>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <ntohl@plt>
cmp    $0x2112a442,%eax
jne    11e4 <stun_is_command_message_str+0xc4>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x3,%eax
cmp    $0x0,%eax
jne    11df <stun_is_command_message_str+0xbf>
mov    -0x1c(%rbp),%eax
add    $0x14,%eax
cltq
cmp    -0x18(%rbp),%rax
jne    11da <stun_is_command_message_str+0xba>
movl   $0x1,-0x4(%rbp)
jmp    11fa <stun_is_command_message_str+0xda>
jmp    11df <stun_is_command_message_str+0xbf>
jmp    11e4 <stun_is_command_message_str+0xc4>
jmp    11e9 <stun_is_command_message_str+0xc9>
jmp    11ee <stun_is_command_message_str+0xce>
jmp    11f3 <stun_is_command_message_str+0xd3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stun_is_command_message_str>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
test   %rdi,%rdi
je     1185 <stun_is_command_message_str+0x65>
mov    %rsi,%r14
cmp    $0x14,%rsi
jb     1185 <stun_is_command_message_str+0x65>
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1030 <ntohs@plt>
cmp    $0x4000,%eax
jl     1153 <stun_is_command_message_str+0x33>
mov    (%rbx),%edi
call   1030 <ntohs@plt>
cmp    $0x8000,%eax
jl     1185 <stun_is_command_message_str+0x65>
cmpb   $0x3f,(%rbx)
ja     1185 <stun_is_command_message_str+0x65>
mov    0x4(%rbx),%ebx
mov    %ebx,%edi
call   1040 <ntohl@plt>
cmp    $0x2112a442,%eax
jne    1185 <stun_is_command_message_str+0x65>
mov    %ebx,%edi
call   1030 <ntohs@plt>
test   $0x3,%al
jne    1183 <stun_is_command_message_str+0x63>
add    $0x14,%eax
cltq
mov    $0x1,%ebp
cmp    %r14,%rax
je     1185 <stun_is_command_message_str+0x65>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stun_is_command_message_str(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %61
9: ; preds = %2
%10 = load i64, i64* %5
%11 = icmp uge i64 %10, 20
br i1 %11, label %12, label %61
12: ; preds = %9
%13 = load i8*, i8** %4
%14 = bitcast i8* %13 to i32*
%15 = getelementptr inbounds i32, i32* %14, i64 0
%16 = load i32, i32* %15
%17 = call i32 @ntohs(i32 %16)
%18 = icmp sge i32 %17, 16384
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = bitcast i8* %20 to i32*
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
%25 = icmp sle i32 %24, 32767
br i1 %25, label %60, label %26
26: ; preds = %19, %12
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = and i32 %30, 192
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %59
33: ; preds = %26
%34 = load i8*, i8** %4
%35 = bitcast i8* %34 to i32*
%36 = getelementptr inbounds i32, i32* %35, i64 1
%37 = load i32, i32* %36
%38 = call i32 @ntohl(i32 %37)
%39 = icmp eq i32 %38, 554869826
br i1 %39, label %40, label %58
40: ; preds = %33
%41 = load i8*, i8** %4
%42 = bitcast i8* %41 to i32*
%43 = getelementptr inbounds i32, i32* %42, i64 1
%44 = load i32, i32* %43
%45 = call i32 @ntohs(i32 %44)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = and i32 %46, 3
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %40
%50 = load i32, i32* %6
%51 = add nsw i32 %50, 20
%52 = sext i32 %51 to i64
%53 = load i64, i64* %5
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %49
store i32 1, i32* %3
br label %62
56: ; preds = %49
br label %57
57: ; preds = %56, %40
br label %58
58: ; preds = %57, %33
br label %59
59: ; preds = %58, %26
br label %60
60: ; preds = %59, %19
br label %61
61: ; preds = %60, %9, %2
store i32 0, i32* %3
br label %62
62: ; preds = %61, %55
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
define i32 @stun_is_command_message_str(i8* %0, i64 %1) {
%3 = icmp ne i8* %0, null
%4 = icmp ugt i64 %1, 19
%5 = and i1 %3, %4
br i1 %5, label %6, label %33
6: ; preds = %2
%7 = bitcast i8* %0 to i32*
%8 = load i32, i32* %7
%9 = tail call i32 @ntohs(i32 %8)
%10 = icmp sgt i32 %9, 16383
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = load i32, i32* %7
%13 = tail call i32 @ntohs(i32 %12)
%14 = icmp slt i32 %13, 32768
br i1 %14, label %33, label %15
15: ; preds = %11, %6
%16 = load i8, i8* %0
%17 = icmp ult i8 %16, 64
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = getelementptr inbounds i8, i8* %0, i64 4
%20 = bitcast i8* %19 to i32*
%21 = load i32, i32* %20
%22 = tail call i32 @ntohl(i32 %21)
%23 = icmp eq i32 %22, 554869826
br i1 %23, label %24, label %33
24: ; preds = %18
%25 = tail call i32 @ntohs(i32 %21)
%26 = and i32 %25, 3
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = add nsw i32 %25, 20
%30 = sext i32 %29 to i64
%31 = icmp eq i64 %30, %1
br i1 %31, label %33, label %32
32: ; preds = %24, %28
br label %33
33: ; preds = %2, %15, %18, %11, %32, %28
%34 = phi i32 [ 1, %28 ], [ 0, %32 ], [ 0, %11 ], [ 0, %18 ], [ 0, %15 ], [ 0, %2 ]
ret i32 %34
}