#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 scalar_t__ uint8_t;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
unsigned int read(int, scalar_t__ *, int);
int main(int argc, char **argv) {
  int scfd = open("/dev/scancode", O_RDONLY, 0);
  while (1) {
    uint8_t buf[100];
    unsigned n = read(scfd, buf, 100);
    for (unsigned i = 0; i < n; i++) {
      printf("Scancode: %x\n", (int)buf[i]);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x340(%rbp),%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,-0x344(%rbp)
movl   $0x0,-0x348(%rbp)
mov    -0x348(%rbp),%eax
cmp    -0x344(%rbp),%eax
jae    11c9 <main+0x99>
mov    -0x348(%rbp),%eax
mov    -0x340(%rbp,%rax,8),%rax
ff
mov    %eax,%esi
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x348(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x348(%rbp)
jmp    1187 <main+0x57>
jmp    1163 <main+0x33>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x320,%rsp
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,%r15d
mov    %rsp,%r14
lea    0xeab(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1170 <main+0x40>
mov    %eax,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%rsp,%rbx,8),%esi
mov    %r12,%rdi
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <main+0x60>
jmp    1170 <main+0x40>

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/scancode\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"Scancode: %x\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [100 x i64]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10, i32 0)
store i32 %11, i32* %6
br label %12
12: ; preds = %2, %30
%13 = load i32, i32* %6
%14 = getelementptr inbounds [100 x i64], [100 x i64]* %7, i64 0, i64 0
%15 = call i32 @read(i32 %13, i64* %14, i32 100)
store i32 %15, i32* %8
store i32 0, i32* %9
br label %16
16: ; preds = %27, %12
%17 = load i32, i32* %9
%18 = load i32, i32* %8
%19 = icmp ult i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i32, i32* %9
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [100 x i64], [100 x i64]* %7, i64 0, i64 %22
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %20
%28 = load i32, i32* %9
%29 = add i32 %28, 1
store i32 %29, i32* %9
br label %16
30: ; preds = %16
br label %12
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/scancode\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"Scancode: %x\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i64]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
%6 = bitcast [100 x i64]* %3 to i8*
%7 = getelementptr inbounds [100 x i64], [100 x i64]* %3, i64 0, i64 0
br label %8
8: ; preds = %13, %2
%9 = call i32 @read(i32 %5, i64* %7, i32 100)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = zext i32 %9 to i64
br label %14
13: ; preds = %14, %8
br label %8
14: ; preds = %11, %14
%15 = phi i64 [ 0, %11 ], [ %20, %14 ]
%16 = getelementptr inbounds [100 x i64], [100 x i64]* %3, i64 0, i64 %15
%17 = load i64, i64* %16
%18 = trunc i64 %17 to i32
%19 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = add nuw nsw i64 %15, 1
%21 = icmp eq i64 %20, %12
br i1 %21, label %13, label %14
}