#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 timeval {
  unsigned int tv_usec;
  unsigned int tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
unsigned int play_pause(int watch_fd, int audio_fd, unsigned int ms) {
  struct timeval tv;
  fd_set fdset;
  tv.tv_usec = (ms % 1000) * 1000;
  tv.tv_sec = (ms / 1000);
  FD_ZERO(&fdset);
  FD_SET(watch_fd, &fdset);
  if (select(watch_fd + 1, &fdset, NULL, NULL, &tv) == 1)
    return ms - (tv.tv_sec * 1000 + tv.tv_usec / 1000);
  return ms;
}


x86 assembly (clang, -O0):
<play_pause>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
cmp    $0x1,%eax
jne    11c7 <play_pause+0x97>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
imul   $0x3e8,-0x14(%rbp),%ecx
mov    -0x18(%rbp),%eax
mov    $0x3e8,%esi
xor    %edx,%edx
div    %esi
mov    %eax,%edx
mov    -0x20(%rbp),%eax
add    %edx,%ecx
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11cd <play_pause+0x9d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<play_pause>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %edi,%ebp
mov    %edx,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
sub    %ecx,%edx
imul   $0x3e8,%edx,%ecx
mov    %ecx,(%rsp)
mov    %eax,0x4(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebp
mov    %rsp,%r8
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
cmp    $0x1,%eax
jne    11aa <play_pause+0x7a>
mov    (%rsp),%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0xfffffc18,0x4(%rsp),%ecx
ff
add    %ebx,%ecx
sub    %eax,%ecx
mov    %ecx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @play_pause(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = urem i32 %10, 1000
%12 = mul i32 %11, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %7
%15 = udiv i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @FD_ZERO(i32* %9)
%18 = load i32, i32* %5
%19 = call i32 @FD_SET(i32 %18, i32* %9)
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %9, i32* null, i32* null, %struct.timeval* %8)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %34
24: ; preds = %3
%25 = load i32, i32* %7
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%27 = load i32, i32* %26
%28 = mul i32 %27, 1000
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%30 = load i32, i32* %29
%31 = udiv i32 %30, 1000
%32 = add i32 %28, %31
%33 = sub i32 %25, %32
store i32 %33, i32* %4
br label %36
34: ; preds = %3
%35 = load i32, i32* %7
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @play_pause(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = urem i32 %2, 1000
%9 = mul nuw nsw i32 %8, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %9, i32* %10
%11 = udiv i32 %2, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_SET(i32 %0, i32* %5)
%15 = add nsw i32 %0, 1
%16 = call i32 @select(i32 %15, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %25
18: ; preds = %3
%19 = load i32, i32* %12
%20 = load i32, i32* %10
%21 = udiv i32 %20, 1000
%22 = mul i32 %19, -1000
%23 = add i32 %22, %2
%24 = sub i32 %23, %21
br label %25
25: ; preds = %3, %18
%26 = phi i32 [ %24, %18 ], [ %2, %3 ]
ret i32 %26
}