#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 events;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int POLLIN;
int POLLPRI;
scalar_t__ poll(struct pollfd *, int, int);
scalar_t__ read(int, char *, size_t);
bool socket_data_retreiver(int fd, char *buffer, size_t amount) {
  struct pollfd pollfds[1] = {{.fd = fd, .events = POLLIN | POLLPRI}};
  ssize_t got = -1;
  amount--;
  while (true) {
    if (poll(pollfds, 1, 10000) < 0)
      return false;
    got = read(fd, buffer, amount);
    if (got < 0)
      return false;
    if (got == 0)
      break;
    buffer += got;
    amount -= got;
  }
  *buffer = '\0';
  return true;
}


x86 assembly (clang, -O0):
<socket_data_retreiver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    0x2ea7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1040 <poll@plt>
cmp    $0x0,%rax
jge    118b <socket_data_retreiver+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <socket_data_retreiver+0xd4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11b6 <socket_data_retreiver+0x96>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <socket_data_retreiver+0xd4>
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <socket_data_retreiver+0xa6>
jmp    11e6 <socket_data_retreiver+0xc6>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1162 <socket_data_retreiver+0x42>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_data_retreiver>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edi,0x8(%rsp)
mov    0x2ea1(%rip),%rax
mov    0x2eaa(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1040 <poll@plt>
xor    %r15d,%r15d
test   %rax,%rax
js     11bd <socket_data_retreiver+0x9d>
add    $0xffffffffffffffff,%rbx
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
js     11bd <socket_data_retreiver+0x9d>
je     11b3 <socket_data_retreiver+0x93>
add    %rax,%r14
sub    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1040 <poll@plt>
test   %rax,%rax
jns    1180 <socket_data_retreiver+0x60>
jmp    11bd <socket_data_retreiver+0x9d>
movb   $0x0,(%r14)
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@POLLPRI = global i32 0
define i32 @socket_data_retreiver(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [1 x %struct.pollfd]
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 0
%12 = load i32, i32* %5
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
%14 = load i32, i32* @POLLIN
%15 = load i32, i32* @POLLPRI
%16 = or i32 %14, %15
store i32 %16, i32* %13
store i64 -1, i64* %9
%17 = load i64, i64* %7
%18 = add i64 %17, -1
store i64 %18, i64* %7
br label %19
19: ; preds = %3, %36
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%21 = call i64 @poll(%struct.pollfd* %20, i32 1, i32 10000)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 0, i32* %4
br label %45
24: ; preds = %19
%25 = load i32, i32* %5
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = call i64 @read(i32 %25, i8* %26, i64 %27)
store i64 %28, i64* %9
%29 = load i64, i64* %9
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 0, i32* %4
br label %45
32: ; preds = %24
%33 = load i64, i64* %9
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32
br label %43
36: ; preds = %32
%37 = load i64, i64* %9
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 %37
store i8* %39, i8** %6
%40 = load i64, i64* %9
%41 = load i64, i64* %7
%42 = sub i64 %41, %40
store i64 %42, i64* %7
br label %19
43: ; preds = %35
%44 = load i8*, i8** %6
store i8 0, i8* %44
store i32 1, i32* %4
br label %45
45: ; preds = %43, %31, %23
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@POLLPRI = global i32 0
define i32 @socket_data_retreiver(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca [1 x %struct.pollfd]
%5 = bitcast [1 x %struct.pollfd]* %4 to i8*
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
store i32 %0, i32* %7
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
%9 = load i32, i32* @POLLIN
%10 = load i32, i32* @POLLPRI
%11 = or i32 %10, %9
store i32 %11, i32* %8
%12 = call i64 @poll(%struct.pollfd* %6, i32 1, i32 10000)
%13 = icmp slt i64 %12, 0
br i1 %13, label %29, label %14
14: ; preds = %3
%15 = add i64 %2, -1
br label %16
16: ; preds = %14, %23
%17 = phi i64 [ %25, %23 ], [ %15, %14 ]
%18 = phi i8* [ %24, %23 ], [ %1, %14 ]
%19 = call i64 @read(i32 %0, i8* %18, i64 %17)
%20 = icmp slt i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %16
%22 = icmp eq i64 %19, 0
br i1 %22, label %28, label %23
23: ; preds = %21
%24 = getelementptr inbounds i8, i8* %18, i64 %19
%25 = sub i64 %17, %19
%26 = call i64 @poll(%struct.pollfd* %6, i32 1, i32 10000)
%27 = icmp slt i64 %26, 0
br i1 %27, label %29, label %16
28: ; preds = %21
store i8 0, i8* %18
br label %29
29: ; preds = %23, %16, %3, %28
%30 = phi i32 [ 1, %28 ], [ 0, %3 ], [ 0, %16 ], [ 0, %23 ]
ret i32 %30
}