#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd_led, fd_key;
  char key_val;
  fd_led = open("/dev/m_led", O_WRONLY);
  if (-1 == fd_led) {
    perror("/dev/m_led");
    exit(EXIT_FAILURE);
  }
  fd_key = open("/dev/m_key", O_RDONLY);
  if (-1 == fd_key) {
    perror("/dev/m_key");
    close(fd_led);
    exit(EXIT_FAILURE);
  }
  do {
    read(fd_key, &key_val, 1);
    write(fd_led, &key_val, 1);
  } while (0xf7 != key_val);
  close(fd_key);
  close(fd_led);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11b6 <main+0x56>
lea    0xe5d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11fe <main+0x9e>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1203 <main+0xa3>
mov    -0x18(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movsbl -0x19(%rbp),%ecx
mov    $0xf7,%eax
cmp    %ecx,%eax
jne    1203 <main+0xa3>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d1 <main+0x71>
mov    %eax,%r14d
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11df <main+0x7f>
mov    %eax,%ebp
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11b0 <main+0x50>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
jmp    11f3 <main+0x93>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/m_led\00"
@O_WRONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"/dev/m_key\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 -1, %20
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
br label %29
29: ; preds = %34, %28
%30 = load i32, i32* %7
%31 = call i32 @read(i32 %30, i8* %8, i32 1)
%32 = load i32, i32* %6
%33 = call i32 @write(i32 %32, i8* %8, i32 1)
br label %34
34: ; preds = %29
%35 = load i8, i8* %8
%36 = sext i8 %35 to i32
%37 = icmp ne i32 247, %36
br i1 %37, label %29, label %38
38: ; preds = %34
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/m_led\00"
@O_WRONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"/dev/m_key\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @close(i32 %5)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %11, %20
%21 = call i32 @read(i32 %13, i8* %3, i32 1)
%22 = call i32 @write(i32 %5, i8* %3, i32 1)
br label %20
}