#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 uint8_t;
typedef int uint32_t;
struct skynet_context {
  int dummy;
};
struct master {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ _request_name(struct master *, char const *, size_t);
int /*<<< orphan*/ _update_address(struct master *, int, char const *, size_t);
int /*<<< orphan*/ _update_name(struct master *, int, char const *, size_t);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ dispatch_socket(void *, void const *, int);
int /*<<< orphan*/ skynet_error(struct skynet_context *, char *, int, int);
int _mainloop(struct skynet_context *context, void *ud, int type, int session,
              uint32_t source, const void *msg, size_t sz) {
  if (type == 6) {
    dispatch_socket(ud, msg, (int)sz);
    return 0;
  }
  if (type != 5) {
    skynet_error(context, "None harbor message recv from %x (type = %d)",
                 source, type);
    return 0;
  }
  assert(sz >= 4);
  struct master *m = ud;
  const uint8_t *handlen = msg;
  uint32_t handle =
      handlen[0] << 24 | handlen[1] << 16 | handlen[2] << 8 | handlen[3];
  sz -= 4;
  const char *name = msg;
  name += 4;
  if (handle == 0) {
    _request_name(m, name, sz);
  } else if (handle < 256) {
    _update_address(m, handle, name, sz);
  } else {
    _update_name(m, handle, name, sz);
  }
  return 0;
}


x86 assembly (clang, -O0):
<_mainloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
cmpl   $0x6,-0x1c(%rbp)
jne    11ab <_mainloop+0x4b>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x10(%rbp),%rax
mov    %eax,%edx
call   1080 <dispatch_socket@plt>
movl   $0x0,-0x4(%rbp)
jmp    12aa <_mainloop+0x14a>
cmpl   $0x5,-0x1c(%rbp)
je     11d7 <_mainloop+0x77>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe3a(%rip),%rsi
call   1060 <skynet_error@plt>
movl   $0x0,-0x4(%rbp)
jmp    12aa <_mainloop+0x14a>
cmpq   $0x4,0x10(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
shl    $0x18,%eax
mov    -0x40(%rbp),%rcx
mov    0x4(%rcx),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x40(%rbp),%rcx
mov    0x8(%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x40(%rbp),%rcx
or     0xc(%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    0x10(%rbp),%rax
sub    $0x4,%rax
mov    %rax,0x10(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x50(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1264 <_mainloop+0x104>
mov    -0x38(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    0x10(%rbp),%rdx
call   1050 <_request_name@plt>
jmp    12a3 <_mainloop+0x143>
cmpl   $0x100,-0x44(%rbp)
jge    128a <_mainloop+0x12a>
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x50(%rbp),%rdx
mov    0x10(%rbp),%rcx
call   1070 <_update_address@plt>
jmp    129e <_mainloop+0x13e>
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x50(%rbp),%rdx
mov    0x10(%rbp),%rcx
call   1030 <_update_name@plt>
jmp    12a3 <_mainloop+0x143>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mainloop>:
push   %r15
push   %r14
push   %rbx
mov    %r9,%rbx
mov    %rsi,%r14
mov    0x20(%rsp),%r15
cmp    $0x5,%edx
je     118c <_mainloop+0x2c>
mov    %edx,%ecx
cmp    $0x6,%edx
jne    11d2 <_mainloop+0x72>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1080 <dispatch_socket@plt>
jmp    11f8 <_mainloop+0x98>
xor    %edi,%edi
cmp    $0x4,%r15
setae  %dil
call   1040 <assert@plt>
mov    (%rbx),%eax
mov    0x4(%rbx),%ecx
shl    $0x18,%eax
lea    0x4(%rbx),%rdx
shl    $0x10,%ecx
or     %eax,%ecx
mov    0x8(%rbx),%esi
shl    $0x8,%esi
or     %ecx,%esi
add    $0xfffffffffffffffc,%r15
or     0xc(%rbx),%esi
je     11e3 <_mainloop+0x83>
mov    %r14,%rdi
mov    %r15,%rcx
cmp    $0xff,%esi
jg     11f3 <_mainloop+0x93>
call   1070 <_update_address@plt>
jmp    11f8 <_mainloop+0x98>
lea    0xe27(%rip),%rsi
mov    %r8d,%edx
call   1060 <skynet_error@plt>
jmp    11f8 <_mainloop+0x98>
mov    %r14,%rdi
mov    %rdx,%rsi
mov    %r15,%rdx
call   1050 <_request_name@plt>
jmp    11f8 <_mainloop+0x98>
call   1030 <_update_name@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.skynet_context = type { i32 }
%struct.master = type { i32 }
@.str = [45 x i8] c"None harbor message recv from %x (type = %d)\00"
define i32 @_mainloop(%struct.skynet_context* %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %5, i64 %6) {
%8 = alloca i32
%9 = alloca %struct.skynet_context*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
%16 = alloca %struct.master*
%17 = alloca i32*
%18 = alloca i32
%19 = alloca i8*
store %struct.skynet_context* %0, %struct.skynet_context** %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store i8* %5, i8** %14
store i64 %6, i64* %15
%20 = load i32, i32* %11
%21 = icmp eq i32 %20, 6
br i1 %21, label %22, label %28
22: ; preds = %7
%23 = load i8*, i8** %10
%24 = load i8*, i8** %14
%25 = load i64, i64* %15
%26 = trunc i64 %25 to i32
%27 = call i32 @dispatch_socket(i8* %23, i8* %24, i32 %26)
store i32 0, i32* %8
br label %92
28: ; preds = %7
%29 = load i32, i32* %11
%30 = icmp ne i32 %29, 5
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load %struct.skynet_context*, %struct.skynet_context** %9
%33 = load i32, i32* %13
%34 = load i32, i32* %11
%35 = call i32 @skynet_error(%struct.skynet_context* %32, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %34)
store i32 0, i32* %8
br label %92
36: ; preds = %28
%37 = load i64, i64* %15
%38 = icmp uge i64 %37, 4
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i8*, i8** %10
%42 = bitcast i8* %41 to %struct.master*
store %struct.master* %42, %struct.master** %16
%43 = load i8*, i8** %14
%44 = bitcast i8* %43 to i32*
store i32* %44, i32** %17
%45 = load i32*, i32** %17
%46 = getelementptr inbounds i32, i32* %45, i64 0
%47 = load i32, i32* %46
%48 = shl i32 %47, 24
%49 = load i32*, i32** %17
%50 = getelementptr inbounds i32, i32* %49, i64 1
%51 = load i32, i32* %50
%52 = shl i32 %51, 16
%53 = or i32 %48, %52
%54 = load i32*, i32** %17
%55 = getelementptr inbounds i32, i32* %54, i64 2
%56 = load i32, i32* %55
%57 = shl i32 %56, 8
%58 = or i32 %53, %57
%59 = load i32*, i32** %17
%60 = getelementptr inbounds i32, i32* %59, i64 3
%61 = load i32, i32* %60
%62 = or i32 %58, %61
store i32 %62, i32* %18
%63 = load i64, i64* %15
%64 = sub i64 %63, 4
store i64 %64, i64* %15
%65 = load i8*, i8** %14
store i8* %65, i8** %19
%66 = load i8*, i8** %19
%67 = getelementptr inbounds i8, i8* %66, i64 4
store i8* %67, i8** %19
%68 = load i32, i32* %18
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %36
%71 = load %struct.master*, %struct.master** %16
%72 = load i8*, i8** %19
%73 = load i64, i64* %15
%74 = call i32 @_request_name(%struct.master* %71, i8* %72, i64 %73)
br label %91
75: ; preds = %36
%76 = load i32, i32* %18
%77 = icmp slt i32 %76, 256
br i1 %77, label %78, label %84
78: ; preds = %75
%79 = load %struct.master*, %struct.master** %16
%80 = load i32, i32* %18
%81 = load i8*, i8** %19
%82 = load i64, i64* %15
%83 = call i32 @_update_address(%struct.master* %79, i32 %80, i8* %81, i64 %82)
br label %90
84: ; preds = %75
%85 = load %struct.master*, %struct.master** %16
%86 = load i32, i32* %18
%87 = load i8*, i8** %19
%88 = load i64, i64* %15
%89 = call i32 @_update_name(%struct.master* %85, i32 %86, i8* %87, i64 %88)
br label %90
90: ; preds = %84, %78
br label %91
91: ; preds = %90, %70
store i32 0, i32* %8
br label %92
92: ; preds = %91, %31, %22
%93 = load i32, i32* %8
ret i32 %93
}

LLVM IR (-O2):
%struct.skynet_context = type { i32 }
%struct.master = type { i32 }
@.str = [45 x i8] c"None harbor message recv from %x (type = %d)\00"
define i32 @_mainloop(%struct.skynet_context* %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %5, i64 %6) {
switch i32 %2, label %11 [
i32 6, label %8
i32 5, label %13
]
8: ; preds = %7
%9 = trunc i64 %6 to i32
%10 = tail call i32 @dispatch_socket(i8* %1, i8* %5, i32 %9)
br label %45
11: ; preds = %7
%12 = tail call i32 @skynet_error(%struct.skynet_context* %0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %2)
br label %45
13: ; preds = %7
%14 = icmp ugt i64 %6, 3
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = bitcast i8* %1 to %struct.master*
%18 = bitcast i8* %5 to i32*
%19 = load i32, i32* %18
%20 = shl i32 %19, 24
%21 = getelementptr inbounds i8, i8* %5, i64 4
%22 = bitcast i8* %21 to i32*
%23 = load i32, i32* %22
%24 = shl i32 %23, 16
%25 = or i32 %24, %20
%26 = getelementptr inbounds i8, i8* %5, i64 8
%27 = bitcast i8* %26 to i32*
%28 = load i32, i32* %27
%29 = shl i32 %28, 8
%30 = or i32 %25, %29
%31 = getelementptr inbounds i8, i8* %5, i64 12
%32 = bitcast i8* %31 to i32*
%33 = load i32, i32* %32
%34 = or i32 %30, %33
%35 = add i64 %6, -4
%36 = icmp eq i32 %34, 0
br i1 %36, label %37, label %39
37: ; preds = %13
%38 = tail call i32 @_request_name(%struct.master* %17, i8* %21, i64 %35)
br label %45
39: ; preds = %13
%40 = icmp slt i32 %34, 256
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = tail call i32 @_update_address(%struct.master* %17, i32 %34, i8* %21, i64 %35)
br label %45
43: ; preds = %39
%44 = tail call i32 @_update_name(%struct.master* %17, i32 %34, i8* %21, i64 %35)
br label %45
45: ; preds = %37, %43, %41, %11, %8
ret i32 0
}