#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_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, unsigned int);
void read_dev(const char *dev, char *buf, unsigned int len) {
  int fd;
  int rl;
  buf[0] = '?';
  buf[1] = 0;
  fd = open(dev, O_RDONLY);
  if (fd == -1)
    return;
  rl = read(fd, buf, len - 1);
  if (rl <= 0) {
    close(fd);
    return;
  }
  buf[rl] = 0;
  close(fd);
}


x86 assembly (clang, -O0):
<read_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8(%rbp),%rdi
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1176 <read_dev+0x46>
jmp    11b6 <read_dev+0x86>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11a2 <read_dev+0x72>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
jmp    11b6 <read_dev+0x86>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_dev>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
movw   $0x3f,(%rsi)
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1178 <read_dev+0x48>
mov    %eax,%ebp
add    $0xffffffff,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    116d <read_dev+0x3d>
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <close@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define void @read_dev(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8 63, i8* %10
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i64 1
store i8 0, i8* %12
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %3
br label %37
19: ; preds = %3
%20 = load i32, i32* %7
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = sub i32 %22, 1
%24 = call i32 @read(i32 %20, i8* %21, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
br label %37
30: ; preds = %19
%31 = load i8*, i8** %5
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %30, %27, %18
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define void @read_dev(i8* nocapture %0, i8* nocapture %1, i32 %2) {
store i8 63, i8* %1
%4 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %4
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %17, label %8
8: ; preds = %3
%9 = add i32 %2, -1
%10 = tail call i32 @read(i32 %6, i8* %1, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = zext i32 %10 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %8, %12
%16 = tail call i32 @close(i32 %6)
br label %17
17: ; preds = %15, %3
ret void
}