#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 ntohl(unsigned int);
int ntohs(unsigned short);
int ILibTURN_GetStunPacketLength(char *buffer, int offset, int length) {
  unsigned short messageLength =
      20 + ntohs(((unsigned short *)(buffer + offset))[1]);
  int magic = ntohl(((unsigned int *)(buffer + offset))[1]);
  if (messageLength > length || magic != 0x2112A442) {
    if (magic != 0x2112A442)
      return ((int)messageLength);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ILibTURN_GetStunPacketLength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzwl 0x2(%rax,%rcx,1),%edi
call   1030 <ntohs@plt>
add    $0x14,%eax
mov    %ax,-0x1a(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    0x4(%rax,%rcx,1),%edi
call   1040 <ntohl@plt>
mov    %eax,-0x20(%rbp)
movzwl -0x1a(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1179 <ILibTURN_GetStunPacketLength+0x59>
cmpl   $0x2112a442,-0x20(%rbp)
je     1197 <ILibTURN_GetStunPacketLength+0x77>
cmpl   $0x2112a442,-0x20(%rbp)
je     1192 <ILibTURN_GetStunPacketLength+0x72>
movzwl -0x1a(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119e <ILibTURN_GetStunPacketLength+0x7e>
jmp    1197 <ILibTURN_GetStunPacketLength+0x77>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ILibTURN_GetStunPacketLength>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rdi,%r15
movslq %esi,%rbx
movzwl 0x2(%rdi,%rbx,1),%edi
call   1030 <ntohs@plt>
mov    %eax,%ebp
add    $0x14,%ebp
mov    0x4(%r15,%rbx,1),%edi
call   1040 <ntohl@plt>
movzwl %bp,%edx
xor    %ecx,%ecx
cmp    $0x2112a442,%eax
mov    $0x0,%eax
cmovne %edx,%eax
cmovne %eax,%ecx
cmp    %r14d,%edx
cmovg  %eax,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ILibTURN_GetStunPacketLength(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i16
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = bitcast i8* %13 to i16*
%15 = getelementptr inbounds i16, i16* %14, i64 1
%16 = load i16, i16* %15
%17 = call i32 @ntohs(i16 zeroext %16)
%18 = add nsw i32 20, %17
%19 = trunc i32 %18 to i16
store i16 %19, i16* %8
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = bitcast i8* %23 to i32*
%25 = getelementptr inbounds i32, i32* %24, i64 1
%26 = load i32, i32* %25
%27 = call i32 @ntohl(i32 %26)
store i32 %27, i32* %9
%28 = load i16, i16* %8
%29 = zext i16 %28 to i32
%30 = load i32, i32* %7
%31 = icmp sgt i32 %29, %30
br i1 %31, label %35, label %32
32: ; preds = %3
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 554869826
br i1 %34, label %35, label %42
35: ; preds = %32, %3
%36 = load i32, i32* %9
%37 = icmp ne i32 %36, 554869826
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i16, i16* %8
%40 = zext i16 %39 to i32
store i32 %40, i32* %4
br label %43
41: ; preds = %35
br label %42
42: ; preds = %41, %32
store i32 0, i32* %4
br label %43
43: ; preds = %42, %38
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
define i32 @ILibTURN_GetStunPacketLength(i8* nocapture %0, i32 %1, i32 %2) {
%4 = sext i32 %1 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = getelementptr inbounds i8, i8* %5, i64 2
%7 = bitcast i8* %6 to i16*
%8 = load i16, i16* %7
%9 = tail call i32 @ntohs(i16 zeroext %8)
%10 = add i32 %9, 20
%11 = getelementptr inbounds i8, i8* %5, i64 4
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
%14 = tail call i32 @ntohl(i32 %13)
%15 = and i32 %10, 65535
%16 = icmp sgt i32 %15, %2
%17 = icmp ne i32 %14, 554869826
%18 = select i1 %16, i1 true, i1 %17
%19 = select i1 %18, i1 %17, i1 false
%20 = select i1 %19, i32 %15, i32 0
ret i32 %20
}