#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 revents;
  int fd;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_WARNING;
int POLLHUP;
int POLLOUT;
int /*<<< orphan*/ check_poll_errors(struct pollfd *, int, int *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(struct pollfd *);
scalar_t__ malloc(int);
int poll(struct pollfd *, int, int);
int prepare_poll(int *, struct pollfd **, int, int);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ write(int, char *, int);
void recvbuff(int src, char *buf, int size, int *tty) {
  struct pollfd *wpfd;
  int n, i;
  int pollret;
  wpfd = (struct pollfd *)malloc(sizeof(struct pollfd) * 256);
  if (!wpfd) {
    fprintf(stderr, "alloc error: %s\n", strerror(errno));
    syslog(LOG_INFO, "alloc error: %s\n", strerror(errno));
  }
  n = prepare_poll(tty, (struct pollfd **)&wpfd, -1, POLLOUT | POLLHUP);
  printf("Writing to %d clients: %d bytes\n", n, size);
  if (n > 1) {
    pollret = poll(wpfd, n, 50);
    if (pollret < 0) {
      fprintf(stderr, "Poll error: %s\n", strerror(errno));
      syslog(LOG_WARNING, "Poll error: %s\n", strerror(errno));
      sleep(1);
      return;
    }
    (void)check_poll_errors(wpfd, n, tty);
    for (i = 0; i < n; i++) {
      if (wpfd[i].revents & POLLOUT && wpfd[i].fd != src)
        write(wpfd[i].fd, buf, size);
    }
  }
  free(wpfd);
}


x86 assembly (clang, -O0):
<recvbuff>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x800,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1237 <recvbuff+0x87>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xdfa(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xdce(%rip),%rsi
call   1090 <syslog@plt>
mov    -0x20(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%ecx
mov    0x2d95(%rip),%rax
or     (%rax),%ecx
lea    -0x28(%rbp),%rsi
mov    $0xffffffff,%edx
call   1060 <prepare_poll@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xda6(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x1,-0x2c(%rbp)
jle    1384 <recvbuff+0x1d4>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    $0x32,%edx
call   10b0 <poll@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12ff <recvbuff+0x14f>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd73(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd47(%rip),%rsi
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    138d <recvbuff+0x1dd>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   10a0 <check_poll_errors@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    137f <recvbuff+0x1cf>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2cc4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     136c <recvbuff+0x1bc>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%eax
cmp    -0x4(%rbp),%eax
je     136c <recvbuff+0x1bc>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
jmp    1371 <recvbuff+0x1c1>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1316 <recvbuff+0x166>
jmp    1384 <recvbuff+0x1d4>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvbuff>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0x800,%edi
call   1080 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
jne    1229 <recvbuff+0x79>
mov    0x2de7(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xe08(%rip),%r13
mov    %ebx,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebx
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1090 <syslog@plt>
mov    0x2dc8(%rip),%r13
mov    0x2da9(%rip),%rax
mov    (%rax),%ecx
or     0x0(%r13),%ecx
mov    %rsp,%rsi
mov    %rbp,%rdi
mov    $0xffffffff,%edx
call   1060 <prepare_poll@plt>
mov    %eax,%ebx
lea    0xdbb(%rip),%rdi
mov    %eax,%esi
mov    %r12d,%edx
call   1050 <printf@plt>
cmp    $0x2,%ebx
jl     12cc <recvbuff+0x11c>
mov    (%rsp),%rdi
mov    %ebx,%esi
mov    $0x32,%edx
call   10b0 <poll@plt>
test   %eax,%eax
js     12d7 <recvbuff+0x127>
mov    (%rsp),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
call   10a0 <check_poll_errors@plt>
mov    %ebx,%ebx
mov    0x0(%r13),%eax
xor    %ebp,%ebp
jmp    12a9 <recvbuff+0xf9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
je     12cc <recvbuff+0x11c>
mov    (%rsp),%rcx
test   %eax,(%rcx,%rbp,8)
je     12a0 <recvbuff+0xf0>
mov    0x4(%rcx,%rbp,8),%edi
cmp    %r15d,%edi
je     12a0 <recvbuff+0xf0>
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <write@plt>
mov    0x0(%r13),%eax
jmp    12a0 <recvbuff+0xf0>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    132b <recvbuff+0x17b>
mov    0x2cea(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd9(%rip),%r14
mov    (%r14),%edi
call   10c0 <strerror@plt>
lea    0xd3c(%rip),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%edi
call   10c0 <strerror@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@stderr = global i32 0
@.str = [17 x i8] c"alloc error: %s\0A\00"
@errno = global i32 0
@LOG_INFO = global i32 0
@POLLOUT = global i32 0
@POLLHUP = global i32 0
@.str.1 = [33 x i8] c"Writing to %d clients: %d bytes\0A\00"
@.str.2 = [16 x i8] c"Poll error: %s\0A\00"
@LOG_WARNING = global i32 0
define void @recvbuff(i32 %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.pollfd*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%13 = call i64 @malloc(i32 2048)
%14 = inttoptr i64 %13 to %struct.pollfd*
store %struct.pollfd* %14, %struct.pollfd** %9
%15 = load %struct.pollfd*, %struct.pollfd** %9
%16 = icmp ne %struct.pollfd* %15, null
br i1 %16, label %26, label %17
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @LOG_INFO
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @syslog(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %17, %4
%27 = load i32*, i32** %8
%28 = load i32, i32* @POLLOUT
%29 = load i32, i32* @POLLHUP
%30 = or i32 %28, %29
%31 = call i32 @prepare_poll(i32* %27, %struct.pollfd** %9, i32 -1, i32 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = load i32, i32* %7
%34 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i32, i32* %10
%36 = icmp sgt i32 %35, 1
br i1 %36, label %37, label %96
37: ; preds = %26
%38 = load %struct.pollfd*, %struct.pollfd** %9
%39 = load i32, i32* %10
%40 = call i32 @poll(%struct.pollfd* %38, i32 %39, i32 50)
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @LOG_WARNING
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @syslog(i32 %48, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = call i32 @sleep(i32 1)
br label %99
53: ; preds = %37
%54 = load %struct.pollfd*, %struct.pollfd** %9
%55 = load i32, i32* %10
%56 = load i32*, i32** %8
%57 = call i32 @check_poll_errors(%struct.pollfd* %54, i32 %55, i32* %56)
store i32 0, i32* %11
br label %58
58: ; preds = %92, %53
%59 = load i32, i32* %11
%60 = load i32, i32* %10
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %95
62: ; preds = %58
%63 = load %struct.pollfd*, %struct.pollfd** %9
%64 = load i32, i32* %11
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %63, i64 %65
%67 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %66, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32, i32* @POLLOUT
%70 = and i32 %68, %69
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %91
72: ; preds = %62
%73 = load %struct.pollfd*, %struct.pollfd** %9
%74 = load i32, i32* %11
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %73, i64 %75
%77 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %76, i32 0, i32 1
%78 = load i32, i32* %77
%79 = load i32, i32* %5
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %91
81: ; preds = %72
%82 = load %struct.pollfd*, %struct.pollfd** %9
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %82, i64 %84
%86 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %85, i32 0, i32 1
%87 = load i32, i32* %86
%88 = load i8*, i8** %6
%89 = load i32, i32* %7
%90 = call i32 @write(i32 %87, i8* %88, i32 %89)
br label %91
91: ; preds = %81, %72, %62
br label %92
92: ; preds = %91
%93 = load i32, i32* %11
%94 = add nsw i32 %93, 1
store i32 %94, i32* %11
br label %58
95: ; preds = %58
br label %96
96: ; preds = %95, %26
%97 = load %struct.pollfd*, %struct.pollfd** %9
%98 = call i32 @free(%struct.pollfd* %97)
br label %99
99: ; preds = %96, %43
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@stderr = global i32 0
@.str = [17 x i8] c"alloc error: %s\0A\00"
@errno = global i32 0
@LOG_INFO = global i32 0
@POLLOUT = global i32 0
@POLLHUP = global i32 0
@.str.1 = [33 x i8] c"Writing to %d clients: %d bytes\0A\00"
@.str.2 = [16 x i8] c"Poll error: %s\0A\00"
@LOG_WARNING = global i32 0
define void @recvbuff(i32 %0, i8* nocapture %1, i32 %2, i32* %3) {
%5 = alloca %struct.pollfd*
%6 = bitcast %struct.pollfd** %5 to i8*
%7 = tail call i64 @malloc(i32 2048)
%8 = inttoptr i64 %7 to %struct.pollfd*
store %struct.pollfd* %8, %struct.pollfd** %5
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %19
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @LOG_INFO
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @syslog(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %10, %4
%20 = load i32, i32* @POLLOUT
%21 = load i32, i32* @POLLHUP
%22 = or i32 %21, %20
%23 = call i32 @prepare_poll(i32* %3, %struct.pollfd** %5, i32 -1, i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %2)
%25 = icmp sgt i32 %23, 1
br i1 %25, label %26, label %64
26: ; preds = %19
%27 = load %struct.pollfd*, %struct.pollfd** %5
%28 = call i32 @poll(%struct.pollfd* %27, i32 %23, i32 50)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @LOG_WARNING
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @syslog(i32 %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = call i32 @sleep(i32 1)
br label %67
40: ; preds = %26
%41 = load %struct.pollfd*, %struct.pollfd** %5
%42 = call i32 @check_poll_errors(%struct.pollfd* %41, i32 %23, i32* %3)
%43 = zext i32 %23 to i64
%44 = load i32, i32* @POLLOUT
br label %45
45: ; preds = %40, %60
%46 = phi i32 [ %44, %40 ], [ %61, %60 ]
%47 = phi i64 [ 0, %40 ], [ %62, %60 ]
%48 = load %struct.pollfd*, %struct.pollfd** %5
%49 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %48, i64 %47, i32 0
%50 = load i32, i32* %49
%51 = and i32 %46, %50
%52 = icmp eq i32 %51, 0
br i1 %52, label %60, label %53
53: ; preds = %45
%54 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %48, i64 %47, i32 1
%55 = load i32, i32* %54
%56 = icmp eq i32 %55, %0
br i1 %56, label %60, label %57
57: ; preds = %53
%58 = call i32 @write(i32 %55, i8* %1, i32 %2)
%59 = load i32, i32* @POLLOUT
br label %60
60: ; preds = %45, %53, %57
%61 = phi i32 [ %46, %45 ], [ %46, %53 ], [ %59, %57 ]
%62 = add nuw nsw i64 %47, 1
%63 = icmp eq i64 %62, %43
br i1 %63, label %64, label %45
64: ; preds = %60, %19
%65 = load %struct.pollfd*, %struct.pollfd** %5
%66 = call i32 @free(%struct.pollfd* %65)
br label %67
67: ; preds = %64, %30
ret void
}