#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 member_0;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int poll(struct pollfd *, int, int /*<<< orphan*/);
int read(int, char *, int);
int flush_socket(int fd) {
  int rc = 0;
  for (;;) {
    char buffer[4096];
    struct pollfd fds[1] = {{fd, POLLIN, 0}};
    rc = poll(fds, 1, 0);
    if (rc <= 0)
      break;
    rc = read(fd, buffer, sizeof(buffer));
    if (rc <= 0) {
      rc = -1;
      break;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<flush_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1020(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x101c(%rbp)
movl   $0x0,-0x1018(%rbp)
lea    -0x1020(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     117c <flush_socket+0x5c>
jmp    11ae <flush_socket+0x8e>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     11a9 <flush_socket+0x89>
movl   $0xffffffff,-0x8(%rbp)
jmp    11ae <flush_socket+0x8e>
jmp    1135 <flush_socket+0x15>
mov    -0x8(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flush_socket>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ebx
mov    %edi,(%rsp)
mov    0x2eae(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jle    11b0 <flush_socket+0x90>
lea    0x10(%rsp),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    11ab <flush_socket+0x8b>
mov    %ebx,(%rsp)
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jg     1170 <flush_socket+0x50>
jmp    11b0 <flush_socket+0x90>
mov    $0xffffffff,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
define i32 @flush_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca [1 x %struct.pollfd]
store i32 %0, i32* %2
store i32 0, i32* %3
br label %6
6: ; preds = %25, %1
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
%9 = load i32, i32* %2
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
%11 = load i32, i32* @POLLIN
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 2
store i32 0, i32* %12
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%14 = call i32 @poll(%struct.pollfd* %13, i32 1, i32 0)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %6
br label %26
18: ; preds = %6
%19 = load i32, i32* %2
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 4096)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -1, i32* %3
br label %26
25: ; preds = %18
br label %6
26: ; preds = %24, %17
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
define i32 @flush_socket(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca [1 x %struct.pollfd]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = bitcast [1 x %struct.pollfd]* %3 to i8*
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
store i32 %0, i32* %7
%10 = load i32, i32* @POLLIN
store i32 %10, i32* %8
store i32 0, i32* %9
%11 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 0)
%12 = icmp slt i32 %11, 1
br i1 %12, label %20, label %13
13: ; preds = %1, %16
%14 = call i32 @read(i32 %0, i8* %4, i32 4096)
%15 = icmp slt i32 %14, 1
br i1 %15, label %20, label %16
16: ; preds = %13
store i32 %0, i32* %7
%17 = load i32, i32* @POLLIN
store i32 %17, i32* %8
store i32 0, i32* %9
%18 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 0)
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %13
20: ; preds = %16, %13, %1
%21 = phi i32 [ %11, %1 ], [ -1, %13 ], [ %18, %16 ]
ret i32 %21
}