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

/* Variables and functions */
int ntohl(int const);
int ntohs(int const);
int old_stun_is_command_message_str(const unsigned char *buf, size_t blen,
                                    uint32_t *cookie) {
  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) {
              *cookie = ntohl(((const uint32_t *)(buf))[1]);
              return 1;
            }
          }
        }
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<old_stun_is_command_message_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     120b <old_stun_is_command_message_str+0xeb>
cmpq   $0x14,-0x18(%rbp)
jb     120b <old_stun_is_command_message_str+0xeb>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
cmp    $0x4000,%eax
jl     1176 <old_stun_is_command_message_str+0x56>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
cmp    $0x7fff,%eax
jle    1206 <old_stun_is_command_message_str+0xe6>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
and    $0xc0,%eax
cmp    $0x0,%eax
jne    1201 <old_stun_is_command_message_str+0xe1>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <ntohl@plt>
cmp    $0x2112a442,%eax
je     11fc <old_stun_is_command_message_str+0xdc>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0x3,%eax
cmp    $0x0,%eax
jne    11f7 <old_stun_is_command_message_str+0xd7>
mov    -0x24(%rbp),%eax
add    $0x14,%eax
cltq
cmp    -0x18(%rbp),%rax
jne    11f2 <old_stun_is_command_message_str+0xd2>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <ntohl@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1212 <old_stun_is_command_message_str+0xf2>
jmp    11f7 <old_stun_is_command_message_str+0xd7>
jmp    11fc <old_stun_is_command_message_str+0xdc>
jmp    1201 <old_stun_is_command_message_str+0xe1>
jmp    1206 <old_stun_is_command_message_str+0xe6>
jmp    120b <old_stun_is_command_message_str+0xeb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<old_stun_is_command_message_str>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r15d,%r15d
test   %rdi,%rdi
je     119a <old_stun_is_command_message_str+0x7a>
mov    %rsi,%rbp
cmp    $0x14,%rsi
jb     119a <old_stun_is_command_message_str+0x7a>
mov    %rdx,%r14
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1030 <ntohs@plt>
cmp    $0x4000,%eax
jl     115b <old_stun_is_command_message_str+0x3b>
mov    (%rbx),%edi
call   1030 <ntohs@plt>
cmp    $0x8000,%eax
jl     119a <old_stun_is_command_message_str+0x7a>
cmpb   $0x3f,(%rbx)
ja     119a <old_stun_is_command_message_str+0x7a>
mov    0x4(%rbx),%r12d
mov    %r12d,%edi
call   1040 <ntohl@plt>
cmp    $0x2112a442,%eax
je     119a <old_stun_is_command_message_str+0x7a>
mov    %r12d,%edi
call   1030 <ntohs@plt>
test   $0x3,%al
jne    119a <old_stun_is_command_message_str+0x7a>
add    $0x14,%eax
cltq
cmp    %rbp,%rax
jne    119a <old_stun_is_command_message_str+0x7a>
mov    0x4(%rbx),%edi
call   1040 <ntohl@plt>
mov    %eax,(%r14)
mov    $0x1,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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