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

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ net_search_gateway(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
in_addr_t net_get_gateway(void) {
  in_addr_t gate_addr;
  char buff[132];
  FILE *fp = fopen("/proc/net/route", "r");
  if (!fp) {
    perror("/proc/net/route");
    printf("INET (IPv4) not configured in this system.\n");
    return (INADDR_ANY);
  }
  fgets(buff, 130, fp);
  while (fgets(buff, 130, fp) != NULL) {
    if (net_search_gateway(buff, &gate_addr) == 0) {
      fclose(fp);
      return gate_addr;
    }
  }
  fclose(fp);
  return (INADDR_ANY);
}


x86 assembly (clang, -O0):
<net_get_gateway>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    0xe8e(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11bc <net_get_gateway+0x5c>
lea    0xe66(%rip),%rdi
call   1080 <perror@plt>
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1244 <net_get_gateway+0xe4>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x82,%esi
call   1050 <fgets@plt>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x82,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     122c <net_get_gateway+0xcc>
lea    -0x90(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <net_search_gateway@plt>
cmp    $0x0,%rax
jne    1227 <net_get_gateway+0xc7>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1244 <net_get_gateway+0xe4>
jmp    11d4 <net_get_gateway+0x74>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_get_gateway>:
push   %r15
push   %r14
push   %rbx
sub    $0xa0,%rsp
lea    0xe8d(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fe <net_get_gateway+0x9e>
mov    %rax,%r15
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x82,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x82,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11df <net_get_gateway+0x7f>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <net_search_gateway@plt>
test   %rax,%rax
jne    11b0 <net_get_gateway+0x50>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    11ee <net_get_gateway+0x8e>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x2dfa(%rip),%r14
mov    (%r14),%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
lea    0xe01(%rip),%rdi
call   1030 <puts@plt>
jmp    11e7 <net_get_gateway+0x87>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [44 x i8] c"INET (IPv4) not configured in this system.\0A\00"
@INADDR_ANY = global i32 0
define i32 @net_get_gateway() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [132 x i8]
%4 = alloca i32*
%5 = 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* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @INADDR_ANY
store i32 %11, i32* %1
br label %34
12: ; preds = %0
%13 = getelementptr inbounds [132 x i8], [132 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32* @fgets(i8* %13, i32 130, i32* %14)
br label %16
16: ; preds = %29, %12
%17 = getelementptr inbounds [132 x i8], [132 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i32* @fgets(i8* %17, i32 130, i32* %18)
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %30
21: ; preds = %16
%22 = getelementptr inbounds [132 x i8], [132 x i8]* %3, i64 0, i64 0
%23 = call i64 @net_search_gateway(i8* %22, i32* %2)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32*, i32** %4
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %2
store i32 %28, i32* %1
br label %34
29: ; preds = %21
br label %16
30: ; preds = %16
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* @INADDR_ANY
store i32 %33, i32* %1
br label %34
34: ; preds = %30, %25, %8
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@INADDR_ANY = global i32 0
@str = [43 x i8] c"INET (IPv4) not configured in this system.\00"
define i32 @net_get_gateway() {
%1 = alloca i32
%2 = alloca [132 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [132 x i8], [132 x i8]* %2, i64 0, i64 0
%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 %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %22
10: ; preds = %0
%11 = call i32* @fgets(i8* %4, i32 130, i32* %5)
br label %12
12: ; preds = %15, %10
%13 = call i32* @fgets(i8* %4, i32 130, i32* %5)
%14 = icmp eq i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = call i64 @net_search_gateway(i8* %4, i32* %1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %12
18: ; preds = %15
%19 = call i32 @fclose(i32* %5)
br label %22
20: ; preds = %12
%21 = call i32 @fclose(i32* %5)
br label %22
22: ; preds = %20, %18, %7
%23 = phi i32* [ %1, %18 ], [ @INADDR_ANY, %20 ], [ @INADDR_ANY, %7 ]
%24 = load i32, i32* %23
ret i32 %24
}