#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 pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stdout;
int read_msg(unsigned char *buf, int len, int *readbytes, int fd) {
  struct pollfd pfd;
  int ret = 0;
  int err = 0;
  int left = len;
  int idx = 0;
  int sum = 0;
  pfd.fd = fd;
  pfd.events = POLLIN;
  while ((left > 0) && !err) {
    err = poll(&pfd, 1, -1);
    if (err == -1) {
      printf("Error poll() !\n");
      fflush(stdout);
    } else {
      ret = read(fd, &buf[idx], left);
      if (ret < 0) {
        printf("Error read() !\n");
        fflush(stdout);
      } else {
        left -= ret;
        idx += ret;
        sum += ret;
        ret = 0;
        err = 0;
      }
    }
  }
  if (readbytes)
    *readbytes = sum;
  return err;
}


x86 assembly (clang, -O0):
<read_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x3d(%rbp)
jle    11a5 <read_msg+0x65>
cmpl   $0x0,-0x30(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    11b5 <read_msg+0x75>
jmp    126b <read_msg+0x12b>
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11f4 <read_msg+0xb4>
lea    0xe24(%rip),%rdi
call   1030 <printf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    1266 <read_msg+0x126>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x38(%rbp),%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1236 <read_msg+0xf6>
lea    0xdf2(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    1261 <read_msg+0x121>
mov    -0x2c(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
jmp    1266 <read_msg+0x126>
jmp    118a <read_msg+0x4a>
cmpq   $0x0,-0x18(%rbp)
je     127f <read_msg+0x13f>
mov    -0x3c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x8(%rsp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
test   %esi,%esi
mov    %rdx,0x10(%rsp)
jle    11e5 <read_msg+0xa5>
mov    %ecx,%ebp
mov    %esi,%ebx
mov    %rdi,%r15
xor    %r13d,%r13d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
cmp    $0xffffffff,%eax
je     11ed <read_msg+0xad>
mov    %eax,%r14d
movslq %r12d,%rsi
add    %r15,%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11c0 <read_msg+0x80>
sub    %eax,%ebx
add    %eax,%r12d
add    %eax,%r13d
xor    %r14d,%r14d
test   %ebx,%ebx
jg     11de <read_msg+0x9e>
jmp    120d <read_msg+0xcd>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
test   %ebx,%ebx
jle    120d <read_msg+0xcd>
test   %r14d,%r14d
je     1180 <read_msg+0x40>
jmp    120d <read_msg+0xcd>
xor    %r14d,%r14d
xor    %r13d,%r13d
jmp    120d <read_msg+0xcd>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    $0xffffffff,%r14d
mov    0x10(%rsp),%rax
test   %rax,%rax
je     121a <read_msg+0xda>
mov    %r13d,(%rax)
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [16 x i8] c"Error poll() !\0A\00"
@stdout = global i32 0
@.str.1 = [16 x i8] c"Error read() !\0A\00"
define i32 @read_msg(i8* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.pollfd
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
store i32 0, i32* %10
store i32 0, i32* %11
%15 = load i32, i32* %6
store i32 %15, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%16 = load i32, i32* %8
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @POLLIN
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %18, i32* %19
br label %20
20: ; preds = %62, %4
%21 = load i32, i32* %12
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* %11
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br label %27
27: ; preds = %23, %20
%28 = phi i1 [ false, %20 ], [ %26, %23 ]
br i1 %28, label %29, label %63
29: ; preds = %27
%30 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 -1)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%35 = load i32, i32* @stdout
%36 = call i32 @fflush(i32 %35)
br label %62
37: ; preds = %29
%38 = load i32, i32* %8
%39 = load i8*, i8** %5
%40 = load i32, i32* %13
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i32, i32* %12
%44 = call i32 @read(i32 %38, i8* %42, i32 %43)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %37
%48 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* @stdout
%50 = call i32 @fflush(i32 %49)
br label %61
51: ; preds = %37
%52 = load i32, i32* %10
%53 = load i32, i32* %12
%54 = sub nsw i32 %53, %52
store i32 %54, i32* %12
%55 = load i32, i32* %10
%56 = load i32, i32* %13
%57 = add nsw i32 %56, %55
store i32 %57, i32* %13
%58 = load i32, i32* %10
%59 = load i32, i32* %14
%60 = add nsw i32 %59, %58
store i32 %60, i32* %14
store i32 0, i32* %10
store i32 0, i32* %11
br label %61
61: ; preds = %51, %47
br label %62
62: ; preds = %61, %33
br label %20
63: ; preds = %27
%64 = load i32*, i32** %7
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32, i32* %14
%68 = load i32*, i32** %7
store i32 %67, i32* %68
br label %69
69: ; preds = %66, %63
%70 = load i32, i32* %11
ret i32 %70
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@stdout = global i32 0
@str = [15 x i8] c"Error read() !\00"
@str.2 = [15 x i8] c"Error poll() !\00"
define i32 @read_msg(i8* nocapture %0, i32 %1, i32* writeonly %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %3, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %8, i32* %9
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %42
11: ; preds = %4, %34
%12 = phi i32 [ %38, %34 ], [ 0, %4 ]
%13 = phi i32 [ %37, %34 ], [ 0, %4 ]
%14 = phi i32 [ %36, %34 ], [ %1, %4 ]
%15 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 -1)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.2, i64 0, i64 0))
%19 = load i32, i32* @stdout
%20 = call i32 @fflush(i32 %19)
br label %42
21: ; preds = %11
%22 = sext i32 %13 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = call i32 @read(i32 %3, i8* %23, i32 %14)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%28 = load i32, i32* @stdout
%29 = call i32 @fflush(i32 %28)
br label %34
30: ; preds = %21
%31 = sub nsw i32 %14, %24
%32 = add nsw i32 %24, %13
%33 = add nsw i32 %24, %12
br label %34
34: ; preds = %26, %30
%35 = phi i32 [ %15, %26 ], [ 0, %30 ]
%36 = phi i32 [ %14, %26 ], [ %31, %30 ]
%37 = phi i32 [ %13, %26 ], [ %32, %30 ]
%38 = phi i32 [ %12, %26 ], [ %33, %30 ]
%39 = icmp sgt i32 %36, 0
%40 = icmp eq i32 %35, 0
%41 = select i1 %39, i1 %40, i1 false
br i1 %41, label %11, label %42
42: ; preds = %34, %17, %4
%43 = phi i32 [ 0, %4 ], [ -1, %17 ], [ %35, %34 ]
%44 = phi i32 [ 0, %4 ], [ %12, %17 ], [ %38, %34 ]
%45 = icmp eq i32* %2, null
br i1 %45, label %47, label %46
46: ; preds = %42
store i32 %44, i32* %2
br label %47
47: ; preds = %46, %42
ret i32 %43
}