#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int get_interface_speed(char *interface) {
  char filename[100];
  sprintf(filename, "/sys/class/net/%s/speed", interface);
  if (fopen(filename, "r") == NULL) {
    return 0;
  }
  char buffer[10];
  FILE *info = fopen(filename, "r");
  fgets(buffer, 9, info);
  fclose(info);
  return atoi(buffer);
}


x86 assembly (clang, -O0):
<get_interface_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe92(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1050 <fopen@plt>
cmp    $0x0,%rax
jne    1199 <get_interface_speed+0x49>
movl   $0x0,-0x4(%rbp)
jmp    11e3 <get_interface_speed+0x93>
lea    -0x80(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x98(%rbp)
lea    -0x8a(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x9,%esi
call   1040 <fgets@plt>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x8a(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_interface_speed>:
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rdi,%rdx
lea    0xe9f(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xea3(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11bd <get_interface_speed+0x6d>
lea    0xe8f(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0x6(%rsp),%r14
mov    %r14,%rdi
mov    $0x9,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1060 <atoi@plt>
jmp    11bf <get_interface_speed+0x6f>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/sys/class/net/%s/speed\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_interface_speed(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [100 x i8]
%5 = alloca [10 x i8]
%6 = alloca i32*
store i8* %0, i8** %3
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 0, i32* %2
br label %24
14: ; preds = %1
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%18 = load i32*, i32** %6
%19 = call i32 @fgets(i8* %17, i32 9, i32* %18)
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %2
br label %24
24: ; preds = %14, %13
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [24 x i8] c"/sys/class/net/%s/speed\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_interface_speed(i8* %0) {
%2 = alloca [100 x i8]
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%10 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @fgets(i8* %9, i32 9, i32* %10)
%12 = call i32 @fclose(i32* %10)
%13 = call i32 @atoi(i8* %9)
br label %14
14: ; preds = %1, %8
%15 = phi i32 [ %13, %8 ], [ 0, %1 ]
ret i32 %15
}