#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 */
struct s_virtserv_state {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int utilReadInt16(unsigned char const *);
int /*<<< orphan*/ utilWriteInt16(unsigned char *, int);
int virtservDecodeEcho(struct s_virtserv_state *, unsigned char *, int const,
                       unsigned char const *, int const);
int virtservDecodeUDPv6(struct s_virtserv_state *virtserv,
                        unsigned char *outbuf, const int outbuf_len,
                        const unsigned char *inbuf, const int inbuf_len) {
  int portnumber;
  int payload_len;
  if (inbuf_len >= 8) {
    portnumber = utilReadInt16(&inbuf[2]);
    switch (portnumber) {
    case 7:
      payload_len = virtservDecodeEcho(virtserv, &outbuf[8], (outbuf_len - 8),
                                       &inbuf[8], (inbuf_len - 8));
      break;
    default:
      payload_len = 0;
      break;
    }
    if ((payload_len > 0) && (outbuf_len >= (payload_len + 8 + 4))) {
      memcpy(&outbuf[0], &inbuf[2], 2);
      memcpy(&outbuf[2], &inbuf[0], 2);
      utilWriteInt16(&outbuf[4], (payload_len + 8));
      memset(&outbuf[6], 0, 2);
      memset(&outbuf[(payload_len + 8)], 0, 4);
      return (payload_len + 8);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<virtservDecodeUDPv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x8,-0x2c(%rbp)
jl     126b <virtservDecodeUDPv6+0x11b>
mov    -0x28(%rbp),%rdi
add    $0x2,%rdi
call   1040 <utilReadInt16@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
sub    $0x7,%eax
jne    11c5 <virtservDecodeUDPv6+0x75>
jmp    1196 <virtservDecodeUDPv6+0x46>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x8,%rsi
mov    -0x1c(%rbp),%edx
sub    $0x8,%edx
mov    -0x28(%rbp),%rcx
add    $0x8,%rcx
mov    -0x2c(%rbp),%r8d
sub    $0x8,%r8d
call   1060 <virtservDecodeEcho@plt>
mov    %eax,-0x34(%rbp)
jmp    11cc <virtservDecodeUDPv6+0x7c>
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1266 <virtservDecodeUDPv6+0x116>
mov    -0x1c(%rbp),%eax
mov    -0x34(%rbp),%ecx
add    $0x8,%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
jl     1266 <virtservDecodeUDPv6+0x116>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x2,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rdi
add    $0x2,%rdi
mov    -0x28(%rbp),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rdi
add    $0x4,%rdi
mov    -0x34(%rbp),%esi
add    $0x8,%esi
call   1070 <utilWriteInt16@plt>
mov    -0x18(%rbp),%rdi
add    $0x6,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%eax
add    $0x8,%eax
cltq
add    %rax,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x34(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x4(%rbp)
jmp    1272 <virtservDecodeUDPv6+0x122>
jmp    126b <virtservDecodeUDPv6+0x11b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<virtservDecodeUDPv6>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebp,%ebp
cmp    $0x8,%r8d
jl     120e <virtservDecodeUDPv6+0xbe>
mov    %r8d,%ebx
mov    %rcx,%r15
mov    %edx,%r13d
mov    %rdi,%r14
mov    %rsi,(%rsp)
lea    0x2(%rcx),%r12
mov    %r12,%rdi
call   1040 <utilReadInt16@plt>
cmp    $0x7,%eax
jne    120e <virtservDecodeUDPv6+0xbe>
mov    (%rsp),%rax
lea    0x8(%rax),%rsi
lea    -0x8(%r13),%edx
lea    0x8(%r15),%rcx
add    $0xfffffff8,%ebx
mov    %r14,%rdi
mov    %ebx,%r8d
call   1060 <virtservDecodeEcho@plt>
test   %eax,%eax
jle    120e <virtservDecodeUDPv6+0xbe>
mov    %eax,%ebx
lea    0xc(%rbx),%eax
cmp    %r13d,%eax
jg     120e <virtservDecodeUDPv6+0xbe>
add    $0x8,%ebx
mov    (%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
lea    0x2(%rbp),%rdi
mov    %r15,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
lea    0x4(%rbp),%rdi
mov    %ebx,%esi
call   1070 <utilWriteInt16@plt>
lea    0x6(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1030 <memset@plt>
mov    %ebx,%edi
add    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.s_virtserv_state = type { i32 }
define i32 @virtservDecodeUDPv6(%struct.s_virtserv_state* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.s_virtserv_state*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store %struct.s_virtserv_state* %0, %struct.s_virtserv_state** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%14 = load i32, i32* %11
%15 = icmp sge i32 %14, 8
br i1 %15, label %16, label %70
16: ; preds = %5
%17 = load i8*, i8** %10
%18 = getelementptr inbounds i8, i8* %17, i64 2
%19 = call i32 @utilReadInt16(i8* %18)
store i32 %19, i32* %12
%20 = load i32, i32* %12
switch i32 %20, label %32 [
i32 7, label %21
]
21: ; preds = %16
%22 = load %struct.s_virtserv_state*, %struct.s_virtserv_state** %7
%23 = load i8*, i8** %8
%24 = getelementptr inbounds i8, i8* %23, i64 8
%25 = load i32, i32* %9
%26 = sub nsw i32 %25, 8
%27 = load i8*, i8** %10
%28 = getelementptr inbounds i8, i8* %27, i64 8
%29 = load i32, i32* %11
%30 = sub nsw i32 %29, 8
%31 = call i32 @virtservDecodeEcho(%struct.s_virtserv_state* %22, i8* %24, i32 %26, i8* %28, i32 %30)
store i32 %31, i32* %13
br label %33
32: ; preds = %16
store i32 0, i32* %13
br label %33
33: ; preds = %32, %21
%34 = load i32, i32* %13
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %69
36: ; preds = %33
%37 = load i32, i32* %9
%38 = load i32, i32* %13
%39 = add nsw i32 %38, 8
%40 = add nsw i32 %39, 4
%41 = icmp sge i32 %37, %40
br i1 %41, label %42, label %69
42: ; preds = %36
%43 = load i8*, i8** %8
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8*, i8** %10
%46 = getelementptr inbounds i8, i8* %45, i64 2
%47 = call i32 @memcpy(i8* %44, i8* %46, i32 2)
%48 = load i8*, i8** %8
%49 = getelementptr inbounds i8, i8* %48, i64 2
%50 = load i8*, i8** %10
%51 = getelementptr inbounds i8, i8* %50, i64 0
%52 = call i32 @memcpy(i8* %49, i8* %51, i32 2)
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 4
%55 = load i32, i32* %13
%56 = add nsw i32 %55, 8
%57 = call i32 @utilWriteInt16(i8* %54, i32 %56)
%58 = load i8*, i8** %8
%59 = getelementptr inbounds i8, i8* %58, i64 6
%60 = call i32 @memset(i8* %59, i32 0, i32 2)
%61 = load i8*, i8** %8
%62 = load i32, i32* %13
%63 = add nsw i32 %62, 8
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %61, i64 %64
%66 = call i32 @memset(i8* %65, i32 0, i32 4)
%67 = load i32, i32* %13
%68 = add nsw i32 %67, 8
store i32 %68, i32* %6
br label %71
69: ; preds = %36, %33
br label %70
70: ; preds = %69, %5
store i32 0, i32* %6
br label %71
71: ; preds = %70, %42
%72 = load i32, i32* %6
ret i32 %72
}

LLVM IR (-O2):
%struct.s_virtserv_state = type { i32 }
define i32 @virtservDecodeUDPv6(%struct.s_virtserv_state* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = icmp sgt i32 %4, 7
br i1 %6, label %7, label %33
7: ; preds = %5
%8 = getelementptr inbounds i8, i8* %3, i64 2
%9 = tail call i32 @utilReadInt16(i8* %8)
%10 = icmp eq i32 %9, 7
br i1 %10, label %11, label %33
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %1, i64 8
%13 = add nsw i32 %2, -8
%14 = getelementptr inbounds i8, i8* %3, i64 8
%15 = add nsw i32 %4, -8
%16 = tail call i32 @virtservDecodeEcho(%struct.s_virtserv_state* %0, i8* %12, i32 %13, i8* %14, i32 %15)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %11
%19 = add nuw nsw i32 %16, 8
%20 = add nuw nsw i32 %16, 12
%21 = icmp sgt i32 %20, %2
br i1 %21, label %33, label %22
22: ; preds = %18
%23 = tail call i32 @memcpy(i8* %1, i8* %8, i32 2)
%24 = getelementptr inbounds i8, i8* %1, i64 2
%25 = tail call i32 @memcpy(i8* %24, i8* %3, i32 2)
%26 = getelementptr inbounds i8, i8* %1, i64 4
%27 = tail call i32 @utilWriteInt16(i8* %26, i32 %19)
%28 = getelementptr inbounds i8, i8* %1, i64 6
%29 = tail call i32 @memset(i8* %28, i32 0, i32 2)
%30 = zext i32 %19 to i64
%31 = getelementptr inbounds i8, i8* %1, i64 %30
%32 = tail call i32 @memset(i8* %31, i32 0, i32 4)
br label %33
33: ; preds = %7, %5, %18, %11, %22
%34 = phi i32 [ %19, %22 ], [ 0, %11 ], [ 0, %18 ], [ 0, %5 ], [ 0, %7 ]
ret i32 %34
}