#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 feed_sample(int, int, char *, unsigned int);
unsigned int get_time_in_buffer(int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
unsigned int play_sample(int watch_fd, int audio_fd, char *buffer,
                         unsigned int size) {
  unsigned int done = 0;
  fd_set fdset;
  struct timeval tv;
  while (done != size) {
    unsigned int time_in_buffer;
    time_in_buffer = get_time_in_buffer(audio_fd);
    if (time_in_buffer < (50 + 50)) {
      int this_time;
      this_time = feed_sample(audio_fd, (50 + 50) - time_in_buffer,
                              buffer + done, size - done);
      done += this_time;
      time_in_buffer += this_time * 1000 / (2 * 11025);
    }
    if (time_in_buffer > 50) {
      time_in_buffer -= 50;
      FD_ZERO(&fdset);
      FD_SET(watch_fd, &fdset);
      tv.tv_usec = (time_in_buffer % 1000) * 1000;
      tv.tv_sec = (time_in_buffer / 1000);
      if (select(watch_fd + 1, &fdset, NULL, NULL, &tv))
        break;
    }
  }
  return done;
}


x86 assembly (clang, -O0):
<play_sample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     124d <play_sample+0xfd>
mov    -0x8(%rbp),%edi
call   1060 <get_time_in_buffer@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jae    11ce <play_sample+0x7e>
mov    -0x8(%rbp),%edi
mov    $0x64,%esi
sub    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%eax
add    %rax,%rdx
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
call   1030 <feed_sample@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
imul   $0x3e8,-0x30(%rbp),%eax
mov    $0x5622,%ecx
cltd
idiv   %ecx
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x32,-0x2c(%rbp)
jbe    1248 <play_sample+0xf8>
mov    -0x2c(%rbp),%eax
sub    $0x32,%eax
mov    %eax,-0x2c(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
je     1243 <play_sample+0xf3>
jmp    124d <play_sample+0xfd>
jmp    1248 <play_sample+0xf8>
jmp    116c <play_sample+0x1c>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<play_sample>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x18(%rsp)
test   %ecx,%ecx
je     1242 <play_sample+0xf2>
mov    %ecx,%ebx
mov    %esi,%r12d
mov    %edi,%r15d
lea    0x1(%r15),%eax
mov    %eax,0xc(%rsp)
xor    %ebp,%ebp
lea    0x24(%rsp),%r14
jmp    11e8 <play_sample+0x98>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xffffffce,%r13d
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
imul   $0x10624dd3,%r13,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
sub    %ecx,%r13d
imul   $0x3e8,%r13d,%ecx
mov    %ecx,0x10(%rsp)
mov    %eax,0x14(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1050 <select@plt>
test   %rax,%rax
jne    1248 <play_sample+0xf8>
cmp    %ebx,%ebp
je     1246 <play_sample+0xf6>
mov    %r12d,%edi
call   1060 <get_time_in_buffer@plt>
mov    %eax,%r13d
cmp    $0x63,%eax
ja     1190 <play_sample+0x40>
mov    $0x64,%esi
sub    %r13d,%esi
mov    %ebp,%edx
add    0x18(%rsp),%rdx
mov    %ebx,%ecx
sub    %ebp,%ecx
mov    %r12d,%edi
call   1030 <feed_sample@plt>
add    %eax,%ebp
imul   $0x3e8,%eax,%eax
cltq
imul   $0x2f8df18f,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2c,%rax
add    %ecx,%eax
add    %r13d,%eax
mov    %eax,%r13d
cmp    $0x33,%eax
jae    1190 <play_sample+0x40>
jmp    11e4 <play_sample+0x94>
xor    %ebp,%ebp
jmp    1248 <play_sample+0xf8>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @play_sample(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %14
14: ; preds = %65, %4
%15 = load i32, i32* %9
%16 = load i32, i32* %8
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %66
18: ; preds = %14
%19 = load i32, i32* %6
%20 = call i32 @get_time_in_buffer(i32 %19)
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = icmp ult i32 %21, 100
br i1 %22, label %23, label %43
23: ; preds = %18
%24 = load i32, i32* %6
%25 = load i32, i32* %12
%26 = sub i32 100, %25
%27 = load i8*, i8** %7
%28 = load i32, i32* %9
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %8
%32 = load i32, i32* %9
%33 = sub i32 %31, %32
%34 = call i32 @feed_sample(i32 %24, i32 %26, i8* %30, i32 %33)
store i32 %34, i32* %13
%35 = load i32, i32* %13
%36 = load i32, i32* %9
%37 = add i32 %36, %35
store i32 %37, i32* %9
%38 = load i32, i32* %13
%39 = mul nsw i32 %38, 1000
%40 = sdiv i32 %39, 22050
%41 = load i32, i32* %12
%42 = add i32 %41, %40
store i32 %42, i32* %12
br label %43
43: ; preds = %23, %18
%44 = load i32, i32* %12
%45 = icmp ugt i32 %44, 50
br i1 %45, label %46, label %65
46: ; preds = %43
%47 = load i32, i32* %12
%48 = sub i32 %47, 50
store i32 %48, i32* %12
%49 = call i32 @FD_ZERO(i32* %10)
%50 = load i32, i32* %5
%51 = call i32 @FD_SET(i32 %50, i32* %10)
%52 = load i32, i32* %12
%53 = urem i32 %52, 1000
%54 = mul i32 %53, 1000
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %54, i32* %55
%56 = load i32, i32* %12
%57 = udiv i32 %56, 1000
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i32 %57, i32* %58
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
%61 = call i64 @select(i32 %60, i32* %10, i32* null, i32* null, %struct.timeval* %11)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %46
br label %66
64: ; preds = %46
br label %65
65: ; preds = %64, %43
br label %14
66: ; preds = %63, %14
%67 = load i32, i32* %9
ret i32 %67
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @play_sample(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = icmp eq i32 %3, 0
br i1 %9, label %43, label %10
10: ; preds = %4
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%13 = add nsw i32 %0, 1
br label %14
14: ; preds = %10, %40
%15 = phi i32 [ 0, %10 ], [ %41, %40 ]
%16 = call i32 @get_time_in_buffer(i32 %1)
%17 = icmp ult i32 %16, 100
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = sub nuw nsw i32 100, %16
%20 = zext i32 %15 to i64
%21 = getelementptr inbounds i8, i8* %2, i64 %20
%22 = sub i32 %3, %15
%23 = call i32 @feed_sample(i32 %1, i32 %19, i8* %21, i32 %22)
%24 = add i32 %23, %15
%25 = mul nsw i32 %23, 1000
%26 = sdiv i32 %25, 22050
%27 = add nsw i32 %26, %16
%28 = icmp ugt i32 %27, 50
br i1 %28, label %29, label %40
29: ; preds = %14, %18
%30 = phi i32 [ %24, %18 ], [ %15, %14 ]
%31 = phi i32 [ %27, %18 ], [ %16, %14 ]
%32 = add i32 %31, -50
%33 = call i32 @FD_ZERO(i32* %5)
%34 = call i32 @FD_SET(i32 %0, i32* %5)
%35 = urem i32 %32, 1000
%36 = mul nuw nsw i32 %35, 1000
store i32 %36, i32* %11
%37 = udiv i32 %32, 1000
store i32 %37, i32* %12
%38 = call i64 @select(i32 %13, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %18, %29
%41 = phi i32 [ %30, %29 ], [ %24, %18 ]
%42 = icmp eq i32 %41, %3
br i1 %42, label %43, label %14
43: ; preds = %40, %29, %4
%44 = phi i32 [ 0, %4 ], [ %30, %29 ], [ %3, %40 ]
ret i32 %44
}