#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int sscanf(char *, char *, char *, unsigned int *, unsigned int *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, int /*<<< orphan*/);
int get_network_stat(const char *if_name, unsigned int *bytes_rx,
                     unsigned int *bytes_tx) {
  FILE *fp;
  char interface[16];
  char *buffer = NULL;
  size_t len = 0;
  int found = 0;
  unsigned int p_bytes_rx, p_bytes_tx;
  fp = fopen("/proc/net/dev", "r");
  if (fp == NULL) {
    fprintf(stdout, "failed to read network interface\n");
    return -1;
  }
  while (getline(&buffer, &len, fp) != -1) {
    int ret = sscanf(buffer,
                     "%s %u"
                     "%*d %*d %*d %*d"
                     "%*d %*d %*d %u"
                     "%*d %*d %*d %*d"
                     "%*d %*d %*d %*d",
                     interface, &p_bytes_rx, &p_bytes_tx);
    if (ret >= 3 && !strncmp(interface, if_name, strlen(if_name))) {
      *bytes_tx = p_bytes_tx;
      *bytes_rx = p_bytes_rx;
      found = 1;
      break;
    }
  }
  free(buffer);
  fclose(fp);
  if (found)
    return 0;
  else
    return -1;
}


x86 assembly (clang, -O0):
<get_network_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
lea    0xe4e(%rip),%rdi
lea    0xe55(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ee <get_network_stat+0x6e>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <get_network_stat+0x139>
jmp    11f3 <get_network_stat+0x73>
mov    -0x28(%rbp),%rdx
lea    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     128a <get_network_stat+0x10a>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xe16(%rip),%rsi
lea    -0x58(%rbp),%rcx
lea    -0x5c(%rbp),%r8
call   1080 <sscanf@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x3,-0x60(%rbp)
jl     1285 <get_network_stat+0x105>
lea    -0x40(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    1285 <get_network_stat+0x105>
mov    -0x5c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x54(%rbp)
jmp    128a <get_network_stat+0x10a>
jmp    11f3 <get_network_stat+0x73>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x54(%rbp)
je     12b2 <get_network_stat+0x132>
movl   $0x0,-0x4(%rbp)
jmp    12b9 <get_network_stat+0x139>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_network_stat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
movq   $0x0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xe50(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1277 <get_network_stat+0xf7>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    %rax,%rdx
call   10a0 <getline@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1298 <get_network_stat+0x118>
mov    %r14,0x20(%rsp)
mov    %r15,0x28(%rsp)
lea    0x30(%rsp),%r14
lea    0x10(%rsp),%r13
lea    0x8(%rsp),%rbp
lea    0x18(%rsp),%r15
jmp    1223 <get_network_stat+0xa3>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     1293 <get_network_stat+0x113>
mov    0x8(%rsp),%rdi
lea    0xe03(%rip),%rsi
mov    %r14,%rdx
lea    0x14(%rsp),%rcx
mov    %r13,%r8
call   1080 <sscanf@plt>
cmp    $0x3,%eax
jl     1210 <get_network_stat+0x90>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    1210 <get_network_stat+0x90>
mov    0x10(%rsp),%eax
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx)
mov    0x14(%rsp),%eax
mov    0x28(%rsp),%rcx
mov    %eax,(%rcx)
xor    %ebp,%ebp
jmp    1298 <get_network_stat+0x118>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xd89(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12aa <get_network_stat+0x12a>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/net/dev\00"
@.str.1 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.2 = [34 x i8] c"failed to read network interface\0A\00"
@.str.3 = [65 x i8] c"%s %u%*d %*d %*d %*d%*d %*d %*d %u%*d %*d %*d %*d%*d %*d %*d %*d\00"
define i32 @get_network_stat(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [16 x i8]
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i8* null, i8** %10
store i64 0, i64* %11
store i32 0, i32* %12
%16 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32, i32* @stdout
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %55
22: ; preds = %3
br label %23
23: ; preds = %45, %22
%24 = load i32*, i32** %8
%25 = call i32 @getline(i8** %10, i64* %11, i32* %24)
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %46
27: ; preds = %23
%28 = load i8*, i8** %10
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%30 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %29, i32* %13, i32* %14)
store i32 %30, i32* %15
%31 = load i32, i32* %15
%32 = icmp sge i32 %31, 3
br i1 %32, label %33, label %45
33: ; preds = %27
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%35 = load i8*, i8** %5
%36 = load i8*, i8** %5
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @strncmp(i8* %34, i8* %35, i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %33
%41 = load i32, i32* %14
%42 = load i32*, i32** %7
store i32 %41, i32* %42
%43 = load i32, i32* %13
%44 = load i32*, i32** %6
store i32 %43, i32* %44
store i32 1, i32* %12
br label %46
45: ; preds = %33, %27
br label %23
46: ; preds = %40, %23
%47 = load i8*, i8** %10
%48 = call i32 @free(i8* %47)
%49 = load i32*, i32** %8
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %12
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %46
store i32 0, i32* %4
br label %55
54: ; preds = %46
store i32 -1, i32* %4
br label %55
55: ; preds = %54, %53, %19
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/net/dev\00"
@.str.1 = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.2 = [34 x i8] c"failed to read network interface\0A\00"
@.str.3 = [65 x i8] c"%s %u%*d %*d %*d %*d%*d %*d %*d %u%*d %*d %*d %*d%*d %*d %*d %*d\00"
define i32 @get_network_stat(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [16 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%10 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%11 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = call i32 @getline(i8** %5, i64* %6, i32* %14)
%18 = icmp eq i32 %17, -1
br i1 %18, label %36, label %22
19: ; preds = %3
%20 = load i32, i32* @stdout
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %41
22: ; preds = %16, %33
%23 = load i8*, i8** %5
%24 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i8* %9, i32* %7, i32* %8)
%25 = icmp sgt i32 %24, 2
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = call i32 @strlen(i8* %0)
%28 = call i32 @strncmp(i8* %9, i8* %0, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %8
store i32 %31, i32* %2
%32 = load i32, i32* %7
store i32 %32, i32* %1
br label %36
33: ; preds = %22, %26
%34 = call i32 @getline(i8** %5, i64* %6, i32* %14)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %22
36: ; preds = %33, %16, %30
%37 = phi i32 [ 0, %30 ], [ -1, %16 ], [ -1, %33 ]
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
%40 = call i32 @fclose(i32* %14)
br label %41
41: ; preds = %36, %19
%42 = phi i32 [ -1, %19 ], [ %37, %36 ]
ret i32 %42
}