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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
void socket_read_cb(int fd, short events, void *arg) {
  char msg[1024];
  int len = read(fd, msg, sizeof(msg) - 1);
  if (len <= 0) {
    perror("read fail ");
    exit(1);
  }
  msg[len] = '\0';
  printf("recv %s from server\n", msg);
}


x86 assembly (clang, -O0):
<socket_read_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     1196 <socket_read_cb+0x56>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_read_cb>:
sub    $0x408,%rsp
mov    %rsp,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1175 <socket_read_cb+0x35>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xea6(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
add    $0x408,%rsp
ret
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"read fail \00"
@.str.1 = [21 x i8] c"recv %s from server\0A\00"
define void @socket_read_cb(i32 %0, i16 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i16
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i16 %1, i16* %5
store i8* %2, i8** %6
%9 = load i32, i32* %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 1023)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %19
store i8 0, i8* %20
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %21)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"read fail \00"
@.str.1 = [21 x i8] c"recv %s from server\0A\00"
define void @socket_read_cb(i32 %0, i16 signext %1, i8* nocapture readnone %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @read(i32 %0, i8* %5, i32 1023)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = zext i32 %6 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %5)
ret void
}