#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void onMessage(int fd, int *res) {
  int n;
  char buf[256];
  printf("(server) on message! (%d)\n", fd);
  n = read(fd, buf, 256);
  write(1, buf, n);
  if (n == 0) {
    printf("(server) on close! (%d)\n", fd);
    *res = -1;
  }
}


x86 assembly (clang, -O0):
<onMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%esi
lea    0xeb4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
lea    -0x120(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmpl   $0x0,-0x14(%rbp)
jne    119f <onMessage+0x6f>
mov    -0x4(%rbp),%esi
lea    0xe8b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<onMessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xeb7(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rsp,%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,%ebx
mov    $0x1,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %ebx,%ebx
jne    118c <onMessage+0x5c>
lea    0xe9d(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
movl   $0xffffffff,(%r14)
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"(server) on message! (%d)\0A\00"
@.str.1 = [25 x i8] c"(server) on close! (%d)\0A\00"
define void @onMessage(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [256 x i8]
store i32 %0, i32* %3
store i32* %1, i32** %4
%7 = load i32, i32* %3
%8 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %3
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 256)
store i32 %11, i32* %5
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* %5
%14 = call i32 @write(i32 1, i8* %12, i32 %13)
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* %3
%19 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32*, i32** %4
store i32 -1, i32* %20
br label %21
21: ; preds = %17, %2
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"(server) on message! (%d)\0A\00"
@.str.1 = [25 x i8] c"(server) on close! (%d)\0A\00"
define void @onMessage(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32 @read(i32 %0, i8* %4, i32 256)
%7 = call i32 @write(i32 1, i8* %4, i32 %6)
%8 = icmp eq i32 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %0)
store i32 -1, i32* %1
br label %11
11: ; preds = %9, %2
ret void
}