#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__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ close_conn(int *);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, scalar_t__);
int receive_data(int *fd, char *buf, ssize_t max_bytes, int *poll) {
  ssize_t bytes;
  if ((bytes = read(*fd, buf, max_bytes)) < 0) {
    perror("read");
    *poll = 1;
    return 0;
  } else if (bytes == 0) {
    close_conn(fd);
    *poll = 0;
    return 0;
  } else {
    if (bytes == max_bytes)
      *poll = 1;
    else
      *poll = 0;
    return bytes;
  }
}


x86 assembly (clang, -O0):
<receive_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    118b <receive_data+0x5b>
lea    0xe90(%rip),%rdi
call   1050 <perror@plt>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11e3 <receive_data+0xb3>
cmpq   $0x0,-0x30(%rbp)
jne    11b5 <receive_data+0x85>
mov    -0x10(%rbp),%rdi
call   1030 <close_conn@plt>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11e3 <receive_data+0xb3>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11d2 <receive_data+0xa2>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
jmp    11dc <receive_data+0xac>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_data>:
push   %r15
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rdi,%r15
mov    (%rdi),%edi
call   1040 <read@plt>
test   %rax,%rax
js     1170 <receive_data+0x40>
je     1159 <receive_data+0x29>
xor    %ecx,%ecx
cmp    %rbx,%rax
sete   %cl
mov    %ecx,(%r14)
jmp    116a <receive_data+0x3a>
mov    %r15,%rdi
call   1030 <close_conn@plt>
movl   $0x0,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe89(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,(%r14)
jmp    1168 <receive_data+0x38>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
define i32 @receive_data(i32* %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
store i32* %0, i32** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%11 = load i32*, i32** %6
%12 = load i32, i32* %11
%13 = load i8*, i8** %7
%14 = load i64, i64* %8
%15 = call i64 @read(i32 %12, i8* %13, i64 %14)
store i64 %15, i64* %10
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = load i32*, i32** %9
store i32 1, i32* %19
store i32 0, i32* %5
br label %38
20: ; preds = %4
%21 = load i64, i64* %10
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32*, i32** %6
%25 = call i32 @close_conn(i32* %24)
%26 = load i32*, i32** %9
store i32 0, i32* %26
store i32 0, i32* %5
br label %38
27: ; preds = %20
%28 = load i64, i64* %10
%29 = load i64, i64* %8
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32*, i32** %9
store i32 1, i32* %32
br label %35
33: ; preds = %27
%34 = load i32*, i32** %9
store i32 0, i32* %34
br label %35
35: ; preds = %33, %31
%36 = load i64, i64* %10
%37 = trunc i64 %36 to i32
store i32 %37, i32* %5
br label %38
38: ; preds = %35, %23, %17
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define i32 @receive_data(i32* %0, i8* nocapture %1, i64 %2, i32* nocapture writeonly %3) {
%5 = load i32, i32* %0
%6 = tail call i64 @read(i32 %5, i8* %1, i64 %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %18
10: ; preds = %4
%11 = icmp eq i64 %6, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @close_conn(i32* %0)
store i32 0, i32* %3
br label %18
14: ; preds = %10
%15 = icmp eq i64 %6, %2
%16 = zext i1 %15 to i32
store i32 %16, i32* %3
%17 = trunc i64 %6 to i32
br label %18
18: ; preds = %14, %12, %8
%19 = phi i32 [ 0, %8 ], [ 0, %12 ], [ %17, %14 ]
ret i32 %19
}