#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*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, char *, unsigned int *);
void get_default(char *if_name, int length) {
  if (length <= 999) {
    FILE *routes = fopen("/proc/net/route", "r");
    if (routes) {
      char line[256];
      fgets(line, 256, routes);
      fgets(line, 256, routes);
      while (!feof(routes)) {
        unsigned int address;
        char query[9];
        sprintf(query, "%%%ds %%x", length);
        sscanf(line, query, if_name, &address);
        if (!address)
          break;
        fgets(line, 256, routes);
      }
      if (feof(routes))
        if_name[0] = 0;
      fclose(routes);
    }
  } else
    if_name[0] = 0;
}


x86 assembly (clang, -O0):
<get_default>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x3e7,-0xc(%rbp)
jg     1272 <get_default+0x112>
lea    0xe7a(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     126d <get_default+0x10d>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ea <get_default+0x8a>
jmp    124a <get_default+0xea>
lea    -0x12d(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe17(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    -0x12d(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    -0x124(%rbp),%rcx
call   1060 <sscanf@plt>
cmpl   $0x0,-0x124(%rbp)
jne    1230 <get_default+0xd0>
jmp    124a <get_default+0xea>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
jmp    11cb <get_default+0x6b>
mov    -0x18(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1264 <get_default+0x104>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1279 <get_default+0x119>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_default>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
cmp    $0x3e7,%esi
jg     1240 <get_default+0xe0>
mov    %esi,%ebp
lea    0xe77(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1244 <get_default+0xe4>
mov    %rax,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %r15,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    1225 <get_default+0xc5>
lea    0x7(%rsp),%r12
lea    0x10(%rsp),%r13
mov    %rsp,%r15
mov    %r12,%rdi
lea    0xe28(%rip),%rsi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1060 <sscanf@plt>
cmpl   $0x0,(%rsp)
je     1225 <get_default+0xc5>
mov    %r13,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     11e0 <get_default+0x80>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     1236 <get_default+0xd6>
movb   $0x0,(%r14)
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1244 <get_default+0xe4>
movb   $0x0,(%r14)
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"%%%ds %%x\00"
define void @get_default(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca [9 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 999
br i1 %10, label %11, label %53
11: ; preds = %2
%12 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %52
15: ; preds = %11
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%17 = load i32*, i32** %5
%18 = call i32 @fgets(i8* %16, i32 256, i32* %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = load i32*, i32** %5
%21 = call i32 @fgets(i8* %19, i32 256, i32* %20)
br label %22
22: ; preds = %38, %15
%23 = load i32*, i32** %5
%24 = call i64 @feof(i32* %23)
%25 = icmp ne i64 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %42
27: ; preds = %22
%28 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 0
%29 = load i32, i32* %4
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%32 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 0
%33 = load i8*, i8** %3
%34 = call i32 @sscanf(i8* %31, i8* %32, i8* %33, i32* %7)
%35 = load i32, i32* %7
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %27
br label %42
38: ; preds = %27
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%40 = load i32*, i32** %5
%41 = call i32 @fgets(i8* %39, i32 256, i32* %40)
br label %22
42: ; preds = %37, %22
%43 = load i32*, i32** %5
%44 = call i64 @feof(i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i8*, i8** %3
%48 = getelementptr inbounds i8, i8* %47, i64 0
store i8 0, i8* %48
br label %49
49: ; preds = %46, %42
%50 = load i32*, i32** %5
%51 = call i32 @fclose(i32* %50)
br label %52
52: ; preds = %49, %11
br label %56
53: ; preds = %2
%54 = load i8*, i8** %3
%55 = getelementptr inbounds i8, i8* %54, i64 0
store i8 0, i8* %55
br label %56
56: ; preds = %53, %52
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"%%%ds %%x\00"
define void @get_default(i8* %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca [9 x i8]
%6 = icmp slt i32 %1, 1000
br i1 %6, label %7, label %35
7: ; preds = %2
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %36, label %10
10: ; preds = %7
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = call i32 @fgets(i8* %11, i32 256, i32* %8)
%13 = call i32 @fgets(i8* %11, i32 256, i32* %8)
%14 = call i64 @feof(i32* %8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %10
%17 = bitcast i32* %4 to i8*
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
br label %19
19: ; preds = %16, %25
%20 = call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%21 = call i32 @sscanf(i8* %11, i8* %18, i8* %0, i32* %4)
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %29
25: ; preds = %19
%26 = call i32 @fgets(i8* %11, i32 256, i32* %8)
%27 = call i64 @feof(i32* %8)
%28 = icmp eq i64 %27, 0
br i1 %28, label %19, label %29
29: ; preds = %25, %10, %24
%30 = call i64 @feof(i32* %8)
%31 = icmp eq i64 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %29
store i8 0, i8* %0
br label %33
33: ; preds = %32, %29
%34 = call i32 @fclose(i32* %8)
br label %36
35: ; preds = %2
store i8 0, i8* %0
br label %36
36: ; preds = %7, %33, %35
ret void
}