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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
uint32_t getroute(void) {
  uint32_t gw = 0;
  FILE *froute = fopen("/proc/net/route", "r");
  if (froute) {
    char buffer[512];
    char *l = fgets(buffer, sizeof(buffer), froute);
    if (l) {
      while (l) {
        char interf[32];
        uint32_t dest;
        uint32_t gateway;
        l = fgets(buffer, sizeof(buffer), froute);
        if (l) {
          int val = sscanf(buffer, "%31s %x %x", interf, &dest, &gateway);
          if (3 == val) {
            if (!dest) {
              gw = gateway;
              break;
            }
          }
        }
      }
    }
    fclose(froute);
  }
  return gw;
}


x86 assembly (clang, -O0):
<getroute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1252 <getroute+0x112>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     1249 <getroute+0x109>
jmp    11a3 <getroute+0x63>
cmpq   $0x0,-0x218(%rbp)
je     1244 <getroute+0x104>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     123f <getroute+0xff>
lea    -0x210(%rbp),%rdi
lea    -0x240(%rbp),%rdx
lea    0xe22(%rip),%rsi
lea    -0x244(%rbp),%rcx
lea    -0x248(%rbp),%r8
call   1050 <sscanf@plt>
mov    %eax,-0x24c(%rbp)
mov    $0x3,%eax
cmp    -0x24c(%rbp),%eax
jne    123a <getroute+0xfa>
cmpl   $0x0,-0x244(%rbp)
jne    1235 <getroute+0xf5>
mov    -0x248(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1244 <getroute+0x104>
jmp    123a <getroute+0xfa>
jmp    123f <getroute+0xff>
jmp    11a3 <getroute+0x63>
jmp    1249 <getroute+0x109>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getroute>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
lea    0xea8(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e7 <getroute+0xa7>
mov    %rax,%rbx
lea    0x30(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11ed <getroute+0xad>
lea    0x30(%rsp),%rbp
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0x8(%rsp),%r14
jmp    11a5 <getroute+0x65>
nopl   0x0(%rax,%rax,1)
test   %r15,%r15
je     11eb <getroute+0xab>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11eb <getroute+0xab>
mov    %rax,%r15
mov    %rbp,%rdi
lea    0xe4b(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %r14,%r8
call   1050 <sscanf@plt>
cmp    $0x3,%eax
jne    11a0 <getroute+0x60>
cmpl   $0x0,0xc(%rsp)
jne    11a0 <getroute+0x60>
mov    0x8(%rsp),%ebp
jmp    11ed <getroute+0xad>
xor    %ebp,%ebp
jmp    11f5 <getroute+0xb5>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x238,%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 = [11 x i8] c"%31s %x %x\00"
define i32 @getroute() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [512 x i8]
%4 = alloca i8*
%5 = alloca [32 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = 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* %9, i32** %2
%10 = load i32*, i32** %2
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %46
12: ; preds = %0
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %2
%15 = call i8* @fgets(i8* %13, i32 512, i32* %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %43
18: ; preds = %12
br label %19
19: ; preds = %41, %18
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %42
22: ; preds = %19
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%24 = load i32*, i32** %2
%25 = call i8* @fgets(i8* %23, i32 512, i32* %24)
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %41
28: ; preds = %22
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%31 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32* %6, i32* %7)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp eq i32 3, %32
br i1 %33, label %34, label %40
34: ; preds = %28
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = load i32, i32* %7
store i32 %38, i32* %1
br label %42
39: ; preds = %34
br label %40
40: ; preds = %39, %28
br label %41
41: ; preds = %40, %22
br label %19
42: ; preds = %37, %19
br label %43
43: ; preds = %42, %12
%44 = load i32*, i32** %2
%45 = call i32 @fclose(i32* %44)
br label %46
46: ; preds = %43, %0
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"%31s %x %x\00"
define i32 @getroute() {
%1 = alloca [512 x i8]
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %31, label %7
7: ; preds = %0
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%9 = call i8* @fgets(i8* %8, i32 512, i32* %5)
%10 = icmp eq i8* %9, null
br i1 %10, label %28, label %11
11: ; preds = %7
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%13 = bitcast i32* %3 to i8*
%14 = bitcast i32* %4 to i8*
br label %15
15: ; preds = %11, %27
%16 = call i8* @fgets(i8* %8, i32 512, i32* %5)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %15
br label %28
19: ; preds = %15
%20 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %12, i32* %3, i32* %4)
%21 = icmp ne i32 %20, 3
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 0
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = load i32, i32* %4
br label %28
27: ; preds = %19
br i1 %17, label %28, label %15
28: ; preds = %27, %25, %18, %7
%29 = phi i32 [ 0, %7 ], [ %26, %25 ], [ 0, %18 ], [ 0, %27 ]
%30 = call i32 @fclose(i32* %5)
br label %31
31: ; preds = %28, %0
%32 = phi i32 [ %29, %28 ], [ 0, %0 ]
ret i32 %32
}