#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 {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ play_with_decoder(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ setlinebuf(int /*<<< orphan*/ *);
double timediff(struct timeval *, struct timeval *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main() {
  int dec_stdin[2], dec_err[2];
  int pid;
  int retval = 0;
  struct timeval ta, tb;
  gettimeofday(&ta, NULL);
  pipe(dec_stdin);
  pipe(dec_err);
  pid = fork();
  if (pid == 0) {
    close(dec_stdin[1]);
    close(dec_err[0]);
    dup2(dec_stdin[0], STDIN_FILENO);
    dup2(dec_err[1], STDERR_FILENO);
    execlp("mpg123", "-q", "-s", "-R", "--remote-err", NULL);
    exit(1);
  }
  if (waitpid(pid, &retval, WNOHANG) == 0) {
    FILE *status;
    FILE *control;
    close(dec_stdin[0]);
    close(dec_err[1]);
    fcntl(dec_stdin[1], F_SETFD, FD_CLOEXEC);
    fcntl(dec_err[0], F_SETFD, FD_CLOEXEC);
    status = fdopen(dec_err[0], "r");
    control = fdopen(dec_stdin[1], "a");
    setlinebuf(control);
    setlinebuf(status);
    play_with_decoder(control, status);
    fprintf(control, "quit\n");
    waitpid(pid, &retval, 0);
  }
  gettimeofday(&tb, NULL);
  printf("Needed %f ms for the whole deal\n", timediff(&ta, &tb));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0xc(%rbp),%rdi
call   1090 <pipe@plt>
lea    -0x14(%rbp),%rdi
call   1090 <pipe@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    12a2 <main+0xb2>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    0xd8e(%rip),%rdi
lea    0xd8e(%rip),%rsi
lea    0xd8a(%rip),%rdx
lea    0xd86(%rip),%rcx
lea    0xd82(%rip),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   10f0 <execlp@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   10d0 <waitpid@plt>
cmp    $0x0,%eax
jne    1367 <main+0x177>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
mov    0x2ced(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%edi
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
mov    0x2cd3(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%edi
lea    0xd0d(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    0xcfc(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <setlinebuf@plt>
mov    -0x30(%rbp),%rdi
call   1070 <setlinebuf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <play_with_decoder@plt>
mov    -0x38(%rbp),%rdi
lea    0xccb(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   10d0 <waitpid@plt>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1110 <timediff@plt>
lea    0xc9d(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movl   $0x0,0xc(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
lea    0x18(%rsp),%rdi
call   1090 <pipe@plt>
lea    0x10(%rsp),%rdi
call   1090 <pipe@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1325 <main+0x135>
mov    %eax,%ebp
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   10c0 <waitpid@plt>
test   %eax,%eax
jne    12f2 <main+0x102>
mov    0x18(%rsp),%edi
call   1080 <close@plt>
mov    0x14(%rsp),%edi
call   1080 <close@plt>
mov    0x1c(%rsp),%edi
mov    0x2d6d(%rip),%r14
mov    (%r14),%esi
mov    0x2d5b(%rip),%rbx
mov    (%rbx),%edx
call   1030 <fcntl@plt>
mov    0x10(%rsp),%edi
mov    (%r14),%esi
mov    (%rbx),%edx
call   1030 <fcntl@plt>
mov    0x10(%rsp),%edi
lea    0xd86(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,%r14
mov    0x1c(%rsp),%edi
lea    0xd75(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <setlinebuf@plt>
mov    %r14,%rdi
call   1070 <setlinebuf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <play_with_decoder@plt>
lea    0xd4d(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10c0 <waitpid@plt>
lea    0x28(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
lea    0x20(%rsp),%rdi
mov    %rbx,%rsi
call   1110 <timediff@plt>
lea    0xd10(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x1c(%rsp),%edi
call   1080 <close@plt>
mov    0x10(%rsp),%edi
call   1080 <close@plt>
mov    0x18(%rsp),%edi
mov    0x2c7e(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x14(%rsp),%edi
mov    0x2c74(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    0xc9e(%rip),%rdi
lea    0xc9e(%rip),%rsi
lea    0xc9a(%rip),%rdx
lea    0xc96(%rip),%rcx
lea    0xc92(%rip),%r8
xor    %r9d,%r9d
call   10f0 <execlp@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32 }
@STDIN_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [7 x i8] c"mpg123\00"
@.str.1 = [3 x i8] c"-q\00"
@.str.2 = [3 x i8] c"-s\00"
@.str.3 = [3 x i8] c"-R\00"
@.str.4 = [13 x i8] c"--remote-err\00"
@WNOHANG = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [6 x i8] c"quit\0A\00"
@.str.8 = [33 x i8] c"Needed %f ms for the whole deal\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i32*
%9 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %5
%10 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%14 = call i32 @pipe(i32* %13)
%15 = call i32 (...) @fork()
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %35
18: ; preds = %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %25
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = load i32, i32* @STDERR_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = call i32 @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32* null)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %0
%36 = load i32, i32* %4
%37 = load i32, i32* @WNOHANG
%38 = call i32 @waitpid(i32 %36, i32* %5, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %74
40: ; preds = %35
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%48 = load i32, i32* %47
%49 = load i32, i32* @F_SETFD
%50 = load i32, i32* @FD_CLOEXEC
%51 = call i32 @fcntl(i32 %48, i32 %49, i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%53 = load i32, i32* %52
%54 = load i32, i32* @F_SETFD
%55 = load i32, i32* @FD_CLOEXEC
%56 = call i32 @fcntl(i32 %53, i32 %54, i32 %55)
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32* @fdopen(i32 %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %59, i32** %8
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32* @fdopen(i32 %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %62, i32** %9
%63 = load i32*, i32** %9
%64 = call i32 @setlinebuf(i32* %63)
%65 = load i32*, i32** %8
%66 = call i32 @setlinebuf(i32* %65)
%67 = load i32*, i32** %9
%68 = load i32*, i32** %8
%69 = call i32 @play_with_decoder(i32* %67, i32* %68)
%70 = load i32*, i32** %9
%71 = call i32 @fprintf(i32* %70, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%72 = load i32, i32* %4
%73 = call i32 @waitpid(i32 %72, i32* %5, i32 0)
br label %74
74: ; preds = %40, %35
%75 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%76 = call double @timediff(%struct.timeval* %6, %struct.timeval* %7)
%77 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), double %76)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@STDIN_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [7 x i8] c"mpg123\00"
@.str.1 = [3 x i8] c"-q\00"
@.str.2 = [3 x i8] c"-s\00"
@.str.3 = [3 x i8] c"-R\00"
@.str.4 = [13 x i8] c"--remote-err\00"
@WNOHANG = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [6 x i8] c"quit\0A\00"
@.str.8 = [33 x i8] c"Needed %f ms for the whole deal\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast [2 x i32]* %1 to i8*
%7 = bitcast [2 x i32]* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = bitcast %struct.timeval* %4 to i8*
%10 = bitcast %struct.timeval* %5 to i8*
%11 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = call i32 (...) @fork()
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %14
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %12
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
%29 = load i32, i32* @STDERR_FILENO
%30 = call i32 @dup2(i32 %28, i32 %29)
%31 = call i32 @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32* null)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %0
%34 = load i32, i32* @WNOHANG
%35 = call i32 @waitpid(i32 %16, i32* %3, i32 %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %61
37: ; preds = %33
%38 = load i32, i32* %12
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i32, i32* @F_SETFD
%46 = load i32, i32* @FD_CLOEXEC
%47 = call i32 @fcntl(i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* %14
%49 = load i32, i32* @F_SETFD
%50 = load i32, i32* @FD_CLOEXEC
%51 = call i32 @fcntl(i32 %48, i32 %49, i32 %50)
%52 = load i32, i32* %14
%53 = call i32* @fdopen(i32 %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%54 = load i32, i32* %43
%55 = call i32* @fdopen(i32 %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 @setlinebuf(i32* %55)
%57 = call i32 @setlinebuf(i32* %53)
%58 = call i32 @play_with_decoder(i32* %55, i32* %53)
%59 = call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i64 5, i64 1, i32* %55)
%60 = call i32 @waitpid(i32 %16, i32* %3, i32 0)
br label %61
61: ; preds = %37, %33
%62 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%63 = call double @timediff(%struct.timeval* %4, %struct.timeval* %5)
%64 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), double %63)
ret i32 0
}