#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 */
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int handle_readfd(char **message, int readfd) {
  *message = (char *)malloc(1024 * sizeof(char));
  memset(*message, 0, 1024);
  int bytes, available = 0;
  bytes = recv(readfd, *message, 1024, 0);
  if (bytes < 0) {
    if (errno == EWOULDBLOCK) {
      printf("(Socked %d): Got EWOULDBLOCK.\n", readfd);
    }
    perror("recv() failed");
  }
  available = 1024 - bytes;
  printf("(Socket %d): Read %d bytes, buffer available %d bytes.\n", readfd,
         bytes, available);
  return bytes;
}


x86 assembly (clang, -O0):
<handle_readfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e7 <handle_readfd+0x97>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
jne    11db <handle_readfd+0x8b>
mov    -0xc(%rbp),%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x400,%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_readfd>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    $0x400,%ebp
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    (%rbx),%rsi
mov    %r14d,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b1 <handle_readfd+0x61>
sub    %ebx,%ebp
lea    0xe91(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <handle_readfd+0x88>
lea    0xe32(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
jmp    1193 <handle_readfd+0x43>

LLVM IR (-O0):
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [31 x i8] c"(Socked %d): Got EWOULDBLOCK.\0A\00"
@.str.1 = [14 x i8] c"recv() failed\00"
@.str.2 = [56 x i8] c"(Socket %d): Read %d bytes, buffer available %d bytes.\0A\00"
define i32 @handle_readfd(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
%7 = call i64 @malloc(i32 1024)
%8 = inttoptr i64 %7 to i8*
%9 = load i8**, i8*** %3
store i8* %8, i8** %9
%10 = load i8**, i8*** %3
%11 = load i8*, i8** %10
%12 = call i32 @memset(i8* %11, i32 0, i32 1024)
store i32 0, i32* %6
%13 = load i32, i32* %4
%14 = load i8**, i8*** %3
%15 = load i8*, i8** %14
%16 = call i32 @recv(i32 %13, i8* %15, i32 1024, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %2
%20 = load i64, i64* @errno
%21 = load i64, i64* @EWOULDBLOCK
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %4
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %19
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %2
%29 = load i32, i32* %5
%30 = sub nsw i32 1024, %29
store i32 %30, i32* %6
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = load i32, i32* %6
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %32, i32 %33)
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [31 x i8] c"(Socked %d): Got EWOULDBLOCK.\0A\00"
@.str.1 = [14 x i8] c"recv() failed\00"
@.str.2 = [56 x i8] c"(Socket %d): Read %d bytes, buffer available %d bytes.\0A\00"
define i32 @handle_readfd(i8** nocapture %0, i32 %1) {
%3 = tail call i64 @malloc(i32 1024)
%4 = inttoptr i64 %3 to i8*
store i8* %4, i8** %0
%5 = tail call i32 @memset(i8* %4, i32 0, i32 1024)
%6 = load i8*, i8** %0
%7 = tail call i32 @recv(i32 %1, i8* %6, i32 1024, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i64, i64* @errno
%11 = load i64, i64* @EWOULDBLOCK
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %15
15: ; preds = %13, %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = sub nsw i32 1024, %7
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %7, i32 %18)
ret i32 %7
}