#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);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int, ...);
unsigned int get_time_in_buffer(int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
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 = get_time_in_buffer(audio_fd);
      fprintf(stderr, "done %u, %u ms in buffer\n", done, time_in_buffer);
    }
    if (time_in_buffer > 50) {
      fprintf(stderr, "Time is %u\n", time_in_buffer);
      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     128a <play_sample+0x12a>
mov    -0x8(%rbp),%edi
call   1070 <get_time_in_buffer@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jae    11f1 <play_sample+0x91>
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)
mov    -0x8(%rbp),%edi
call   1070 <get_time_in_buffer@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpl   $0x32,-0x2c(%rbp)
jbe    1285 <play_sample+0x125>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
sub    $0x32,%eax
mov    %eax,-0x2c(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1080 <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   1060 <select@plt>
cmp    $0x0,%rax
je     1280 <play_sample+0x120>
jmp    128a <play_sample+0x12a>
jmp    1285 <play_sample+0x125>
jmp    117c <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,0x10(%rsp)
test   %ecx,%ecx
je     127c <play_sample+0x11c>
mov    %ecx,%r13d
mov    %esi,%ebp
mov    %rdi,0x18(%rsp)
lea    0x1(%rdi),%eax
mov    %eax,0x4(%rsp)
lea    0x24(%rsp),%r14
xor    %ebx,%ebx
jmp    121a <play_sample+0xba>
nopl   0x0(%rax,%rax,1)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    -0x32(%r12),%r15d
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    0x18(%rsp),%rdi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
imul   $0x10624dd3,%r15,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
neg    %ecx
add    %r12d,%ecx
add    $0xffffffce,%ecx
imul   $0x3e8,%ecx,%ecx
mov    %ecx,0x8(%rsp)
mov    %eax,0xc(%rsp)
mov    0x4(%rsp),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1060 <select@plt>
test   %rax,%rax
jne    1283 <play_sample+0x123>
cmp    %r13d,%ebx
je     1280 <play_sample+0x120>
mov    %ebp,%edi
call   1070 <get_time_in_buffer@plt>
mov    %eax,%r12d
cmp    $0x63,%eax
ja     11a0 <play_sample+0x40>
mov    $0x64,%esi
sub    %r12d,%esi
mov    %ebx,%edx
add    0x10(%rsp),%rdx
mov    %r13d,%ecx
sub    %ebx,%ecx
mov    %ebp,%edi
call   1030 <feed_sample@plt>
add    %eax,%ebx
mov    %ebp,%edi
call   1070 <get_time_in_buffer@plt>
mov    %eax,%r12d
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
mov    %ebx,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
cmp    $0x33,%r12d
jae    11a0 <play_sample+0x40>
jmp    1215 <play_sample+0xb5>
xor    %ebx,%ebx
jmp    1283 <play_sample+0x123>
mov    %r13d,%ebx
mov    %ebx,%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 }
@stderr = global i32 0
@.str = [26 x i8] c"done %u, %u ms in buffer\0A\00"
@.str.1 = [12 x i8] c"Time is %u\0A\00"
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 = %69, %4
%15 = load i32, i32* %9
%16 = load i32, i32* %8
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %70
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 %44
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* %6
%39 = call i32 @get_time_in_buffer(i32 %38)
store i32 %39, i32* %12
%40 = load i32, i32* @stderr
%41 = load i32, i32* %9
%42 = load i32, i32* %12
%43 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %42)
br label %44
44: ; preds = %23, %18
%45 = load i32, i32* %12
%46 = icmp ugt i32 %45, 50
br i1 %46, label %47, label %69
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = load i32, i32* %12
%50 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %12
%52 = sub i32 %51, 50
store i32 %52, i32* %12
%53 = call i32 @FD_ZERO(i32* %10)
%54 = load i32, i32* %5
%55 = call i32 @FD_SET(i32 %54, i32* %10)
%56 = load i32, i32* %12
%57 = urem i32 %56, 1000
%58 = mul i32 %57, 1000
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %58, i32* %59
%60 = load i32, i32* %12
%61 = udiv i32 %60, 1000
%62 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i32 %61, i32* %62
%63 = load i32, i32* %5
%64 = add nsw i32 %63, 1
%65 = call i64 @select(i32 %64, i32* %10, i32* null, i32* null, %struct.timeval* %11)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %68
67: ; preds = %47
br label %70
68: ; preds = %47
br label %69
69: ; preds = %68, %44
br label %14
70: ; preds = %67, %14
%71 = load i32, i32* %9
ret i32 %71
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@stderr = global i32 0
@.str = [26 x i8] c"done %u, %u ms in buffer\0A\00"
@.str.1 = [12 x i8] c"Time is %u\0A\00"
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 %45, 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, %42
%15 = phi i32 [ 0, %10 ], [ %43, %42 ]
%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 = call i32 @get_time_in_buffer(i32 %1)
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
%28 = icmp ugt i32 %25, 50
br i1 %28, label %29, label %42
29: ; preds = %14, %18
%30 = phi i32 [ %24, %18 ], [ %15, %14 ]
%31 = phi i32 [ %25, %18 ], [ %16, %14 ]
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%34 = add i32 %31, -50
%35 = call i32 @FD_ZERO(i32* %5)
%36 = call i32 @FD_SET(i32 %0, i32* %5)
%37 = urem i32 %34, 1000
%38 = mul nuw nsw i32 %37, 1000
store i32 %38, i32* %11
%39 = udiv i32 %34, 1000
store i32 %39, i32* %12
%40 = call i64 @select(i32 %13, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %18, %29
%43 = phi i32 [ %30, %29 ], [ %24, %18 ]
%44 = icmp eq i32 %43, %3
br i1 %44, label %45, label %14
45: ; preds = %42, %29, %4
%46 = phi i32 [ 0, %4 ], [ %30, %29 ], [ %3, %42 ]
ret i32 %46
}