#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 */
struct ifreq {
  char *ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ raise_error(char *);
int read(int, char *, int);
void read_bytes_tun(int tun_fd, struct ifreq *ifr) {
  int stat;
  char buff[3000];
  int buff_size = 3000;
  while (1) {
    stat = read(tun_fd, buff, buff_size);
    if (stat < 0)
      raise_error("read() failed");
    printf("Read %d bytes from tun device %s from: \n", stat, ifr->ifr_name);
  }
}


x86 assembly (clang, -O0):
<read_bytes_tun>:
push   %rbp
mov    %rsp,%rbp
sub    $0xbe0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xbb8,-0xbd4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xbd0(%rbp),%rsi
mov    -0xbd4(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117a <read_bytes_tun+0x4a>
lea    0xe8b(%rip),%rdi
call   1050 <raise_error@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe83(%rip),%rdi
call   1030 <printf@plt>
jmp    114c <read_bytes_tun+0x1c>

x86 assembly (clang, -O2):
<read_bytes_tun>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xbb8,%rsp
mov    %rsi,%r13
mov    %edi,%ebp
mov    %rsp,%r14
lea    0xeb0(%rip),%r15
lea    0xeb7(%rip),%r12
jmp    116e <read_bytes_tun+0x3e>
nopl   0x0(%rax)
mov    0x0(%r13),%rdx
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xbb8,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1160 <read_bytes_tun+0x30>
mov    %r15,%rdi
call   1050 <raise_error@plt>
jmp    1160 <read_bytes_tun+0x30>

LLVM IR (-O0):
%struct.ifreq = type { i8* }
@.str = [14 x i8] c"read() failed\00"
@.str.1 = [41 x i8] c"Read %d bytes from tun device %s from: \0A\00"
define void @read_bytes_tun(i32 %0, %struct.ifreq* %1) {
%3 = alloca i32
%4 = alloca %struct.ifreq*
%5 = alloca i32
%6 = alloca [3000 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store %struct.ifreq* %1, %struct.ifreq** %4
store i32 3000, i32* %7
br label %8
8: ; preds = %2, %17
%9 = load i32, i32* %3
%10 = getelementptr inbounds [3000 x i8], [3000 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* %7
%12 = call i32 @read(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = call i32 @raise_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %8
%18 = load i32, i32* %5
%19 = load %struct.ifreq*, %struct.ifreq** %4
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %18, i8* %21)
br label %8
}

LLVM IR (-O2):
%struct.ifreq = type { i8* }
@.str = [14 x i8] c"read() failed\00"
@.str.1 = [41 x i8] c"Read %d bytes from tun device %s from: \0A\00"
define void @read_bytes_tun(i32 %0, %struct.ifreq* nocapture %1) {
%3 = alloca [3000 x i8]
%4 = getelementptr inbounds [3000 x i8], [3000 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0
br label %6
6: ; preds = %11, %2
%7 = call i32 @read(i32 %0, i8* %4, i32 3000)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @raise_error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = load i8*, i8** %5
%13 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %7, i8* %12)
br label %6
}