#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 char uint8_t;
typedef scalar_t__ ssize_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int execvp(char *, char **);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  int soundfifo, pru_data, pru_clock;
  pid_t forkit;
  uint8_t sinebuf[490];
  forkit = fork();
  if (!forkit) {
    int result;
    char *arguments[] = {"aplay",       "--format=S16_LE", "-Dplughw:1,0",
                         "--rate=8000", "soundfifo",       NULL};
    result = execvp("aplay", arguments);
    printf("The return value of execvp(aplay) is %d.\n", result);
  } else if (forkit > 0) {
    ssize_t readpru, writepipe, prime_char, pru_clock_command;
    soundfifo = open("soundfifo", O_RDWR);
    if (soundfifo < 0)
      printf("Failed to open soundfifo.");
    pru_data = open("/dev/rpmsg_pru30", O_RDWR);
    if (pru_data < 0)
      printf("Failed to open pru character device rpmsg_pru30.");
    prime_char = write(pru_data, "prime", 6);
    if (prime_char < 0)
      printf("Failed to prime the PRU0 char device.");
    pru_clock = open("/dev/rpmsg_pru31", O_RDWR);
    pru_clock_command = write(pru_clock, "g", 2);
    if (pru_clock_command < 0)
      printf("The pru clock start command failed.");
    for (int i = 0; i < 20000000; i++) {
      readpru = read(pru_data, sinebuf, 490);
      writepipe = write(soundfifo, sinebuf, 490);
    }
    printf("The last value of readpru is %d and the last value of writepipe is "
           "%d.\n",
           readpru, writepipe);
  } else if (forkit == -1)
    perror("fork error");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1238 <main+0xb8>
lea    -0x250(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
lea    0xe39(%rip),%rax
mov    %rax,-0x250(%rbp)
lea    0xe31(%rip),%rax
mov    %rax,-0x248(%rbp)
lea    0xe33(%rip),%rax
mov    %rax,-0x240(%rbp)
lea    0xe32(%rip),%rax
mov    %rax,-0x238(%rbp)
lea    0xe30(%rip),%rax
mov    %rax,-0x230(%rbp)
lea    -0x250(%rbp),%rsi
lea    0xdec(%rip),%rdi
call   1090 <execvp@plt>
mov    %eax,-0x214(%rbp)
mov    -0x214(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13cd <main+0x24d>
cmpl   $0x0,-0x20(%rbp)
jle    13ad <main+0x22d>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    0xddd(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1272 <main+0xf2>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    0xdfb(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a2 <main+0x122>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xe13(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %rax,-0x268(%rbp)
cmpq   $0x0,-0x268(%rbp)
jge    12d9 <main+0x159>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
lea    0xe02(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xe01(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %rax,-0x270(%rbp)
cmpq   $0x0,-0x270(%rbp)
jge    1328 <main+0x1a8>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x274(%rbp)
cmpl   $0x1312d00,-0x274(%rbp)
2d 31 01
jge    138c <main+0x20c>
mov    -0x18(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1ea,%edx
call   1060 <read@plt>
mov    %rax,-0x258(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1ea,%edx
call   1030 <write@plt>
mov    %rax,-0x260(%rbp)
mov    -0x274(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x274(%rbp)
jmp    1332 <main+0x1b2>
mov    -0x258(%rbp),%rsi
mov    -0x260(%rbp),%rdx
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13c8 <main+0x248>
cmpl   $0xffffffff,-0x20(%rbp)
jne    13c3 <main+0x243>
lea    0xdac(%rip),%rdi
call   1080 <perror@plt>
jmp    13c8 <main+0x248>
jmp    13cd <main+0x24d>
mov    -0x4(%rbp),%eax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1f0,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     128e <main+0x11e>
jle    12eb <main+0x17b>
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11be <main+0x4e>
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%esi
lea    0xeb6(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11e1 <main+0x71>
lea    0xeb4(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed7(%rip),%rsi
mov    %r15d,%edi
mov    $0x6,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1208 <main+0x98>
lea    0xec4(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%esi
lea    0xeda(%rip),%rdi
call   1060 <open@plt>
lea    0xedf(%rip),%rsi
mov    %eax,%edi
mov    $0x2,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    123c <main+0xcc>
lea    0xec9(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1312d00,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1ea,%edx
call   1050 <read@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1ea,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebx
jne    1250 <main+0xe0>
lea    0xea3(%rip),%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12f0 <main+0x180>
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xd63(%rip),%rdi
mov    %rdi,(%rsp)
lea    0xd5e(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xd62(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xd63(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xd63(%rip),%rax
mov    %rax,0x20(%rsp)
mov    %rsp,%rsi
call   1080 <execvp@plt>
lea    0xd59(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12f0 <main+0x180>
cmp    $0xffffffff,%eax
je     1302 <main+0x192>
xor    %eax,%eax
add    $0x1f0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
jmp    12f0 <main+0x180>

LLVM IR (-O0):
@.str = [6 x i8] c"aplay\00"
@.str.1 = [16 x i8] c"--format=S16_LE\00"
@.str.2 = [13 x i8] c"-Dplughw:1,0\00"
@.str.3 = [12 x i8] c"--rate=8000\00"
@.str.4 = [10 x i8] c"soundfifo\00"
@.str.5 = [42 x i8] c"The return value of execvp(aplay) is %d.\0A\00"
@O_RDWR = global i32 0
@.str.6 = [26 x i8] c"Failed to open soundfifo.\00"
@.str.7 = [17 x i8] c"/dev/rpmsg_pru30\00"
@.str.8 = [49 x i8] c"Failed to open pru character device rpmsg_pru30.\00"
@.str.9 = [6 x i8] c"prime\00"
@.str.10 = [38 x i8] c"Failed to prime the PRU0 char device.\00"
@.str.11 = [17 x i8] c"/dev/rpmsg_pru31\00"
@.str.12 = [2 x i8] c"g\00"
@.str.13 = [36 x i8] c"The pru clock start command failed.\00"
@.str.14 = [72 x i8] c"The last value of readpru is %d and the last value of writepipe is %d.\0A\00"
@.str.15 = [11 x i8] c"fork error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [490 x i8]
%11 = alloca i32
%12 = alloca [6 x i8*]
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = call i32 (...) @fork()
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %2
%22 = bitcast [6 x i8*]* %12 to i8*
%23 = bitcast i8* %22 to [6 x i8*]*
%24 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8** %24
%25 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i32 0, i32 0), i8** %25
%26 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i32 0, i32 0), i8** %26
%27 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 3
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i32 0, i32 0), i8** %27
%28 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 4
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i32 0, i32 0), i8** %28
%29 = getelementptr inbounds [6 x i8*], [6 x i8*]* %12, i64 0, i64 0
%30 = call i32 @execvp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %29)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %31)
br label %91
33: ; preds = %2
%34 = load i32, i32* %9
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %84
36: ; preds = %33
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = load i32, i32* @O_RDWR
%45 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.8, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = load i32, i32* %7
%52 = call i64 @write(i32 %51, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 6)
store i64 %52, i64* %15
%53 = load i64, i64* %15
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.10, i64 0, i64 0))
br label %57
57: ; preds = %55, %50
%58 = load i32, i32* @O_RDWR
%59 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.11, i64 0, i64 0), i32 %58)
store i32 %59, i32* %8
%60 = load i32, i32* %8
%61 = call i64 @write(i32 %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0), i32 2)
store i64 %61, i64* %16
%62 = load i64, i64* %16
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %57
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0))
br label %66
66: ; preds = %64, %57
store i32 0, i32* %17
br label %67
67: ; preds = %77, %66
%68 = load i32, i32* %17
%69 = icmp slt i32 %68, 20000000
br i1 %69, label %70, label %80
70: ; preds = %67
%71 = load i32, i32* %7
%72 = getelementptr inbounds [490 x i8], [490 x i8]* %10, i64 0, i64 0
%73 = call i64 @read(i32 %71, i8* %72, i32 490)
store i64 %73, i64* %13
%74 = load i32, i32* %6
%75 = getelementptr inbounds [490 x i8], [490 x i8]* %10, i64 0, i64 0
%76 = call i64 @write(i32 %74, i8* %75, i32 490)
store i64 %76, i64* %14
br label %77
77: ; preds = %70
%78 = load i32, i32* %17
%79 = add nsw i32 %78, 1
store i32 %79, i32* %17
br label %67
80: ; preds = %67
%81 = load i64, i64* %13
%82 = load i64, i64* %14
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.14, i64 0, i64 0), i64 %81, i64 %82)
br label %90
84: ; preds = %33
%85 = load i32, i32* %9
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %89
87: ; preds = %84
%88 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.15, i64 0, i64 0))
br label %89
89: ; preds = %87, %84
br label %90
90: ; preds = %89, %80
br label %91
91: ; preds = %90, %21
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [6 x i8] c"aplay\00"
@.str.1 = [16 x i8] c"--format=S16_LE\00"
@.str.2 = [13 x i8] c"-Dplughw:1,0\00"
@.str.3 = [12 x i8] c"--rate=8000\00"
@.str.4 = [10 x i8] c"soundfifo\00"
@.str.5 = [42 x i8] c"The return value of execvp(aplay) is %d.\0A\00"
@O_RDWR = global i32 0
@.str.6 = [26 x i8] c"Failed to open soundfifo.\00"
@.str.7 = [17 x i8] c"/dev/rpmsg_pru30\00"
@.str.8 = [49 x i8] c"Failed to open pru character device rpmsg_pru30.\00"
@.str.9 = [6 x i8] c"prime\00"
@.str.10 = [38 x i8] c"Failed to prime the PRU0 char device.\00"
@.str.11 = [17 x i8] c"/dev/rpmsg_pru31\00"
@.str.12 = [2 x i8] c"g\00"
@.str.13 = [36 x i8] c"The pru clock start command failed.\00"
@.str.14 = [72 x i8] c"The last value of readpru is %d and the last value of writepipe is %d.\0A\00"
@.str.15 = [11 x i8] c"fork error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [490 x i8]
%4 = alloca [6 x i8*]
%5 = getelementptr inbounds [490 x i8], [490 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @fork()
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %19
8: ; preds = %2
%9 = bitcast [6 x i8*]* %4 to i8*
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 4
%11 = bitcast i8** %10 to i8*
%12 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 3
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 4
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %16
%17 = call i32 @execvp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %12)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %17)
br label %58
19: ; preds = %2
%20 = icmp sgt i32 %6, 0
br i1 %20, label %21, label %54
21: ; preds = %19
%22 = load i32, i32* @O_RDWR
%23 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = load i32, i32* @O_RDWR
%29 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.8, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = tail call i64 @write(i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 6)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.10, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = load i32, i32* @O_RDWR
%40 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.11, i64 0, i64 0), i32 %39)
%41 = tail call i64 @write(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0), i32 2)
%42 = icmp slt i64 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %44, %38
br label %48
44: ; preds = %38
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0))
br label %43
46: ; preds = %48
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @.str.14, i64 0, i64 0), i64 %50, i64 %51)
br label %58
48: ; preds = %43, %48
%49 = phi i32 [ %52, %48 ], [ 0, %43 ]
%50 = call i64 @read(i32 %29, i8* %5, i32 490)
%51 = call i64 @write(i32 %23, i8* %5, i32 490)
%52 = add nuw nsw i32 %49, 1
%53 = icmp eq i32 %52, 20000000
br i1 %53, label %46, label %48
54: ; preds = %19
%55 = icmp eq i32 %6, -1
br i1 %55, label %56, label %58
56: ; preds = %54
%57 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.15, i64 0, i64 0))
br label %58
58: ; preds = %46, %56, %54, %8
ret i32 0
}