#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 skynet_context {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, void *, size_t);
int skynet_context_newsession(struct skynet_context *);
void _filter_args(struct skynet_context *context, int type, int *session,
                  void **data, size_t *sz) {
  int needcopy = !(type & 0x10000);
  int allocsession = type & 0x20000;
  type &= 0xff;
  if (allocsession) {
    assert(*session == 0);
    *session = skynet_context_newsession(context);
  }
  if (needcopy && *data) {
    char *msg = malloc(*sz + 1);
    memcpy(msg, *data, *sz);
    msg[*sz] = '\0';
    *data = msg;
  }
  *sz |= (size_t)type << ((sizeof(size_t) - 1) * 8);
}


x86 assembly (clang, -O0):
<_filter_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0xc(%rbp),%eax
and    $0x10000,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
and    $0x20000,%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     11b8 <_filter_args+0x78>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1060 <skynet_context_newsession@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x2c(%rbp)
je     1215 <_filter_args+0xd5>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1215 <_filter_args+0xd5>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movslq -0xc(%rbp),%rcx
shl    $0x38,%rcx
mov    -0x28(%rbp),%rax
or     (%rax),%rcx
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_filter_args>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %esi,%ebx
test   $0x20000,%esi
je     117b <_filter_args+0x3b>
mov    %rdx,%r12
mov    %rdi,%r13
xor    %edi,%edi
cmpl   $0x0,(%rdx)
sete   %dil
call   1030 <assert@plt>
mov    %r13,%rdi
call   1060 <skynet_context_newsession@plt>
mov    %eax,(%r12)
test   $0x10000,%ebx
jne    11b3 <_filter_args+0x73>
mov    (%r15),%r12
test   %r12,%r12
je     11b3 <_filter_args+0x73>
mov    (%r14),%r13
lea    0x1(%r13),%rdi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
mov    (%r14),%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,(%r15)
shl    $0x38,%rbx
or     %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.skynet_context = type { i32 }
define void @_filter_args(%struct.skynet_context* %0, i32 %1, i32* %2, i8** %3, i64* %4) {
%6 = alloca %struct.skynet_context*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8**
%10 = alloca i64*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store %struct.skynet_context* %0, %struct.skynet_context** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i8** %3, i8*** %9
store i64* %4, i64** %10
%14 = load i32, i32* %7
%15 = and i32 %14, 65536
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
%18 = zext i1 %17 to i32
store i32 %18, i32* %11
%19 = load i32, i32* %7
%20 = and i32 %19, 131072
store i32 %20, i32* %12
%21 = load i32, i32* %7
%22 = and i32 %21, 255
store i32 %22, i32* %7
%23 = load i32, i32* %12
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %5
%26 = load i32*, i32** %8
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load %struct.skynet_context*, %struct.skynet_context** %6
%32 = call i32 @skynet_context_newsession(%struct.skynet_context* %31)
%33 = load i32*, i32** %8
store i32 %32, i32* %33
br label %34
34: ; preds = %25, %5
%35 = load i32, i32* %11
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %58
37: ; preds = %34
%38 = load i8**, i8*** %9
%39 = load i8*, i8** %38
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %58
41: ; preds = %37
%42 = load i64*, i64** %10
%43 = load i64, i64* %42
%44 = add i64 %43, 1
%45 = call i8* @malloc(i64 %44)
store i8* %45, i8** %13
%46 = load i8*, i8** %13
%47 = load i8**, i8*** %9
%48 = load i8*, i8** %47
%49 = load i64*, i64** %10
%50 = load i64, i64* %49
%51 = call i32 @memcpy(i8* %46, i8* %48, i64 %50)
%52 = load i8*, i8** %13
%53 = load i64*, i64** %10
%54 = load i64, i64* %53
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
%56 = load i8*, i8** %13
%57 = load i8**, i8*** %9
store i8* %56, i8** %57
br label %58
58: ; preds = %41, %37, %34
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = shl i64 %60, 56
%62 = load i64*, i64** %10
%63 = load i64, i64* %62
%64 = or i64 %63, %61
store i64 %64, i64* %62
ret void
}

LLVM IR (-O2):
%struct.skynet_context = type { i32 }
define void @_filter_args(%struct.skynet_context* %0, i32 %1, i32* nocapture %2, i8** nocapture %3, i64* nocapture %4) {
%6 = and i32 %1, 65536
%7 = icmp eq i32 %6, 0
%8 = and i32 %1, 131072
%9 = icmp eq i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %5
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i32 @skynet_context_newsession(%struct.skynet_context* %0)
store i32 %15, i32* %2
br label %16
16: ; preds = %10, %5
br i1 %7, label %17, label %27
17: ; preds = %16
%18 = load i8*, i8** %3
%19 = icmp eq i8* %18, null
br i1 %19, label %27, label %20
20: ; preds = %17
%21 = load i64, i64* %4
%22 = add i64 %21, 1
%23 = tail call i8* @malloc(i64 %22)
%24 = tail call i32 @memcpy(i8* %23, i8* %18, i64 %21)
%25 = load i64, i64* %4
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
store i8* %23, i8** %3
br label %27
27: ; preds = %20, %17, %16
%28 = zext i32 %1 to i64
%29 = shl i64 %28, 56
%30 = load i64, i64* %4
%31 = or i64 %30, %29
store i64 %31, i64* %4
ret void
}