#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__ ssize_t;

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char *, int);
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ write(int /*<<< orphan*/, char *, scalar_t__);
int ktest_read(int dev_no, char *dev[], int nonblock) {
  int fd, flags, retval = 0;
  char *buf;
  size_t size;
  ssize_t count;
  if (dev_no != 1)
    fprintf(stderr, "Read only from %s\n", dev[0]);
  flags = nonblock ? (O_RDONLY | O_NONBLOCK) : (O_RDONLY);
  fd = open(dev[0], flags);
  if (fd < 0) {
    fprintf(stderr, "Can't open %s\n", dev[0]);
    retval = -1;
    goto err1;
  }
  size = (4 * 1024);
  buf = malloc(size);
  if (buf == NULL) {
    fprintf(stderr, "Can't get memory\n");
    retval = -1;
    goto err2;
  }
  while (1) {
    count = read(fd, buf, size);
    if (count <= 0)
      break;
    count = write(fileno(stdout), buf, count);
  }
  free(buf);
  close(fd);
  return retval;
err2:
  close(fd);
err1:
  return retval;
}


x86 assembly (clang, -O0):
<ktest_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     11c1 <ktest_read+0x41>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
je     11e5 <ktest_read+0x65>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
jmp    11f1 <ktest_read+0x71>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    123d <ktest_read+0xbd>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    12f3 <ktest_read+0x173>
movq   $0x1000,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1280 <ktest_read+0x100>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x20(%rbp)
jmp    12eb <ktest_read+0x16b>
jmp    1285 <ktest_read+0x105>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jg     12a9 <ktest_read+0x129>
jmp    12cf <ktest_read+0x14f>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x38(%rbp)
jmp    1285 <ktest_read+0x105>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f9 <ktest_read+0x179>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ktest_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
cmp    $0x1,%edi
je     11ab <ktest_read+0x2b>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe5c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2e2e(%rip),%rax
test   %ebp,%ebp
je     11bf <ktest_read+0x3f>
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    (%rbx),%rdi
mov    %ebp,%esi
call   10a0 <open@plt>
test   %eax,%eax
js     1251 <ktest_read+0xd1>
mov    %eax,%r14d
mov    $0x1000,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     126d <ktest_read+0xed>
mov    %rax,%rbx
mov    $0x1000,%edx
mov    %r14d,%edi
mov    %rax,%rsi
call   1060 <read@plt>
test   %rax,%rax
jle    123d <ktest_read+0xbd>
mov    %rax,%rbp
mov    0x2dbd(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <write@plt>
mov    $0x1000,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jg     1210 <ktest_read+0x90>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1291 <ktest_read+0x111>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdaf(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128c <ktest_read+0x10c>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Read only from %s\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [15 x i8] c"Can't open %s\0A\00"
@.str.2 = [18 x i8] c"Can't get memory\0A\00"
@stdout = global i32 0
define i32 @ktest_read(i32 %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 0, i32* %10
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %6
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %16, %3
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @O_RDONLY
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %26, %27
br label %31
29: ; preds = %22
%30 = load i32, i32* @O_RDONLY
br label %31
31: ; preds = %29, %25
%32 = phi i32 [ %28, %25 ], [ %30, %29 ]
store i32 %32, i32* %9
%33 = load i8**, i8*** %6
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = load i32, i32* %9
%37 = call i32 @open(i8* %35, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = load i8**, i8*** %6
%43 = getelementptr inbounds i8*, i8** %42, i64 0
%44 = load i8*, i8** %43
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %44)
store i32 -1, i32* %10
br label %78
46: ; preds = %31
store i64 4096, i64* %12
%47 = load i64, i64* %12
%48 = call i8* @malloc(i64 %47)
store i8* %48, i8** %11
%49 = load i8*, i8** %11
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %10
br label %75
54: ; preds = %46
br label %55
55: ; preds = %54, %63
%56 = load i32, i32* %8
%57 = load i8*, i8** %11
%58 = load i64, i64* %12
%59 = call i64 @read(i32 %56, i8* %57, i64 %58)
store i64 %59, i64* %13
%60 = load i64, i64* %13
%61 = icmp sle i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %55
br label %69
63: ; preds = %55
%64 = load i32, i32* @stdout
%65 = call i32 @fileno(i32 %64)
%66 = load i8*, i8** %11
%67 = load i64, i64* %13
%68 = call i64 @write(i32 %65, i8* %66, i64 %67)
store i64 %68, i64* %13
br label %55
69: ; preds = %62
%70 = load i8*, i8** %11
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* %8
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* %10
store i32 %74, i32* %4
br label %80
75: ; preds = %51
%76 = load i32, i32* %8
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %40
%79 = load i32, i32* %10
store i32 %79, i32* %4
br label %80
80: ; preds = %78, %69
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Read only from %s\0A\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [15 x i8] c"Can't open %s\0A\00"
@.str.2 = [18 x i8] c"Can't get memory\0A\00"
@stdout = global i32 0
define i32 @ktest_read(i32 %0, i8** nocapture %1, i32 %2) {
%4 = icmp eq i32 %0, 1
br i1 %4, label %9, label %5
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %9
9: ; preds = %5, %3
%10 = icmp eq i32 %2, 0
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_NONBLOCK
%13 = select i1 %10, i32 0, i32 %12
%14 = or i32 %13, %11
%15 = load i8*, i8** %1
%16 = tail call i32 @open(i8* %15, i32 %14)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %9
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %1
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %42
22: ; preds = %9
%23 = tail call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
%24 = icmp eq i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i64 @read(i32 %16, i8* %23, i64 4096)
%27 = icmp slt i64 %26, 1
br i1 %27, label %39, label %32
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @close(i32 %16)
br label %42
32: ; preds = %25, %32
%33 = phi i64 [ %37, %32 ], [ %26, %25 ]
%34 = load i32, i32* @stdout
%35 = tail call i32 @fileno(i32 %34)
%36 = tail call i64 @write(i32 %35, i8* %23, i64 %33)
%37 = tail call i64 @read(i32 %16, i8* %23, i64 4096)
%38 = icmp slt i64 %37, 1
br i1 %38, label %39, label %32
39: ; preds = %32, %25
%40 = tail call i32 @free(i8* %23)
%41 = tail call i32 @close(i32 %16)
br label %42
42: ; preds = %18, %28, %39
%43 = phi i32 [ 0, %39 ], [ -1, %28 ], [ -1, %18 ]
ret i32 %43
}