#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 */

/* Variables and functions */
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read_bytes(int, unsigned char *, int);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int midi_reader(int fd, unsigned char *shared_data) {
  int rc;
  unsigned char ch;
  int ppid;
  unsigned char note, velocity;
  ppid = getpid();
  rc = fork();
  if (rc != 0)
    return rc;
  printf("Midi reader process running.\n");
  while (1) {
    rc = read_bytes(fd, &ch, 1);
    if (rc < 0)
      goto read_error;
    if (ch != 0xfe) {
      printf("0x%02x ", ch);
      fflush(stdout);
    }
    if (ch == 0x90) {
      rc = read_bytes(fd, &note, 1);
      if (rc < 0)
        goto read_error;
      rc = read_bytes(fd, &velocity, 1);
      if (rc < 0)
        goto read_error;
      if (velocity != 0) {
        shared_data[0] = ch;
        shared_data[1] = note;
        shared_data[2] = velocity;
        kill(ppid, SIGUSR1);
      }
    }
  }
read_error:
  if (rc)
    printf("rc = %d, errno='%s'\n", rc, strerror(errno));
  exit(0);
}


x86 assembly (clang, -O0):
<midi_reader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b6 <midi_reader+0x36>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read_bytes@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e7 <midi_reader+0x67>
jmp    12b0 <midi_reader+0x130>
movzbl -0x15(%rbp),%eax
cmp    $0xfe,%eax
je     1216 <midi_reader+0x96>
movzbl -0x15(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movzbl -0x15(%rbp),%eax
cmp    $0x90,%eax
jne    12ab <midi_reader+0x12b>
mov    -0x4(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read_bytes@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1248 <midi_reader+0xc8>
jmp    12b0 <midi_reader+0x130>
mov    -0x4(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read_bytes@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    126b <midi_reader+0xeb>
jmp    12b0 <midi_reader+0x130>
movzbl -0x1e(%rbp),%eax
cmp    $0x0,%eax
je     12a6 <midi_reader+0x126>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1d(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x1e(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x1c(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
jmp    12ab <midi_reader+0x12b>
jmp    11c4 <midi_reader+0x44>
cmpl   $0x0,-0x14(%rbp)
je     12e2 <midi_reader+0x162>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%esi
mov    %rax,%rdx
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<midi_reader>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11c4 <midi_reader+0x34>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
lea    0x5(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <read_bytes@plt>
mov    %eax,%r13d
test   %eax,%eax
js     12a0 <midi_reader+0x110>
lea    0x5(%rsp),%r15
mov    0x2dd8(%rip),%r12
jmp    1217 <midi_reader+0x87>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read_bytes@plt>
test   %eax,%eax
js     129d <midi_reader+0x10d>
movzbl 0x5(%rsp),%esi
cmp    $0xfe,%esi
je     1200 <midi_reader+0x70>
lea    0xdd5(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r12),%edi
call   1080 <fflush@plt>
cmpb   $0x90,0x5(%rsp)
jne    1200 <midi_reader+0x70>
mov    %ebp,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read_bytes@plt>
test   %eax,%eax
js     129d <midi_reader+0x10d>
mov    %ebp,%edi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read_bytes@plt>
test   %eax,%eax
js     129d <midi_reader+0x10d>
movzbl 0x6(%rsp),%eax
test   %al,%al
je     1200 <midi_reader+0x70>
movzbl 0x5(%rsp),%ecx
mov    %cl,(%rbx)
movzbl 0x7(%rsp),%ecx
mov    %cl,0x1(%rbx)
mov    %al,0x2(%rbx)
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1070 <kill@plt>
jmp    1200 <midi_reader+0x70>
mov    %eax,%r13d
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd53(%rip),%rdi
mov    %r13d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"Midi reader process running.\0A\00"
@.str.1 = [8 x i8] c"0x%02x \00"
@stdout = global i32 0
@SIGUSR1 = global i32 0
@.str.2 = [21 x i8] c"rc = %d, errno='%s'\0A\00"
@errno = global i32 0
define i32 @midi_reader(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = call i32 (...) @getpid()
store i32 %10, i32* %7
%11 = call i32 (...) @fork()
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %5
ret i32 %15
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %68
%19 = load i32, i32* %3
%20 = call i32 @read_bytes(i32 %19, i8* %6, i32 1)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %69
24: ; preds = %18
%25 = load i8, i8* %6
%26 = zext i8 %25 to i32
%27 = icmp ne i32 %26, 254
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i8, i8* %6
%30 = zext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* @stdout
%33 = call i32 @fflush(i32 %32)
br label %34
34: ; preds = %28, %24
%35 = load i8, i8* %6
%36 = zext i8 %35 to i32
%37 = icmp eq i32 %36, 144
br i1 %37, label %38, label %68
38: ; preds = %34
%39 = load i32, i32* %3
%40 = call i32 @read_bytes(i32 %39, i8* %8, i32 1)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
br label %69
44: ; preds = %38
%45 = load i32, i32* %3
%46 = call i32 @read_bytes(i32 %45, i8* %9, i32 1)
store i32 %46, i32* %5
%47 = load i32, i32* %5
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
br label %69
50: ; preds = %44
%51 = load i8, i8* %9
%52 = zext i8 %51 to i32
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %67
54: ; preds = %50
%55 = load i8, i8* %6
%56 = load i8*, i8** %4
%57 = getelementptr inbounds i8, i8* %56, i64 0
store i8 %55, i8* %57
%58 = load i8, i8* %8
%59 = load i8*, i8** %4
%60 = getelementptr inbounds i8, i8* %59, i64 1
store i8 %58, i8* %60
%61 = load i8, i8* %9
%62 = load i8*, i8** %4
%63 = getelementptr inbounds i8, i8* %62, i64 2
store i8 %61, i8* %63
%64 = load i32, i32* %7
%65 = load i32, i32* @SIGUSR1
%66 = call i32 @kill(i32 %64, i32 %65)
br label %67
67: ; preds = %54, %50
br label %68
68: ; preds = %67, %34
br label %18
69: ; preds = %49, %43, %23
%70 = load i32, i32* %5
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %69
%73 = load i32, i32* %5
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %73, i8* %75)
br label %77
77: ; preds = %72, %69
%78 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"0x%02x \00"
@stdout = global i32 0
@SIGUSR1 = global i32 0
@.str.2 = [21 x i8] c"rc = %d, errno='%s'\0A\00"
@errno = global i32 0
@str = [29 x i8] c"Midi reader process running.\00"
define i32 @midi_reader(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca i8
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %10, label %9
9: ; preds = %2
ret i32 %7
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%12 = call i32 @read_bytes(i32 %0, i8* %3, i32 1)
%13 = icmp slt i32 %12, 0
br i1 %13, label %44, label %14
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %1, i64 1
%16 = getelementptr inbounds i8, i8* %1, i64 2
br label %17
17: ; preds = %14, %41
%18 = load i8, i8* %3
%19 = icmp eq i8 %18, -2
br i1 %19, label %41, label %20
20: ; preds = %17
%21 = zext i8 %18 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
%25 = load i8, i8* %3
%26 = icmp eq i8 %25, -112
br i1 %26, label %27, label %41
27: ; preds = %20
%28 = call i32 @read_bytes(i32 %0, i8* %4, i32 1)
%29 = icmp slt i32 %28, 0
br i1 %29, label %44, label %30
30: ; preds = %27
%31 = call i32 @read_bytes(i32 %0, i8* %5, i32 1)
%32 = icmp slt i32 %31, 0
br i1 %32, label %44, label %33
33: ; preds = %30
%34 = load i8, i8* %5
%35 = icmp eq i8 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %33
%37 = load i8, i8* %3
store i8 %37, i8* %1
%38 = load i8, i8* %4
store i8 %38, i8* %15
store i8 %34, i8* %16
%39 = load i32, i32* @SIGUSR1
%40 = call i32 @kill(i32 %6, i32 %39)
br label %41
41: ; preds = %17, %33, %36, %20
%42 = call i32 @read_bytes(i32 %0, i8* %3, i32 1)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %17
44: ; preds = %30, %27, %41, %10
%45 = phi i32 [ %12, %10 ], [ %42, %41 ], [ %28, %27 ], [ %31, %30 ]
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %45, i8* %47)
%49 = call i32 @exit(i32 0)
unreachable
}