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

/* Variables and functions */
scalar_t__ skynet_context_total();
int /*<<< orphan*/ skynet_initthread(int);
int skynet_socket_poll();
int /*<<< orphan*/ wakeup(struct monitor *, int /*<<< orphan*/);
void *thread_socket(void *p) {
  struct monitor *m = p;
  skynet_initthread(2);
  for (;;) {
    int r = skynet_socket_poll();
    if (r == 0)
      break;
    if (r < 0) {
      if (skynet_context_total() == 0)
        break;
      continue;
    }
    wakeup(m, 0);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<thread_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x2,%edi
call   1040 <skynet_initthread@plt>
mov    $0x0,%al
call   1050 <skynet_socket_poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1177 <thread_socket+0x37>
jmp    11ac <thread_socket+0x6c>
cmpl   $0x0,-0x14(%rbp)
jge    119c <thread_socket+0x5c>
mov    $0x0,%al
call   1060 <skynet_context_total@plt>
cmp    $0x0,%rax
jne    1197 <thread_socket+0x57>
jmp    11ac <thread_socket+0x6c>
jmp    115e <thread_socket+0x1e>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <wakeup@plt>
jmp    115e <thread_socket+0x1e>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_socket>:
push   %rbx
mov    %rdi,%rbx
mov    $0x2,%edi
call   1040 <skynet_initthread@plt>
jmp    115a <thread_socket+0x1a>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <wakeup@plt>
xor    %eax,%eax
call   1050 <skynet_socket_poll@plt>
test   %eax,%eax
je     1175 <thread_socket+0x35>
test   %eax,%eax
jns    1150 <thread_socket+0x10>
xor    %eax,%eax
call   1060 <skynet_context_total@plt>
test   %rax,%rax
jne    115a <thread_socket+0x1a>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.monitor = type { i32 }
define i8* @thread_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.monitor*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = bitcast i8* %5 to %struct.monitor*
store %struct.monitor* %6, %struct.monitor** %3
%7 = call i32 @skynet_initthread(i32 2)
br label %8
8: ; preds = %21, %20, %1
%9 = call i32 (...) @skynet_socket_poll()
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
br label %24
13: ; preds = %8
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = call i64 (...) @skynet_context_total()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
br label %24
20: ; preds = %16
br label %8
21: ; preds = %13
%22 = load %struct.monitor*, %struct.monitor** %3
%23 = call i32 @wakeup(%struct.monitor* %22, i32 0)
br label %8
24: ; preds = %19, %12
ret i8* null
}

LLVM IR (-O2):
%struct.monitor = type { i32 }
define noalias i8* @thread_socket(i8* %0) {
%2 = bitcast i8* %0 to %struct.monitor*
%3 = tail call i32 @skynet_initthread(i32 2)
%4 = tail call i32 (...) @skynet_socket_poll()
%5 = icmp eq i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %1, %14
%7 = phi i32 [ %15, %14 ], [ %4, %1 ]
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i64 (...) @skynet_context_total()
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %14
12: ; preds = %6
%13 = tail call i32 @wakeup(%struct.monitor* %2, i32 0)
br label %14
14: ; preds = %9, %12
%15 = tail call i32 (...) @skynet_socket_poll()
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %6
17: ; preds = %14, %9, %1
ret i8* null
}