#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 scalar_t__ int32_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ MSG_WAITALL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int ioctl(int, int /*<<< orphan*/, scalar_t__ *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ recv(int, char *, scalar_t__, int /*<<< orphan*/);
int suck_data(int fd) {
  int32_t bytes = -1;
  if (ioctl(fd, FIONREAD, &bytes) != -1 && bytes > 0) {
    if (bytes) {
      char *b = (char *)malloc(bytes);
      if (!b) {
        errno = ENOMEM;
        return -1;
      }
      (void)recv(fd, b, bytes, MSG_WAITALL);
      free(b);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<suck_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movq   $0xffffffffffffffff,-0x10(%rbp)
ff
mov    -0x8(%rbp),%edi
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11e4 <suck_data+0xa4>
cmpq   $0x0,-0x10(%rbp)
jle    11e4 <suck_data+0xa4>
cmpq   $0x0,-0x10(%rbp)
je     11df <suck_data+0x9f>
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bd <suck_data+0x7d>
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    0x2e21(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <suck_data+0xab>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2df1(%rip),%rax
mov    (%rax),%ecx
call   1040 <recv@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    11e4 <suck_data+0xa4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<suck_data>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
movq   $0xffffffffffffffff,0x8(%rsp)
ff ff
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1050 <ioctl@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
je     11c1 <suck_data+0x81>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
jle    11c1 <suck_data+0x81>
je     11c1 <suck_data+0x81>
call   1060 <malloc@plt>
test   %rax,%rax
je     11a9 <suck_data+0x69>
mov    %rax,%rbx
mov    0x8(%rsp),%rdx
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <recv@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11c1 <suck_data+0x81>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@ENOMEM = global i32 0
@errno = global i32 0
@MSG_WAITALL = global i32 0
define i32 @suck_data(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
store i32 %0, i32* %3
store i64 -1, i64* %4
%6 = load i32, i32* %3
%7 = load i32, i32* @FIONREAD
%8 = call i32 @ioctl(i32 %6, i32 %7, i64* %4)
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %33
10: ; preds = %1
%11 = load i64, i64* %4
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %10
%14 = load i64, i64* %4
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %13
%17 = load i64, i64* %4
%18 = call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = load i32, i32* @ENOMEM
store i32 %23, i32* @errno
store i32 -1, i32* %2
br label %34
24: ; preds = %16
%25 = load i32, i32* %3
%26 = load i8*, i8** %5
%27 = load i64, i64* %4
%28 = load i32, i32* @MSG_WAITALL
%29 = call i32 @recv(i32 %25, i8* %26, i64 %27, i32 %28)
%30 = load i8*, i8** %5
%31 = call i32 @free(i8* %30)
br label %32
32: ; preds = %24, %13
br label %33
33: ; preds = %32, %10, %1
store i32 0, i32* %2
br label %34
34: ; preds = %33, %22
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@ENOMEM = global i32 0
@errno = global i32 0
@MSG_WAITALL = global i32 0
define i32 @suck_data(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 -1, i64* %2
%4 = load i32, i32* @FIONREAD
%5 = call i32 @ioctl(i32 %0, i32 %4, i64* %2)
%6 = icmp ne i32 %5, -1
%7 = load i64, i64* %2
%8 = icmp sgt i64 %7, 0
%9 = select i1 %6, i1 %8, i1 false
%10 = icmp ne i64 %7, 0
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %12, label %23
12: ; preds = %1
%13 = call i64 @malloc(i64 %7)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* @ENOMEM
store i32 %17, i32* @errno
br label %23
18: ; preds = %12
%19 = load i64, i64* %2
%20 = load i32, i32* @MSG_WAITALL
%21 = call i32 @recv(i32 %0, i8* %14, i64 %19, i32 %20)
%22 = call i32 @free(i8* %14)
br label %23
23: ; preds = %1, %18, %16
%24 = phi i32 [ -1, %16 ], [ 0, %18 ], [ 0, %1 ]
ret i32 %24
}