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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ strncmp(char const *, char *, int /*<<< orphan*/);
char *strtok(char *, char const *);
int /*<<< orphan*/ strtol(char *, int /*<<< orphan*/ *, int);
uint32_t get_gateway(const char *if_name) {
  uint32_t ret;
  char buf[1024];
  const char seps[] = " \t";
  FILE *f = fopen("/proc/net/route", "r");
  do {
    fgets(buf, 1024, f);
  } while (strncmp(if_name, buf, strlen(if_name)));
  char *p = strtok(buf, seps);
  p = strtok(NULL, seps);
  p = strtok(NULL, seps);
  p[8] = '\0';
  ret = strtol(p, NULL, 16);
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<get_gateway>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xe7a(%rip),%ax
mov    %ax,-0x413(%rbp)
mov    0xe6f(%rip),%al
mov    %al,-0x411(%rbp)
lea    0xe63(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x438(%rbp),%rdi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11b3 <get_gateway+0x43>
lea    -0x410(%rbp),%rdi
lea    -0x413(%rbp),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x428(%rbp)
lea    -0x413(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1090 <strtok@plt>
mov    %rax,-0x428(%rbp)
lea    -0x413(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1090 <strtok@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rax
movb   $0x0,0x8(%rax)
mov    -0x428(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,-0xc(%rbp)
mov    -0x420(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_gateway>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%rbx
movb   $0x0,0xe(%rsp)
movw   $0x920,0xc(%rsp)
lea    0xe71(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b0 <get_gateway+0x40>
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rbx
mov    %rbx,%rsi
call   1090 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <strtok@plt>
movb   $0x0,0x8(%rax)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.get_gateway.seps = [3 x i8] c" \09\00"
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_gateway(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [3 x i8]
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %2
%8 = bitcast [3 x i8]* %5 to i8*
%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** %6
br label %10
10: ; preds = %14, %1
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = load i32*, i32** %6
%13 = call i32 @fgets(i8* %11, i32 1024, i32* %12)
br label %14
14: ; preds = %10
%15 = load i8*, i8** %2
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
%19 = call i64 @strncmp(i8* %15, i8* %16, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %10, label %21
21: ; preds = %14
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%24 = call i8* @strtok(i8* %22, i8* %23)
store i8* %24, i8** %7
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%26 = call i8* @strtok(i8* null, i8* %25)
store i8* %26, i8** %7
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%28 = call i8* @strtok(i8* null, i8* %27)
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 8
store i8 0, i8* %30
%31 = load i8*, i8** %7
%32 = call i32 @strtol(i8* %31, i32* null, i32 16)
store i32 %32, i32* %3
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@__const.get_gateway.seps = [3 x i8] c" \09\00"
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_gateway(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%6 = 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))
br label %7
7: ; preds = %7, %1
%8 = call i32 @fgets(i8* %4, i32 1024, i32* %6)
%9 = call i32 @strlen(i8* %0)
%10 = call i64 @strncmp(i8* %0, i8* %4, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %7
12: ; preds = %7
%13 = call i8* @strtok(i8* %4, i8* %5)
%14 = call i8* @strtok(i8* null, i8* %5)
%15 = call i8* @strtok(i8* null, i8* %5)
%16 = getelementptr inbounds i8, i8* %15, i64 8
store i8 0, i8* %16
%17 = call i32 @strtol(i8* nocapture %15, i32* null, i32 16)
%18 = call i32 @fclose(i32* %6)
ret i32 %17
}