#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
scalar_t__ write(int, char *, int);
int main(void) {
  int dragondev;
  char buf[11];
  dragondev = open("/dev/dragondev", O_RDWR);
  if (dragondev == -1) {
    perror("Test failed.\n");
    exit(0);
  }
  for (int i = 1; i <= 127; i++) {
    buf[0] = i;
    printf("write_size = %d\t", (int)write(dragondev, buf, 1));
  }
  close(dragondev);
  printf("\n\n");
  dragondev = open("/dev/dragondev", O_RDWR);
  if (dragondev == -1) {
    perror("Test failed.\n");
    exit(0);
  }
  for (int i = 1; i <= 12; i++) {
    printf("read_size = %d, ", (int)read(dragondev, buf, 10));
    printf("%s\n", buf);
  }
  close(dragondev);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b4 <main+0x44>
lea    0xe67(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x1,-0x18(%rbp)
cmpl   $0x7f,-0x18(%rbp)
jg     11fa <main+0x8a>
mov    -0x18(%rbp),%eax
mov    %al,-0x13(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x13(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,%esi
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bb <main+0x4b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xde0(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1245 <main+0xd5>
lea    0xdd6(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x1,-0x1c(%rbp)
cmpl   $0xc,-0x1c(%rbp)
jg     1297 <main+0x127>
mov    -0x8(%rbp),%edi
lea    -0x13(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,%esi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x13(%rbp),%rsi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    124c <main+0xdc>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e55(%rip),%r12
mov    (%r12),%esi
lea    0xe62(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     13da <main+0x25a>
mov    %eax,%ebp
mov    $0xffffff81,%ebx
lea    0x5(%rsp),%r14
lea    0xe5e(%rip),%r15
nop
lea    -0x80(%rbx),%eax
mov    %al,0x5(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
inc    %ebx
jne    11c0 <main+0x40>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
mov    (%r12),%esi
lea    0xdfc(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     13da <main+0x25a>
mov    %eax,%ebx
lea    0x5(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
lea    0xdff(%rip),%r15
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc2e(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/dragondev\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"Test failed.\0A\00"
@.str.2 = [17 x i8] c"write_size = %d\09\00"
@.str.3 = [3 x i8] c"\0A\0A\00"
@.str.4 = [17 x i8] c"read_size = %d, \00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [11 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %0
store i32 1, i32* %4
br label %14
14: ; preds = %26, %13
%15 = load i32, i32* %4
%16 = icmp sle i32 %15, 127
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = load i32, i32* %4
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
store i8 %19, i8* %20
%21 = load i32, i32* %2
%22 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%23 = call i64 @write(i32 %21, i8* %22, i32 1)
%24 = trunc i64 %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %17
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %14
29: ; preds = %14
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @O_RDWR
%34 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 %34, i32* %2
%35 = load i32, i32* %2
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %29
store i32 1, i32* %5
br label %41
41: ; preds = %52, %40
%42 = load i32, i32* %5
%43 = icmp sle i32 %42, 12
br i1 %43, label %44, label %55
44: ; preds = %41
%45 = load i32, i32* %2
%46 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%47 = call i64 @read(i32 %45, i8* %46, i32 10)
%48 = trunc i64 %47 to i32
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %44
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %41
55: ; preds = %41
%56 = load i32, i32* %2
%57 = call i32 @close(i32 %56)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/dragondev\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"Test failed.\0A\00"
@.str.2 = [17 x i8] c"write_size = %d\09\00"
@.str.4 = [17 x i8] c"read_size = %d, \00"
@str = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %65
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %65
%10 = tail call i32 @close(i32 %4)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %73, label %15
15: ; preds = %9
%16 = call i64 @read(i32 %13, i8* %2, i32 10)
%17 = trunc i64 %16 to i32
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = call i32 @puts(i8* %2)
%20 = call i64 @read(i32 %13, i8* %2, i32 10)
%21 = trunc i64 %20 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = call i32 @puts(i8* %2)
%24 = call i64 @read(i32 %13, i8* %2, i32 10)
%25 = trunc i64 %24 to i32
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = call i32 @puts(i8* %2)
%28 = call i64 @read(i32 %13, i8* %2, i32 10)
%29 = trunc i64 %28 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = call i32 @puts(i8* %2)
%32 = call i64 @read(i32 %13, i8* %2, i32 10)
%33 = trunc i64 %32 to i32
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = call i32 @puts(i8* %2)
%36 = call i64 @read(i32 %13, i8* %2, i32 10)
%37 = trunc i64 %36 to i32
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = call i32 @puts(i8* %2)
%40 = call i64 @read(i32 %13, i8* %2, i32 10)
%41 = trunc i64 %40 to i32
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = call i32 @puts(i8* %2)
%44 = call i64 @read(i32 %13, i8* %2, i32 10)
%45 = trunc i64 %44 to i32
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = call i32 @puts(i8* %2)
%48 = call i64 @read(i32 %13, i8* %2, i32 10)
%49 = trunc i64 %48 to i32
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = call i32 @puts(i8* %2)
%52 = call i64 @read(i32 %13, i8* %2, i32 10)
%53 = trunc i64 %52 to i32
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = call i32 @puts(i8* %2)
%56 = call i64 @read(i32 %13, i8* %2, i32 10)
%57 = trunc i64 %56 to i32
%58 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%59 = call i32 @puts(i8* %2)
%60 = call i64 @read(i32 %13, i8* %2, i32 10)
%61 = trunc i64 %60 to i32
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %61)
%63 = call i32 @puts(i8* %2)
%64 = tail call i32 @close(i32 %13)
ret i32 0
65: ; preds = %0, %65
%66 = phi i32 [ %71, %65 ], [ 1, %0 ]
%67 = trunc i32 %66 to i8
store i8 %67, i8* %2
%68 = call i64 @write(i32 %4, i8* %2, i32 1)
%69 = trunc i64 %68 to i32
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %69)
%71 = add nuw nsw i32 %66, 1
%72 = icmp eq i32 %71, 128
br i1 %72, label %9, label %65
73: ; preds = %9
%74 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%75 = tail call i32 @exit(i32 0)
unreachable
}