#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*/ hexprint(unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int tunio_open(char *);
int tunio_read(int, unsigned char *, int);
int tunio_test() {
  unsigned char buf[65536];
  int buflen;
  char devname[10];
  strcpy(devname, "cluetest");
  int fd = tunio_open(devname);
  if (fd == -1) {
    printf("Error.\n");
    return 1;
  }
  printf("Opened device %s\n", devname);
  for (;;) {
    printf("Reading packet ...\n");
    buflen = tunio_read(fd, buf, 65536);
    if (buflen < 4) {
      printf("Huh?  Got a length %d\n", buflen);
      continue;
    }
    printf("Packet length %d:\n", buflen);
    hexprint(buf, buflen);
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<tunio_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
lea    -0x1000e(%rbp),%rdi
lea    0xe97(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1000e(%rbp),%rdi
call   1070 <tunio_open@plt>
mov    %eax,-0x10014(%rbp)
cmpl   $0xffffffff,-0x10014(%rbp)
jne    11a9 <tunio_test+0x59>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x10020,%rsp
pop    %rbp
ret
lea    -0x1000e(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10014(%rbp),%edi
lea    -0x10000(%rbp),%rsi
mov    $0x10000,%edx
call   1060 <tunio_read@plt>
mov    %eax,-0x10004(%rbp)
cmpl   $0x4,-0x10004(%rbp)
jge    120f <tunio_test+0xbf>
mov    -0x10004(%rbp),%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11be <tunio_test+0x6e>
mov    -0x10004(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x10000(%rbp),%rdi
mov    -0x10004(%rbp),%esi
call   1050 <hexprint@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11be <tunio_test+0x6e>

x86 assembly (clang, -O2):
<tunio_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
lea    0xe78(%rip),%rsi
lea    0x6(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1090 <tunio_open@plt>
cmp    $0xffffffff,%eax
je     122c <tunio_test+0xbc>
mov    %eax,%ebx
lea    0xe5a(%rip),%rdi
lea    0x6(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe83(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xe4d(%rip),%r12
lea    0xe5d(%rip),%r13
jmp    1200 <tunio_test+0x90>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <hexprint@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1050 <puts@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x10000,%edx
call   1080 <tunio_read@plt>
mov    %eax,%ebp
cmp    $0x3,%eax
jg     11e0 <tunio_test+0x70>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1200 <tunio_test+0x90>
lea    0xe25(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"cluetest\00"
@.str.1 = [8 x i8] c"Error.\0A\00"
@.str.2 = [18 x i8] c"Opened device %s\0A\00"
@.str.3 = [20 x i8] c"Reading packet ...\0A\00"
@.str.4 = [23 x i8] c"Huh? Got a length %d\0A\00"
@.str.5 = [19 x i8] c"Packet length %d:\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
define i32 @tunio_test() {
%1 = alloca [65536 x i8]
%2 = alloca i32
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = call i32 @tunio_open(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
13: ; preds = %0
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %26, %23, %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* %4
%19 = getelementptr inbounds [65536 x i8], [65536 x i8]* %1, i64 0, i64 0
%20 = call i32 @tunio_read(i32 %18, i8* %19, i32 65536)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = icmp slt i32 %21, 4
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %24)
br label %16
26: ; preds = %16
%27 = load i32, i32* %2
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [65536 x i8], [65536 x i8]* %1, i64 0, i64 0
%30 = load i32, i32* %2
%31 = call i32 @hexprint(i8* %29, i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %16
}

LLVM IR (-O2):
@.str = [9 x i8] c"cluetest\00"
@.str.2 = [18 x i8] c"Opened device %s\0A\00"
@.str.4 = [23 x i8] c"Huh? Got a length %d\0A\00"
@.str.5 = [19 x i8] c"Packet length %d:\0A\00"
@str = [19 x i8] c"Reading packet ...\00"
@str.7 = [7 x i8] c"Error.\00"
define i32 @tunio_test() {
%1 = alloca [65536 x i8]
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [65536 x i8], [65536 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @tunio_open(i8* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.7, i64 0, i64 0))
ret i32 1
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %12
12: ; preds = %18, %10
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%14 = call i32 @tunio_read(i32 %6, i8* %3, i32 65536)
%15 = icmp slt i32 %14, 4
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %14)
br label %18
18: ; preds = %16, %19
br label %12
19: ; preds = %12
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %14)
%21 = call i32 @hexprint(i8* %3, i32 %14)
%22 = call i32 @putchar(i32 10)
br label %18
}