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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *skynet_command(struct skynet_context *, char *, char *);
int /*<<< orphan*/ skynet_send(struct skynet_context *, int /*<<< orphan*/,
                               scalar_t__, int /*<<< orphan*/,
                               int /*<<< orphan*/, char *, int);
int sprintf(char *, char *, char const *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strtoul(char const *, int /*<<< orphan*/ *, int);
void _launch_gate(struct skynet_context *ctx, const char *local_addr) {
  char tmp[128];
  sprintf(tmp, "gate L ! %s %d %d 0", local_addr, 5, 256);
  const char *gate_addr = skynet_command(ctx, "LAUNCH", tmp);
  if (gate_addr == NULL) {
    fprintf(stderr, "Harbor : launch gate failed\n");
    exit(1);
  }
  uint32_t gate = strtoul(gate_addr + 1, NULL, 16);
  if (gate == 0) {
    fprintf(stderr, "Harbor : launch gate invalid %s", gate_addr);
    exit(1);
  }
  const char *self_addr = skynet_command(ctx, "REG", NULL);
  int n = sprintf(tmp, "broker %s", self_addr);
  skynet_send(ctx, 0, gate, 0, 0, tmp, n);
  skynet_send(ctx, 0, gate, 0, 0, "start", 5);
}


x86 assembly (clang, -O0):
<_launch_gate>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe7b(%rip),%rsi
mov    $0x5,%ecx
mov    $0x100,%r8d
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    0xe6b(%rip),%rsi
call   1040 <skynet_command@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11e4 <_launch_gate+0x84>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x98(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jne    123a <_launch_gate+0xda>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x98(%rbp),%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe13(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <skynet_command@plt>
mov    %rax,-0xa8(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    %eax,-0xac(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xa0(%rbp),%rdx
lea    -0x90(%rbp),%r9
mov    -0xac(%rbp),%eax
xor    %r8d,%r8d
mov    %r8d,%esi
mov    %r8d,%ecx
mov    %eax,(%rsp)
call   1080 <skynet_send@plt>
mov    -0x8(%rbp),%rdi
mov    -0xa0(%rbp),%rdx
xor    %r8d,%r8d
lea    0xdb1(%rip),%r9
mov    %r8d,%esi
mov    %r8d,%ecx
movl   $0x5,(%rsp)
call   1080 <skynet_send@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_launch_gate>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%rdx
mov    %rdi,%r14
lea    0xe87(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x5,%ecx
mov    $0x100,%r8d
xor    %eax,%eax
call   1060 <sprintf@plt>
lea    0xe7a(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <skynet_command@plt>
test   %rax,%rax
je     123c <_launch_gate+0xdc>
mov    %rax,%r15
lea    0x1(%rax),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
test   %rax,%rax
je     125d <_launch_gate+0xfd>
mov    %rax,%rbx
lea    0xe84(%rip),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1040 <skynet_command@plt>
lea    0xe77(%rip),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %eax,(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
mov    %r15,%r9
call   1080 <skynet_send@plt>
movl   $0x5,(%rsp)
lea    0xe49(%rip),%r9
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <skynet_send@plt>
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.skynet_context = type { i32 }
@.str = [20 x i8] c"gate L ! %s %d %d 0\00"
@.str.1 = [7 x i8] c"LAUNCH\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Harbor : launch gate failed\0A\00"
@.str.3 = [32 x i8] c"Harbor : launch gate invalid %s\00"
@.str.4 = [4 x i8] c"REG\00"
@.str.5 = [10 x i8] c"broker %s\00"
@.str.6 = [6 x i8] c"start\00"
define void @_launch_gate(%struct.skynet_context* %0, i8* %1) {
%3 = alloca %struct.skynet_context*
%4 = alloca i8*
%5 = alloca [128 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store %struct.skynet_context* %0, %struct.skynet_context** %3
store i8* %1, i8** %4
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %11, i32 5, i32 256)
%13 = load %struct.skynet_context*, %struct.skynet_context** %3
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%15 = call i8* @skynet_command(%struct.skynet_context* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = call i64 @strtoul(i8* %24, i32* null, i32 16)
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %6
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %22
%34 = load %struct.skynet_context*, %struct.skynet_context** %3
%35 = call i8* @skynet_command(%struct.skynet_context* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* null)
store i8* %35, i8** %8
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%37 = load i8*, i8** %8
%38 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %37)
store i32 %38, i32* %9
%39 = load %struct.skynet_context*, %struct.skynet_context** %3
%40 = load i64, i64* %7
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%42 = load i32, i32* %9
%43 = call i32 @skynet_send(%struct.skynet_context* %39, i32 0, i64 %40, i32 0, i32 0, i8* %41, i32 %42)
%44 = load %struct.skynet_context*, %struct.skynet_context** %3
%45 = load i64, i64* %7
%46 = call i32 @skynet_send(%struct.skynet_context* %44, i32 0, i64 %45, i32 0, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
ret void
}

LLVM IR (-O2):
%struct.skynet_context = type { i32 }
@.str = [20 x i8] c"gate L ! %s %d %d 0\00"
@.str.1 = [7 x i8] c"LAUNCH\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Harbor : launch gate failed\0A\00"
@.str.3 = [32 x i8] c"Harbor : launch gate invalid %s\00"
@.str.4 = [4 x i8] c"REG\00"
@.str.5 = [10 x i8] c"broker %s\00"
@.str.6 = [6 x i8] c"start\00"
define void @_launch_gate(%struct.skynet_context* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1, i32 5, i32 256)
%6 = call i8* @skynet_command(%struct.skynet_context* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8, i8* %6, i64 1
%14 = call i64 @strtoul(i8* nocapture %13, i32* null, i32 16)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = call i8* @skynet_command(%struct.skynet_context* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* null)
%22 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %21)
%23 = call i32 @skynet_send(%struct.skynet_context* %0, i32 0, i64 %14, i32 0, i32 0, i8* %4, i32 %22)
%24 = call i32 @skynet_send(%struct.skynet_context* %0, i32 0, i64 %14, i32 0, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 5)
ret void
}