#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 /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ _poll(struct pollfd *, int, int);
int gettid();
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ random();
int /*<<< orphan*/ usleep(int);
void thread_pollin_rand_delay(void *args) {
  int fd = (int)args;
  struct pollfd pfd;
  int delay = (int)((double)random() * (10000000.0 / 2147483647.0));
  printf("%d: START (delay = %d)\n", gettid(), delay);
  pfd.fd = fd;
  pfd.events = POLLIN;
  usleep(delay);
  _poll(&pfd, 1, -1);
  printf("%d: END\n", gettid());
}


x86 assembly (clang, -O0):
<thread_pollin_rand_delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
cvtsi2sd %rax,%xmm0
movsd  0xe89(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
mov    -0x1c(%rbp),%edx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <usleep@plt>
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1060 <_poll@plt>
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %eax,%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_pollin_rand_delay>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <random@plt>
cvtsi2sd %rax,%xmm0
mulsd  0xe96(%rip),%xmm0
cvttsd2si %xmm0,%ebp
xor    %eax,%eax
call   1030 <gettid@plt>
lea    0xe8c(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,(%rsp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1070 <usleep@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1060 <_poll@plt>
xor    %eax,%eax
call   1030 <gettid@plt>
lea    0xe62(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@.str = [24 x i8] c"%d: START (delay = %d)\0A\00"
@POLLIN = global i32 0
@.str.1 = [9 x i8] c"%d: END\0A\00"
define void @thread_pollin_rand_delay(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.pollfd
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = ptrtoint i8* %6 to i32
store i32 %7, i32* %3
%8 = call i64 (...) @random()
%9 = sitofp i64 %8 to double
%10 = fmul double %9, 0x3F7312D0002625A0
%11 = fptosi double %10 to i32
store i32 %11, i32* %5
%12 = call i32 (...) @gettid()
%13 = load i32, i32* %5
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = load i32, i32* %3
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @POLLIN
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* %5
%20 = call i32 @usleep(i32 %19)
%21 = call i32 @_poll(%struct.pollfd* %4, i32 1, i32 -1)
%22 = call i32 (...) @gettid()
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %22)
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@.str = [24 x i8] c"%d: START (delay = %d)\0A\00"
@POLLIN = global i32 0
@.str.1 = [9 x i8] c"%d: END\0A\00"
define void @thread_pollin_rand_delay(i8* %0) {
%2 = alloca %struct.pollfd
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = bitcast %struct.pollfd* %2 to i8*
%6 = tail call i64 (...) @random()
%7 = sitofp i64 %6 to double
%8 = fmul double %7, 0x3F7312D0002625A0
%9 = fptosi double %8 to i32
%10 = tail call i32 (...) @gettid()
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %9)
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %4, i32* %12
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call i32 @usleep(i32 %9)
%16 = call i32 @_poll(%struct.pollfd* %2, i32 1, i32 -1)
%17 = call i32 (...) @gettid()
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %17)
ret void
}