#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;
  int revents;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int POLLERR;
int POLLHUP;
int POLLIN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void *consumer_loop(void *arg) {
  printf("Entering consumer\n");
  int socket = *(int *)arg;
  int connected = 1;
  while (connected) {
    struct pollfd fds[1];
    fds[0].fd = socket;
    fds[0].events = POLLIN;
    fds[0].revents = 0;
    int rdy_fds = poll(fds, 1, 2000);
    if (rdy_fds == -1) {
      fprintf(stderr, "Error waiting for data: %s\n", strerror(errno));
      connected = 0;
    } else if (rdy_fds == 1) {
      if ((fds[0].revents & POLLERR) || (fds[0].revents & POLLHUP)) {
        fprintf(stderr, "Socket closed\n");
        connected = 0;
      } else if (fds[0].revents & POLLIN) {
        char buf[30];
        ssize_t recvd = recv(socket, buf, sizeof(buf), 0);
        if (recvd == -1) {
          fprintf(stderr, "Error receiving data: %s\n", strerror(errno));
          connected = 0;
        } else {
          printf("Received %ld bytes: %s\n", recvd, buf);
        }
      }
    } else {
      printf("Timeout waiting for messages, revents = %x\n", fds[0].revents);
      connected = 0;
    }
  }
  printf("Exiting consumer\n");
  return NULL;
}


x86 assembly (clang, -O0):
<consumer_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1303 <consumer_loop+0x1b3>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1060 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f7 <consumer_loop+0xa7>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x10(%rbp)
jmp    12fe <consumer_loop+0x1ae>
cmpl   $0x1,-0x24(%rbp)
jne    12e1 <consumer_loop+0x191>
mov    -0x18(%rbp),%eax
mov    0x2dd5(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    122b <consumer_loop+0xdb>
mov    -0x18(%rbp),%eax
mov    0x2dc8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     124e <consumer_loop+0xfe>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x10(%rbp)
jmp    12dc <consumer_loop+0x18c>
mov    -0x18(%rbp),%eax
mov    0x2d80(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12d7 <consumer_loop+0x187>
mov    -0xc(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1e,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    12bd <consumer_loop+0x16d>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    %rax,%rdx
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x10(%rbp)
jmp    12d2 <consumer_loop+0x182>
mov    -0x54(%rbp),%esi
lea    -0x50(%rbp),%rdx
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d7 <consumer_loop+0x187>
jmp    12dc <consumer_loop+0x18c>
jmp    12f9 <consumer_loop+0x1a9>
mov    -0x18(%rbp),%esi
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
jmp    12fe <consumer_loop+0x1ae>
jmp    117a <consumer_loop+0x2a>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consumer_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
lea    0xf11(%rip),%rdi
call   1040 <puts@plt>
mov    (%rbx),%ebx
mov    0x2e52(%rip),%r12
lea    0x20(%rsp),%r13
mov    0x2e36(%rip),%rbp
mov    %ebx,0xc(%rsp)
jmp    11c0 <consumer_loop+0x60>
nopl   0x0(%rax,%rax,1)
lea    0xe9e(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%ebx
test   %r15d,%r15d
je     12bb <consumer_loop+0x15b>
mov    %ebx,0x10(%rsp)
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
movl   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x7d0,%edx
call   1070 <poll@plt>
cmp    $0x1,%eax
jne    126b <consumer_loop+0x10b>
mov    0x18(%rsp),%eax
mov    0x2de8(%rip),%rcx
test   %eax,(%rcx)
jne    1296 <consumer_loop+0x136>
mov    0x2de1(%rip),%rcx
test   %eax,(%rcx)
jne    1296 <consumer_loop+0x136>
mov    $0x1,%r15d
test   %eax,(%r12)
je     11b7 <consumer_loop+0x57>
mov    %ebx,%edi
mov    %r13,%r14
mov    %r13,%rsi
mov    $0x1e,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    11a0 <consumer_loop+0x40>
mov    0x0(%rbp),%ebx
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
xor    %r15d,%r15d
mov    %ebx,%edi
mov    0x2d83(%rip),%r12
lea    0xdcf(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11b3 <consumer_loop+0x53>
cmp    $0xffffffff,%eax
jne    12a9 <consumer_loop+0x149>
mov    0x0(%rbp),%ebx
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd78(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12bb <consumer_loop+0x15b>
mov    0x0(%rbp),%edi
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12bb <consumer_loop+0x15b>
mov    0x18(%rsp),%esi
lea    0xda9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd9(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [19 x i8] c"Entering consumer\0A\00"
@POLLIN = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Error waiting for data: %s\0A\00"
@errno = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
@.str.2 = [15 x i8] c"Socket closed\0A\00"
@.str.3 = [26 x i8] c"Error receiving data: %s\0A\00"
@.str.4 = [24 x i8] c"Received %ld bytes: %s\0A\00"
@.str.5 = [44 x i8] c"Timeout waiting for messages, revents = %x\0A\00"
@.str.6 = [18 x i8] c"Exiting consumer\0A\00"
define i8* @consumer_loop(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x %struct.pollfd]
%6 = alloca i32
%7 = alloca [30 x i8]
%8 = alloca i32
store i8* %0, i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %2
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
store i32 %12, i32* %3
store i32 1, i32* %4
br label %13
13: ; preds = %85, %1
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %86
16: ; preds = %13
%17 = load i32, i32* %3
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* @POLLIN
%21 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %23, i32 0, i32 2
store i32 0, i32* %24
%25 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%26 = call i32 @poll(%struct.pollfd* %25, i32 1, i32 2000)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 0, i32* %4
br label %85
34: ; preds = %16
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %79
37: ; preds = %34
%38 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%39 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = load i32, i32* @POLLERR
%42 = and i32 %40, %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %37
%45 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%46 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %45, i32 0, i32 2
%47 = load i32, i32* %46
%48 = load i32, i32* @POLLHUP
%49 = and i32 %47, %48
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44, %37
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %78
54: ; preds = %44
%55 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%56 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %55, i32 0, i32 2
%57 = load i32, i32* %56
%58 = load i32, i32* @POLLIN
%59 = and i32 %57, %58
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %77
61: ; preds = %54
%62 = load i32, i32* %3
%63 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%64 = call i32 @recv(i32 %62, i8* %63, i32 30, i32 0)
store i32 %64, i32* %8
%65 = load i32, i32* %8
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %72
67: ; preds = %61
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %70)
store i32 0, i32* %4
br label %76
72: ; preds = %61
%73 = load i32, i32* %8
%74 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %73, i8* %74)
br label %76
76: ; preds = %72, %67
br label %77
77: ; preds = %76, %54
br label %78
78: ; preds = %77, %51
br label %84
79: ; preds = %34
%80 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%81 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %80, i32 0, i32 2
%82 = load i32, i32* %81
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %82)
store i32 0, i32* %4
br label %84
84: ; preds = %79, %78
br label %85
85: ; preds = %84, %29
br label %13
86: ; preds = %13
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
ret i8* null
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"Error waiting for data: %s\0A\00"
@errno = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
@.str.2 = [15 x i8] c"Socket closed\0A\00"
@.str.3 = [26 x i8] c"Error receiving data: %s\0A\00"
@.str.4 = [24 x i8] c"Received %ld bytes: %s\0A\00"
@.str.5 = [44 x i8] c"Timeout waiting for messages, revents = %x\0A\00"
@str = [18 x i8] c"Entering consumer\00"
@str.7 = [17 x i8] c"Exiting consumer\00"
define noalias i8* @consumer_loop(i8* nocapture %0) {
%2 = alloca [1 x %struct.pollfd]
%3 = alloca [30 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = bitcast [1 x %struct.pollfd]* %2 to i8*
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
%11 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0, i32 2
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
br label %13
13: ; preds = %1, %53
store i32 %6, i32* %9
%14 = load i32, i32* @POLLIN
store i32 %14, i32* %10
store i32 0, i32* %11
%15 = call i32 @poll(%struct.pollfd* %8, i32 1, i32 2000)
switch i32 %15, label %49 [
i32 -1, label %16
i32 1, label %21
]
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %52
21: ; preds = %13
%22 = load i32, i32* %11
%23 = load i32, i32* @POLLERR
%24 = and i32 %23, %22
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32, i32* @POLLHUP
%28 = and i32 %27, %22
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %26, %21
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %52
33: ; preds = %26
%34 = load i32, i32* @POLLIN
%35 = and i32 %34, %22
%36 = icmp eq i32 %35, 0
br i1 %36, label %53, label %37
37: ; preds = %33
%38 = call i32 @recv(i32 %6, i8* %12, i32 30, i32 0)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %47
45: ; preds = %37
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %38, i8* %12)
br label %47
47: ; preds = %45, %40
%48 = phi i32 [ 0, %40 ], [ 1, %45 ]
br label %53
49: ; preds = %13
%50 = load i32, i32* %11
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %16, %30, %49
br label %56
53: ; preds = %33, %47
%54 = phi i32 [ %48, %47 ], [ 1, %33 ]
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %13
56: ; preds = %53, %52
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
ret i8* null
}