#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;
  scalar_t__ revents;
};
typedef unsigned int nfds_t;

/* Variables and functions */
int errno;
int gettid();
int poll(struct pollfd *, unsigned int, int);
int /*<<< orphan*/ print_events(scalar_t__);
int /*<<< orphan*/ printf(char *, int, ...);
char *strerror(int);
int _poll(struct pollfd *ufds, nfds_t nfds, int timeout) {
  int ret;
  unsigned int i;
  printf("%d: poll()\n", gettid());
  ret = poll(ufds, nfds, timeout);
  printf("%d: poll() = %d\n", gettid(), ret);
  if (ret < 0)
    printf("\terr %d (%s)\n", errno, strerror(errno));
  if (ret > 0) {
    for (i = 0; i < nfds; i++) {
      if (ufds[i].revents) {
        printf("\tfd %d ", ufds[i].fd);
        print_events(ufds[i].revents);
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <poll@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jge    11dd <_poll+0x8d>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    %rax,%rdx
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jle    1260 <_poll+0x110>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    125b <_poll+0x10b>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x4,%rcx
add    %rcx,%rax
cmpq   $0x0,0x8(%rax)
je     1248 <_poll+0xf8>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rdi
call   1040 <print_events@plt>
jmp    124d <_poll+0xfd>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ee <_poll+0x9e>
jmp    1260 <_poll+0x110>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_poll>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <gettid@plt>
lea    0xe92(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1060 <poll@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1030 <gettid@plt>
lea    0xe77(%rip),%rdi
mov    %eax,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
test   %r15d,%r15d
js     11ea <_poll+0x9a>
je     120d <_poll+0xbd>
test   %r14d,%r14d
je     120d <_poll+0xbd>
mov    %r14d,%r12d
shl    $0x4,%r12
lea    0xe70(%rip),%r14
xor    %ebp,%ebp
jmp    11c9 <_poll+0x79>
nop
add    $0x10,%rbp
cmp    %rbp,%r12
je     120d <_poll+0xbd>
cmpq   $0x0,0x8(%rbx,%rbp,1)
je     11c0 <_poll+0x70>
mov    (%rbx,%rbp,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx,%rbp,1),%rdi
call   1040 <print_events@plt>
jmp    11c0 <_poll+0x70>
mov    0x2def(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe1c(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64 }
@.str = [12 x i8] c"%d: poll()\0A\00"
@.str.1 = [17 x i8] c"%d: poll() = %d\0A\00"
@.str.2 = [14 x i8] c"\09err %d (%s)\0A\00"
@errno = global i32 0
@.str.3 = [8 x i8] c"\09fd %d \00"
define i32 @_poll(%struct.pollfd* %0, i32 %1, i32 %2) {
%4 = alloca %struct.pollfd*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.pollfd* %0, %struct.pollfd** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = call i32 (...) @gettid()
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load %struct.pollfd*, %struct.pollfd** %4
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = call i32 @poll(%struct.pollfd* %11, i32 %12, i32 %13)
store i32 %14, i32* %7
%15 = call i32 (...) @gettid()
%16 = load i32, i32* %7
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i32, i32* @errno
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %21, i8* %23)
br label %25
25: ; preds = %20, %3
%26 = load i32, i32* %7
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %61
28: ; preds = %25
store i32 0, i32* %8
br label %29
29: ; preds = %57, %28
%30 = load i32, i32* %8
%31 = load i32, i32* %5
%32 = icmp ult i32 %30, %31
br i1 %32, label %33, label %60
33: ; preds = %29
%34 = load %struct.pollfd*, %struct.pollfd** %4
%35 = load i32, i32* %8
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %34, i64 %36
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %37, i32 0, i32 1
%39 = load i64, i64* %38
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %56
41: ; preds = %33
%42 = load %struct.pollfd*, %struct.pollfd** %4
%43 = load i32, i32* %8
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %42, i64 %44
%46 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = load %struct.pollfd*, %struct.pollfd** %4
%50 = load i32, i32* %8
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %49, i64 %51
%53 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %52, i32 0, i32 1
%54 = load i64, i64* %53
%55 = call i32 @print_events(i64 %54)
br label %56
56: ; preds = %41, %33
br label %57
57: ; preds = %56
%58 = load i32, i32* %8
%59 = add i32 %58, 1
store i32 %59, i32* %8
br label %29
60: ; preds = %29
br label %61
61: ; preds = %60, %25
%62 = load i32, i32* %7
ret i32 %62
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64 }
@.str = [12 x i8] c"%d: poll()\0A\00"
@.str.1 = [17 x i8] c"%d: poll() = %d\0A\00"
@.str.2 = [14 x i8] c"\09err %d (%s)\0A\00"
@errno = global i32 0
@.str.3 = [8 x i8] c"\09fd %d \00"
define i32 @_poll(%struct.pollfd* %0, i32 %1, i32 %2) {
%4 = tail call i32 (...) @gettid()
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @poll(%struct.pollfd* %0, i32 %1, i32 %2)
%7 = tail call i32 (...) @gettid()
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %6)
%9 = icmp slt i32 %6, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8* %12)
br label %34
14: ; preds = %3
%15 = icmp ne i32 %6, 0
%16 = icmp ne i32 %1, 0
%17 = and i1 %15, %16
br i1 %17, label %18, label %34
18: ; preds = %14
%19 = zext i32 %1 to i64
br label %20
20: ; preds = %18, %31
%21 = phi i64 [ 0, %18 ], [ %32, %31 ]
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 %21, i32 1
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %20
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %0, i64 %21, i32 0
%27 = load i32, i32* %26
%28 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i64, i64* %22
%30 = tail call i32 @print_events(i64 %29)
br label %31
31: ; preds = %20, %25
%32 = add nuw nsw i64 %21, 1
%33 = icmp eq i64 %32, %19
br i1 %33, label %34, label %20
34: ; preds = %31, %10, %14
ret i32 %6
}