#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 int /*<<< orphan*/ uint8_t;
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int POLLHUP;
int POLLIN;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ perror_and_exit(char *);
int poll(struct pollfd *, int, int);
scalar_t__ read_wrapper(int, int /*<<< orphan*/ *, int);
scalar_t__ write_all(int, int /*<<< orphan*/ *, scalar_t__);
int transfer_data(int client_sockfd, int connect_sockfd) {
  static uint8_t data_buf[(1024 * 1024)];
  struct pollfd fds[2] = {
      {.fd = client_sockfd, .events = POLLIN},
      {.fd = connect_sockfd, .events = POLLIN},
  };
  int fd_to_write[2] = {connect_sockfd, client_sockfd};
  int poll_count;
  unsigned int i;
  ssize_t read_bytes;
  while (true) {
    poll_count = poll(fds, 2, -1);
    assert(poll_count != 0);
    if (poll_count < 0)
      perror_and_exit("poll");
    for (i = 0; i < 2 && poll_count > 0; ++i) {
      if (fds[i].revents == 0)
        continue;
      --poll_count;
      if (fds[i].revents & POLLIN) {
        read_bytes = read_wrapper(fds[i].fd, data_buf, (1024 * 1024));
        if (read_bytes <= 0)
          return 1;
        if (write_all(fd_to_write[i], data_buf, read_bytes))
          return 1;
      }
      if (fds[i].revents & POLLHUP)
        return 1;
      if ((fds[i].revents & ~(POLLIN | POLLHUP)) != 0)
        perror_and_exit("poll error event");
    }
  }
}


x86 assembly (clang, -O0):
<transfer_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x3c(%rbp)
jge    11d9 <transfer_data+0x89>
lea    0xe2c(%rip),%rdi
call   1070 <perror_and_exit@plt>
movl   $0x0,-0x40(%rbp)
xor    %eax,%eax
cmpl   $0x2,-0x40(%rbp)
mov    %al,-0x49(%rbp)
jae    11f9 <transfer_data+0xa9>
cmpl   $0x0,-0x3c(%rbp)
setg   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1209 <transfer_data+0xb9>
jmp    1349 <transfer_data+0x1f9>
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
cmpl   $0x0,0x8(%rax)
jne    1228 <transfer_data+0xd8>
jmp    133b <transfer_data+0x1eb>
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    0x2d95(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c6 <transfer_data+0x176>
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    (%rax),%edi
lea    0x2df1(%rip),%rsi
mov    $0x100000,%edx
call   1030 <read_wrapper@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jg     1294 <transfer_data+0x144>
movl   $0x1,-0x4(%rbp)
jmp    134e <transfer_data+0x1fe>
mov    -0x40(%rbp),%eax
mov    -0x38(%rbp,%rax,4),%edi
mov    -0x48(%rbp),%rdx
lea    0x2dba(%rip),%rsi
call   1050 <write_all@plt>
cmp    $0x0,%rax
je     12c1 <transfer_data+0x171>
movl   $0x1,-0x4(%rbp)
jmp    134e <transfer_data+0x1fe>
jmp    12c6 <transfer_data+0x176>
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    0x2d08(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12f7 <transfer_data+0x1a7>
movl   $0x1,-0x4(%rbp)
jmp    134e <transfer_data+0x1fe>
mov    -0x40(%rbp),%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
mov    0x8(%rax),%eax
mov    0x2ccf(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2cce(%rip),%rdx
or     (%rdx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1336 <transfer_data+0x1e6>
lea    0xcd4(%rip),%rdi
call   1070 <perror_and_exit@plt>
jmp    133b <transfer_data+0x1eb>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11e0 <transfer_data+0x90>
jmp    119c <transfer_data+0x4c>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,(%rsp)
mov    0x2e78(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %esi,0xc(%rsp)
mov    %eax,0x10(%rsp)
movl   $0x0,0x14(%rsp)
mov    %esi,0x20(%rsp)
mov    %edi,0x24(%rsp)
lea    0x2ecb(%rip),%r13
jmp    11ac <transfer_data+0x5c>
nopw   0x0(%rax,%rax,1)
lea    0xe59(%rip),%rdi
call   1070 <perror_and_exit@plt>
mov    %rsp,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setne  %dil
call   1040 <assert@plt>
test   %ebp,%ebp
js     11a0 <transfer_data+0x50>
je     11ac <transfer_data+0x5c>
mov    $0x1,%r15b
xor    %r12d,%r12d
jmp    11f6 <transfer_data+0xa6>
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
setg   %al
mov    $0x1,%r12d
test   %al,%r15b
mov    $0x0,%r15d
je     11ac <transfer_data+0x5c>
lea    (%r12,%r12,2),%r14
mov    0x8(%rsp,%r14,4),%eax
test   %eax,%eax
je     11e0 <transfer_data+0x90>
test   %eax,(%rbx)
je     123c <transfer_data+0xec>
mov    (%rsp,%r14,4),%edi
mov    %r13,%rsi
mov    $0x100000,%edx
call   1030 <read_wrapper@plt>
test   %rax,%rax
jle    1265 <transfer_data+0x115>
mov    0x20(%rsp,%r12,4),%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1050 <write_all@plt>
test   %rax,%rax
jne    1265 <transfer_data+0x115>
lea    (%rsp,%r14,4),%rax
add    $0x8,%rax
mov    (%rax),%eax
mov    0x2da5(%rip),%rcx
mov    (%rcx),%ecx
test   %eax,%ecx
jne    1265 <transfer_data+0x115>
add    $0xffffffff,%ebp
or     (%rbx),%ecx
not    %ecx
test   %ecx,%eax
je     11e0 <transfer_data+0x90>
lea    0xdaa(%rip),%rdi
call   1070 <perror_and_exit@plt>
jmp    11e0 <transfer_data+0x90>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@transfer_data.data_buf = internal global [1048576 x i32] zeroinitializer
@POLLIN = global i32 0
@.str = [5 x i8] c"poll\00"
@POLLHUP = global i32 0
@.str.1 = [17 x i8] c"poll error event\00"
define i32 @transfer_data(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x %struct.pollfd]
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
%13 = load i32, i32* %4
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 1
%15 = load i32, i32* @POLLIN
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 2
store i32 0, i32* %16
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i64 1
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 0
%19 = load i32, i32* %5
store i32 %19, i32* %18
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 1
%21 = load i32, i32* @POLLIN
store i32 %21, i32* %20
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 2
store i32 0, i32* %22
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%24 = load i32, i32* %5
store i32 %24, i32* %23
%25 = getelementptr inbounds i32, i32* %23, i64 1
%26 = load i32, i32* %4
store i32 %26, i32* %25
br label %27
27: ; preds = %2, %114
%28 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%29 = call i32 @poll(%struct.pollfd* %28, i32 2, i32 -1)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = call i32 @perror_and_exit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %36, %27
store i32 0, i32* %9
br label %39
39: ; preds = %111, %38
%40 = load i32, i32* %9
%41 = icmp ult i32 %40, 2
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %8
%44 = icmp sgt i32 %43, 0
br label %45
45: ; preds = %42, %39
%46 = phi i1 [ false, %39 ], [ %44, %42 ]
br i1 %46, label %47, label %114
47: ; preds = %45
%48 = load i32, i32* %9
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 %49
%51 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %50, i32 0, i32 2
%52 = load i32, i32* %51
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %47
br label %111
55: ; preds = %47
%56 = load i32, i32* %8
%57 = add nsw i32 %56, -1
store i32 %57, i32* %8
%58 = load i32, i32* %9
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 %59
%61 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %60, i32 0, i32 2
%62 = load i32, i32* %61
%63 = load i32, i32* @POLLIN
%64 = and i32 %62, %63
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %86
66: ; preds = %55
%67 = load i32, i32* %9
%68 = zext i32 %67 to i64
%69 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 %68
%70 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i64 @read_wrapper(i32 %71, i32* getelementptr inbounds ([1048576 x i32], [1048576 x i32]* @transfer_data.data_buf, i64 0, i64 0), i32 1048576)
store i64 %72, i64* %10
%73 = load i64, i64* %10
%74 = icmp sle i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %66
store i32 1, i32* %3
br label %115
76: ; preds = %66
%77 = load i32, i32* %9
%78 = zext i32 %77 to i64
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 %78
%80 = load i32, i32* %79
%81 = load i64, i64* %10
%82 = call i64 @write_all(i32 %80, i32* getelementptr inbounds ([1048576 x i32], [1048576 x i32]* @transfer_data.data_buf, i64 0, i64 0), i64 %81)
%83 = icmp ne i64 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %76
store i32 1, i32* %3
br label %115
85: ; preds = %76
br label %86
86: ; preds = %85, %55
%87 = load i32, i32* %9
%88 = zext i32 %87 to i64
%89 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 %88
%90 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %89, i32 0, i32 2
%91 = load i32, i32* %90
%92 = load i32, i32* @POLLHUP
%93 = and i32 %91, %92
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %96
95: ; preds = %86
store i32 1, i32* %3
br label %115
96: ; preds = %86
%97 = load i32, i32* %9
%98 = zext i32 %97 to i64
%99 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 %98
%100 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %99, i32 0, i32 2
%101 = load i32, i32* %100
%102 = load i32, i32* @POLLIN
%103 = load i32, i32* @POLLHUP
%104 = or i32 %102, %103
%105 = xor i32 %104, -1
%106 = and i32 %101, %105
%107 = icmp ne i32 %106, 0
br i1 %107, label %108, label %110
108: ; preds = %96
%109 = call i32 @perror_and_exit(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %110
110: ; preds = %108, %96
br label %111
111: ; preds = %110, %54
%112 = load i32, i32* %9
%113 = add i32 %112, 1
store i32 %113, i32* %9
br label %39
114: ; preds = %45
br label %27
115: ; preds = %95, %84, %75
%116 = load i32, i32* %3
ret i32 %116
}

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