#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 */
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ write(int, char *, int);
int writen(int, char *, int);
int start_audio(int sfd) {
  fd_set allfds;
  int fd;
  char buf[1024];
  printf("start audio ....\n");
  if ((fd = open("/dev/dsp", O_RDWR)) < 0) {
    fprintf(stderr, "open \"/dev/dsp\":%s\n", strerror(errno));
    return -1;
  }
  fcntl(sfd, F_SETFL, O_NONBLOCK);
  FD_ZERO(&allfds);
  FD_SET(sfd, &allfds);
  FD_SET(fd, &allfds);
  while (1) {
    fd_set fds = allfds;
    if (select(50, &fds, 0, NULL, NULL) < 0) {
      perror("select");
      break;
    }
    if (FD_ISSET(fd, &fds)) {
      int n = read(fd, buf, sizeof(buf));
      if (n <= 0) {
        return -1;
      }
      if (write(sfd, buf, n) < 0 && errno != EAGAIN) {
        close(fd);
        return -1;
      }
    }
    if (FD_ISSET(sfd, &fds)) {
      int n = read(sfd, buf, sizeof(buf));
      if (n <= 0) {
        close(fd);
        return -1;
      }
      if ((writen(fd, buf, n) != n))
        return -1;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<start_audio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe0b(%rip),%rdi
call   1060 <printf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe08(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    125a <start_audio+0x7a>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x420(%rbp),%edi
mov    %rax,%rdx
lea    0xdd2(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    140c <start_audio+0x22c>
mov    -0x8(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
lea    -0xc(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0x10(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x414(%rbp)
mov    $0x32,%edi
lea    -0x414(%rbp),%rsi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   10b0 <select@plt>
cmp    $0x0,%rax
jge    12d4 <start_audio+0xf4>
lea    0xd65(%rip),%rdi
call   10d0 <perror@plt>
jmp    13fd <start_audio+0x21d>
mov    -0x10(%rbp),%edi
lea    -0x414(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1372 <start_audio+0x192>
mov    -0x10(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jg     1320 <start_audio+0x140>
movl   $0xffffffff,-0x4(%rbp)
jmp    140c <start_audio+0x22c>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    136d <start_audio+0x18d>
mov    0x2c7a(%rip),%rax
mov    (%rax),%rax
mov    0x2c60(%rip),%rcx
cmp    (%rcx),%rax
je     136d <start_audio+0x18d>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    140c <start_audio+0x22c>
jmp    1372 <start_audio+0x192>
mov    -0x8(%rbp),%edi
lea    -0x414(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13f8 <start_audio+0x218>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jg     13c6 <start_audio+0x1e6>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    140c <start_audio+0x22c>
mov    -0x10(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   10e0 <writen@plt>
cmp    -0x41c(%rbp),%eax
je     13f3 <start_audio+0x213>
movl   $0xffffffff,-0x4(%rbp)
jmp    140c <start_audio+0x22c>
jmp    13f8 <start_audio+0x218>
jmp    1295 <start_audio+0xb5>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_audio>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edi,%r13d
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0xdf0(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     1353 <start_audio+0x173>
mov    %eax,%ebp
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
call   1040 <fcntl@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1100 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1100 <FD_SET@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
xor    %r14d,%r14d
lea    0x8(%rsp),%rsi
mov    $0x32,%edi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %rax,%rax
js     13a0 <start_audio+0x1c0>
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    12c5 <start_audio+0xe5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    $0x32,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %rax,%rax
js     13a0 <start_audio+0x1c0>
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1313 <start_audio+0x133>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1385 <start_audio+0x1a5>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %rax,%rax
jns    1313 <start_audio+0x133>
mov    0x2cbc(%rip),%rax
mov    (%rax),%rax
mov    0x2ca2(%rip),%rcx
cmp    (%rcx),%rax
jne    137e <start_audio+0x19e>
mov    %r13d,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     12a0 <start_audio+0xc0>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    137e <start_audio+0x19e>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   10e0 <writen@plt>
cmp    %ebx,%eax
je     12a0 <start_audio+0xc0>
jmp    1385 <start_audio+0x1a5>
mov    0x2c76(%rip),%rax
mov    (%rax),%ebx
mov    0x2c5d(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
lea    0xc97(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
jmp    1385 <start_audio+0x1a5>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc76(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    138b <start_audio+0x1ab>

LLVM IR (-O0):
@.str = [18 x i8] c"start audio ....\0A\00"
@.str.1 = [9 x i8] c"/dev/dsp\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"open \22/dev/dsp\22:%s\0A\00"
@errno = global i64 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [7 x i8] c"select\00"
@EAGAIN = global i64 0
define i32 @start_audio(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i64, i64* @errno
%17 = call i8* @strerror(i64 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %17)
store i32 -1, i32* %2
br label %86
19: ; preds = %1
%20 = load i32, i32* %3
%21 = load i32, i32* @F_SETFL
%22 = load i32, i32* @O_NONBLOCK
%23 = call i32 @fcntl(i32 %20, i32 %21, i32 %22)
%24 = call i32 @FD_ZERO(i32* %4)
%25 = load i32, i32* %3
%26 = call i32 @FD_SET(i32 %25, i32* %4)
%27 = load i32, i32* %5
%28 = call i32 @FD_SET(i32 %27, i32* %4)
br label %29
29: ; preds = %19, %82
%30 = load i32, i32* %4
store i32 %30, i32* %7
%31 = call i64 @select(i32 50, i32* %7, i32 0, i32* null, i32* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %83
35: ; preds = %29
%36 = load i32, i32* %5
%37 = call i64 @FD_ISSET(i32 %36, i32* %7)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %60
39: ; preds = %35
%40 = load i32, i32* %5
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 1024)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp sle i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39
store i32 -1, i32* %2
br label %86
46: ; preds = %39
%47 = load i32, i32* %3
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%49 = load i32, i32* %8
%50 = call i64 @write(i32 %47, i8* %48, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %46
%53 = load i64, i64* @errno
%54 = load i64, i64* @EAGAIN
%55 = icmp ne i64 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i32, i32* %5
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %2
br label %86
59: ; preds = %52, %46
br label %60
60: ; preds = %59, %35
%61 = load i32, i32* %3
%62 = call i64 @FD_ISSET(i32 %61, i32* %7)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %82
64: ; preds = %60
%65 = load i32, i32* %3
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%67 = call i32 @read(i32 %65, i8* %66, i32 1024)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp sle i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = load i32, i32* %5
%72 = call i32 @close(i32 %71)
store i32 -1, i32* %2
br label %86
73: ; preds = %64
%74 = load i32, i32* %5
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%76 = load i32, i32* %9
%77 = call i32 @writen(i32 %74, i8* %75, i32 %76)
%78 = load i32, i32* %9
%79 = icmp ne i32 %77, %78
br i1 %79, label %80, label %81
80: ; preds = %73
store i32 -1, i32* %2
br label %86
81: ; preds = %73
br label %82
82: ; preds = %81, %60
br label %29
83: ; preds = %33
%84 = load i32, i32* %5
%85 = call i32 @close(i32 %84)
store i32 0, i32* %2
br label %86
86: ; preds = %83, %80, %70, %56, %45, %14
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"/dev/dsp\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.2 = [20 x i8] c"open \22/dev/dsp\22:%s\0A\00"
@errno = global i64 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [7 x i8] c"select\00"
@EAGAIN = global i64 0
@str = [17 x i8] c"start audio ....\00"
define i32 @start_audio(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i64, i64* @errno
%14 = tail call i8* @strerror(i64 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %59
16: ; preds = %1
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @O_NONBLOCK
%19 = tail call i32 @fcntl(i32 %0, i32 %17, i32 %18)
%20 = call i32 @FD_ZERO(i32* %2)
%21 = call i32 @FD_SET(i32 %0, i32* %2)
%22 = call i32 @FD_SET(i32 %9, i32* %2)
%23 = bitcast i32* %4 to i8*
%24 = load i32, i32* %2
store i32 %24, i32* %4
%25 = call i64 @select(i32 50, i32* %4, i32 0, i32* null, i32* null)
%26 = icmp slt i64 %25, 0
br i1 %26, label %56, label %27
27: ; preds = %16, %52
%28 = call i64 @FD_ISSET(i32 %9, i32* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %27
%31 = call i32 @read(i32 %9, i8* %6, i32 1024)
%32 = icmp slt i32 %31, 1
br i1 %32, label %51, label %33
33: ; preds = %30
%34 = call i64 @write(i32 %0, i8* %6, i32 %31)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %49
40: ; preds = %36, %33, %27
%41 = call i64 @FD_ISSET(i32 %0, i32* %4)
%42 = icmp eq i64 %41, 0
br i1 %42, label %52, label %43
43: ; preds = %40
%44 = call i32 @read(i32 %0, i8* %6, i32 1024)
%45 = icmp slt i32 %44, 1
br i1 %45, label %49, label %46
46: ; preds = %43
%47 = call i32 @writen(i32 %9, i8* %6, i32 %44)
%48 = icmp eq i32 %47, %44
br i1 %48, label %52, label %51
49: ; preds = %43, %36
%50 = call i32 @close(i32 %9)
br label %51
51: ; preds = %46, %30, %49
br label %59
52: ; preds = %40, %46
%53 = load i32, i32* %2
store i32 %53, i32* %4
%54 = call i64 @select(i32 50, i32* %4, i32 0, i32* null, i32* null)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %27
56: ; preds = %52, %16
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 @close(i32 %9)
br label %59
59: ; preds = %51, %56, %11
%60 = phi i32 [ -1, %11 ], [ 0, %56 ], [ -1, %51 ]
ret i32 %60
}