#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 O_NONBLOCK;
int O_RDONLY;
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  char line[256];
  int n, inf;
  if (argc != 2) {
    printf("Syntax:  nbread <devicename>\n");
    return 1;
  }
  inf = open(argv[1], O_RDONLY | O_NONBLOCK);
  if (inf < 0) {
    printf("Error:  Unable to open %s for reading\n", argv[1]);
    return 1;
  }
  while (1) {
    n = read(inf, line, 256);
    if (n < 0) {
      printf("Error:  Unable to read %s\n", argv[1]);
      return 1;
    }
    if (n == 0)
      return 0;
    line[n] = '\0';
    printf("%s", line);
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     116d <main+0x3d>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1254 <main+0x124>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jge    11c1 <main+0x91>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1254 <main+0x124>
jmp    11c6 <main+0x96>
mov    -0x118(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1040 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1212 <main+0xe2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1254 <main+0x124>
cmpl   $0x0,-0x114(%rbp)
jne    122b <main+0xfb>
movl   $0x0,-0x4(%rbp)
jmp    1254 <main+0x124>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c6 <main+0x96>
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
cmp    $0x2,%edi
jne    11d7 <main+0x97>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e7a(%rip),%rax
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11e5 <main+0xa5>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11ca <main+0x8a>
lea    0xeac(%rip),%r15
xor    %r12d,%r12d
mov    %rsp,%rbx
nopl   0x0(%rax)
test   %eax,%eax
je     11fd <main+0xbd>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11a0 <main+0x60>
mov    0x8(%r14),%rsi
lea    0xe52(%rip),%rdi
jmp    11f0 <main+0xb0>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
jmp    11f7 <main+0xb7>
mov    0x8(%r14),%rsi
lea    0xe10(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Syntax: nbread <devicename>\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [39 x i8] c"Error: Unable to open %s for reading\0A\00"
@.str.2 = [27 x i8] c"Error: Unable to read %s\0A\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %50
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %13
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 1, i32* %3
br label %50
28: ; preds = %13
br label %29
29: ; preds = %28, %44
%30 = load i32, i32* %8
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 256)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 1, i32* %3
br label %50
40: ; preds = %29
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 0, i32* %3
br label %50
44: ; preds = %40
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %46
store i8 0, i8* %47
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %29
50: ; preds = %43, %35, %23, %11
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [39 x i8] c"Error: Unable to open %s for reading\0A\00"
@.str.2 = [27 x i8] c"Error: Unable to read %s\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@str = [29 x i8] c"Syntax: nbread <devicename>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %34
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %10, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %8
%17 = call i32 @read(i32 %14, i8* %4, i32 256)
%18 = icmp slt i32 %17, 0
br i1 %18, label %22, label %25
19: ; preds = %8
%20 = load i8*, i8** %9
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %34
22: ; preds = %28, %16
%23 = load i8*, i8** %9
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %34
25: ; preds = %16, %28
%26 = phi i32 [ %32, %28 ], [ %17, %16 ]
%27 = icmp eq i32 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %25
%29 = zext i32 %26 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%32 = call i32 @read(i32 %14, i8* %4, i32 256)
%33 = icmp slt i32 %32, 0
br i1 %33, label %22, label %25
34: ; preds = %25, %22, %19, %6
%35 = phi i32 [ 1, %6 ], [ 1, %19 ], [ 1, %22 ], [ 0, %25 ]
ret i32 %35
}