#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 /*<<< orphan*/ rd_buf;

/* Variables and functions */
int /*<<< orphan*/ ce_close(int);
int ce_cur_task();
int ce_read(int, char *, int);
int /*<<< orphan*/ ce_write(int, char *, scalar_t__);
int /*<<< orphan*/ ce_yield();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
scalar_t__ strlen(char *);
int take();
void process_io(void *arg) {
  while (1) {
    int cli_fd;
    char rd_buf[1024];
    char wt_buf[1024 + 128];
    int bytes;
    cli_fd = take();
    if (cli_fd == -1) {
      ce_yield();
      continue;
    }
    while (1) {
      bytes = ce_read(cli_fd, rd_buf, sizeof(rd_buf));
      if (bytes <= 0) {
        printf("ERROR: Failed to read data, will close socket %d\n", cli_fd);
        ce_close(cli_fd);
        break;
      }
      rd_buf[bytes] = '\0';
      sprintf(wt_buf, "echo from server(coroutine id: %d):\n%s\n",
              ce_cur_task(), rd_buf);
      ce_write(cli_fd, wt_buf, strlen(wt_buf) + 1);
    }
  }
}


x86 assembly (clang, -O0):
<process_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8b0,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <take@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11bf <process_io+0x2f>
mov    $0x0,%al
call   10b0 <ce_yield@plt>
jmp    119f <process_io+0xf>
jmp    11c4 <process_io+0x34>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <ce_read@plt>
mov    %eax,-0x894(%rbp)
cmpl   $0x0,-0x894(%rbp)
jg     1207 <process_io+0x77>
mov    -0xc(%rbp),%esi
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <ce_close@plt>
jmp    1288 <process_io+0xf8>
movslq -0x894(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x890(%rbp),%rax
mov    %rax,-0x8b0(%rbp)
mov    $0x0,%al
call   1060 <ce_cur_task@plt>
mov    -0x8b0(%rbp),%rdi
mov    %eax,%edx
lea    -0x410(%rbp),%rcx
lea    0xdf0(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x8a4(%rbp)
lea    -0x890(%rbp),%rax
mov    %rax,-0x8a0(%rbp)
lea    -0x890(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x8a4(%rbp),%edi
mov    -0x8a0(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1070 <ce_write@plt>
jmp    11c4 <process_io+0x34>
jmp    119f <process_io+0xf>

x86 assembly (clang, -O2):
<process_io>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x880,%rsp
mov    %rsp,%r12
lea    0xe57(%rip),%r14
lea    0xe82(%rip),%r15
lea    0x400(%rsp),%rbx
jmp    11d1 <process_io+0x41>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <ce_close@plt>
xor    %eax,%eax
call   10a0 <take@plt>
cmp    $0xffffffff,%eax
je     1230 <process_io+0xa0>
mov    %eax,%ebp
nop
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1080 <ce_read@plt>
test   %eax,%eax
jle    11c0 <process_io+0x30>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
xor    %eax,%eax
call   1060 <ce_cur_task@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
mov    %r12,%rcx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <ce_write@plt>
jmp    11e0 <process_io+0x50>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10b0 <ce_yield@plt>
jmp    11d1 <process_io+0x41>

LLVM IR (-O0):
@.str = [50 x i8] c"ERROR: Failed to read data, will close socket %d\0A\00"
@.str.1 = [40 x i8] c"echo from server(coroutine id: %d):\0A%s\0A\00"
define void @process_io(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1152 x i8]
%6 = alloca i32
store i8* %0, i8** %2
br label %7
7: ; preds = %1, %11, %39
%8 = call i32 (...) @take()
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 (...) @ce_yield()
br label %7
13: ; preds = %7
br label %14
14: ; preds = %13, %25
%15 = load i32, i32* %3
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = call i32 @ce_read(i32 %15, i8* %16, i32 1024)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i32, i32* %3
%22 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @ce_close(i32 %23)
br label %39
25: ; preds = %14
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [1152 x i8], [1152 x i8]* %5, i64 0, i64 0
%30 = call i32 (...) @ce_cur_task()
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* %31)
%33 = load i32, i32* %3
%34 = getelementptr inbounds [1152 x i8], [1152 x i8]* %5, i64 0, i64 0
%35 = getelementptr inbounds [1152 x i8], [1152 x i8]* %5, i64 0, i64 0
%36 = call i64 @strlen(i8* %35)
%37 = add nsw i64 %36, 1
%38 = call i32 @ce_write(i32 %33, i8* %34, i64 %37)
br label %14
39: ; preds = %20
br label %7
}

LLVM IR (-O2):
@.str = [50 x i8] c"ERROR: Failed to read data, will close socket %d\0A\00"
@.str.1 = [40 x i8] c"echo from server(coroutine id: %d):\0A%s\0A\00"
define void @process_io(i8* nocapture readnone %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1152 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1152 x i8], [1152 x i8]* %3, i64 0, i64 0
br label %6
6: ; preds = %28, %1
%7 = call i32 (...) @take()
%8 = icmp eq i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = call i32 @ce_read(i32 %7, i8* %4, i32 1024)
%11 = icmp slt i32 %10, 1
br i1 %11, label %14, label %17
12: ; preds = %6
%13 = call i32 (...) @ce_yield()
br label %28
14: ; preds = %17, %9
%15 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %7)
%16 = call i32 @ce_close(i32 %7)
br label %28
17: ; preds = %9, %17
%18 = phi i32 [ %26, %17 ], [ %10, %9 ]
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 (...) @ce_cur_task()
%22 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %4)
%23 = call i64 @strlen(i8* %5)
%24 = add nsw i64 %23, 1
%25 = call i32 @ce_write(i32 %7, i8* %5, i64 %24)
%26 = call i32 @ce_read(i32 %7, i8* %4, i32 1024)
%27 = icmp slt i32 %26, 1
br i1 %27, label %14, label %17
28: ; preds = %14, %12
br label %6
}