#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*/ O_RDWR;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ isprint(unsigned char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stdout;
int main() {
  unsigned char c;
  int midifd;
  midifd = open("/dev/midi", O_RDWR);
  if (midifd < 0) {
    printf("Failed to open MIDI device.\n");
    return 0;
  }
  while (1) {
    if (read(midifd, &c, 1) == 1) {
      if (isprint(c))
        printf("%c", c);
      else if (c == '\n')
        printf("\n");
      else
        printf("\\x%02.2x", c);
      fflush(stdout);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1197 <main+0x47>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret
jmp    119c <main+0x4c>
mov    -0xc(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1225 <main+0xd5>
movzbl -0x5(%rbp),%edi
call   1060 <isprint@plt>
cmp    $0x0,%rax
je     11e0 <main+0x90>
movzbl -0x5(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1217 <main+0xc7>
movzbl -0x5(%rbp),%eax
cmp    $0xa,%eax
jne    1200 <main+0xb0>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1212 <main+0xc2>
movzbl -0x5(%rbp),%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1217 <main+0xc7>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    119c <main+0x4c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1200 <main+0x90>
mov    %eax,%ebx
lea    0xf(%rsp),%r14
lea    0xe67(%rip),%r15
mov    0x2e2e(%rip),%r12
jmp    11c0 <main+0x50>
nopl   0x0(%rax)
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    (%r12),%edi
call   1070 <fflush@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    11c0 <main+0x50>
movzbl 0xf(%rsp),%ebp
mov    %ebp,%edi
call   1080 <isprint@plt>
test   %rax,%rax
jne    11b0 <main+0x40>
cmp    $0xa,%bpl
jne    11f2 <main+0x82>
mov    $0xa,%edi
jmp    11b2 <main+0x42>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11b7 <main+0x47>
lea    0xe0c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/midi\00"
@O_RDWR = global i32 0
@.str.1 = [29 x i8] c"Failed to open MIDI device.\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [9 x i8] c"\\x%02.2x\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
10: ; preds = %0
br label %11
11: ; preds = %10, %37
%12 = load i32, i32* %3
%13 = call i32 @read(i32 %12, i8* %2, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %37
15: ; preds = %11
%16 = load i8, i8* %2
%17 = call i64 @isprint(i8 zeroext %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i8, i8* %2
%21 = zext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %34
23: ; preds = %15
%24 = load i8, i8* %2
%25 = zext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %33
29: ; preds = %23
%30 = load i8, i8* %2
%31 = zext i8 %30 to i32
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %29, %27
br label %34
34: ; preds = %33, %19
%35 = load i32, i32* @stdout
%36 = call i32 @fflush(i32 %35)
br label %37
37: ; preds = %34, %11
br label %11
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/midi\00"
@O_RDWR = global i32 0
@.str.4 = [9 x i8] c"\\x%02.2x\00"
@stdout = global i32 0
@str = [28 x i8] c"Failed to open MIDI device.\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
ret i32 0
7: ; preds = %0, %27
%8 = call i32 @read(i32 %3, i8* %1, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %27
10: ; preds = %7
%11 = load i8, i8* %1
%12 = tail call i64 @isprint(i8 zeroext %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = zext i8 %11 to i32
%16 = tail call i32 @putchar(i32 %15)
br label %24
17: ; preds = %10
%18 = icmp eq i8 %11, 10
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 @putchar(i32 10)
br label %24
21: ; preds = %17
%22 = zext i8 %11 to i32
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %19, %21, %14
%25 = load i32, i32* @stdout
%26 = tail call i32 @fflush(i32 %25)
br label %27
27: ; preds = %24, %7
br label %7
}