#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 aeEventLoop {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ free_conn(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, scalar_t__);
void read_sock(struct aeEventLoop *eventLoop, int fd, void *clientData,
               int mask) {
  (void)eventLoop;
  (void)clientData;
  char buf[(1024)] = {0};
  ssize_t n = read(fd, buf, (1024));
  if (n < 0) {
    if (errno == EAGAIN || errno == EINTR)
      return;
    perror("read server");
    free_conn(fd, mask);
    return;
  } else if (n == 0) {
    printf("WARNING: server has been closed. type \"rc\" to reconnect.\n");
    free_conn(fd, mask);
    return;
  }
  write(STDOUT_FILENO, buf, n);
}


x86 assembly (clang, -O0):
<read_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jge    120a <read_sock+0xaa>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%rax
je     11e9 <read_sock+0x89>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
jne    11ee <read_sock+0x8e>
jmp    1255 <read_sock+0xf5>
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1070 <free_conn@plt>
jmp    1255 <read_sock+0xf5>
cmpq   $0x0,-0x428(%rbp)
jne    1234 <read_sock+0xd4>
lea    0xded(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1070 <free_conn@plt>
jmp    1255 <read_sock+0xf5>
jmp    1239 <read_sock+0xd9>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1030 <write@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %ecx,%r14d
mov    %esi,%ebp
mov    %rsp,%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
js     11ae <read_sock+0x4e>
je     11dc <read_sock+0x7c>
mov    0x2e51(%rip),%rcx
mov    (%rcx),%edi
mov    %rsp,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
jmp    11d0 <read_sock+0x70>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <read_sock+0x70>
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jne    11fd <read_sock+0x9d>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r14d,%esi
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <free_conn@plt>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
jmp    11e8 <read_sock+0x88>

LLVM IR (-O0):
%struct.aeEventLoop = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [12 x i8] c"read server\00"
@.str.1 = [58 x i8] c"WARNING: server has been closed. type \22rc\22 to reconnect.\0A\00"
@STDOUT_FILENO = global i32 0
define void @read_sock(%struct.aeEventLoop* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.aeEventLoop*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i64
store %struct.aeEventLoop* %0, %struct.aeEventLoop** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load %struct.aeEventLoop*, %struct.aeEventLoop** %5
%12 = load i8*, i8** %7
%13 = bitcast [1024 x i8]* %9 to i8*
%14 = load i32, i32* %6
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%16 = call i64 @read(i32 %14, i8* %15, i32 1024)
store i64 %16, i64* %10
%17 = load i64, i64* %10
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %4
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %19
br label %47
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = load i32, i32* %8
%32 = call i32 @free_conn(i32 %30, i32 %31)
br label %47
33: ; preds = %4
%34 = load i64, i64* %10
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %6
%39 = load i32, i32* %8
%40 = call i32 @free_conn(i32 %38, i32 %39)
br label %47
41: ; preds = %33
br label %42
42: ; preds = %41
%43 = load i32, i32* @STDOUT_FILENO
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%45 = load i64, i64* %10
%46 = call i32 @write(i32 %43, i8* %44, i64 %45)
br label %47
47: ; preds = %42, %36, %28, %27
ret void
}

LLVM IR (-O2):
%struct.aeEventLoop = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [12 x i8] c"read server\00"
@STDOUT_FILENO = global i32 0
@str = [57 x i8] c"WARNING: server has been closed. type \22rc\22 to reconnect.\00"
define void @read_sock(%struct.aeEventLoop* nocapture readnone %0, i32 %1, i8* nocapture readnone %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i64 @read(i32 %1, i8* %6, i32 1024)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %4
%10 = load i64, i64* @errno
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %10, %11
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %10, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %27, label %16
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @free_conn(i32 %1, i32 %3)
br label %27
19: ; preds = %4
%20 = icmp eq i64 %7, 0
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @free_conn(i32 %1, i32 %3)
br label %27
24: ; preds = %19
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @write(i32 %25, i8* %6, i64 %7)
br label %27
27: ; preds = %9, %24, %21, %16
ret void
}