#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 scalar_t__ uint32_t;
struct in_addr {
  scalar_t__ s_addr;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int sscanf(char *, char *, char *, scalar_t__ *, scalar_t__ *, scalar_t__ *);
scalar_t__ strcmp(char *, char const *);
int get_gateway(const char *ifname, struct in_addr *gateway) {
  FILE *fp = fopen("/proc/net/route", "r");
  if (!fp)
    return -1;
  char line[128], iface[8];
  uint32_t dest, gw, flags;
  while (fgets(line, sizeof(line), fp))
    if (sscanf(line, "%s %x %x %x", iface, &dest, &gw, &flags) == 4 &&
        strcmp(iface, ifname) == 0 && dest == 0 && (flags & 2)) {
      fclose(fp);
      if (gateway)
        gateway->s_addr = gw;
      return 0;
    }
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<get_gateway>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe96(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1191 <get_gateway+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <get_gateway+0x11d>
jmp    1196 <get_gateway+0x46>
lea    -0xa0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     125d <get_gateway+0x10d>
lea    -0xa0(%rbp),%rdi
lea    -0xa8(%rbp),%rdx
lea    0xe48(%rip),%rsi
lea    -0xb0(%rbp),%rcx
lea    -0xb8(%rbp),%r8
lea    -0xc0(%rbp),%r9
call   1060 <sscanf@plt>
cmp    $0x4,%eax
jne    1258 <get_gateway+0x108>
lea    -0xa8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1258 <get_gateway+0x108>
cmpq   $0x0,-0xb0(%rbp)
jne    1258 <get_gateway+0x108>
mov    -0xc0(%rbp),%rax
and    $0x2,%rax
cmp    $0x0,%rax
je     1258 <get_gateway+0x108>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     124c <get_gateway+0xfc>
mov    -0xb8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    126d <get_gateway+0x11d>
jmp    1196 <get_gateway+0x46>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_gateway>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %rsi,%rbx
mov    %rdi,0x8(%rsp)
lea    0xe90(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     124c <get_gateway+0xfc>
mov    %rax,%r13
mov    %rbx,(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     123d <get_gateway+0xed>
lea    0x30(%rsp),%rbx
lea    0x28(%rsp),%rbp
lea    0x20(%rsp),%r14
lea    0x18(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    11e5 <get_gateway+0x95>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     123d <get_gateway+0xed>
mov    %rbx,%rdi
lea    0xe23(%rip),%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %r15,%r8
mov    %r12,%r9
call   1060 <sscanf@plt>
cmp    $0x4,%eax
jne    11d0 <get_gateway+0x80>
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
call   1050 <strcmp@plt>
or     0x20(%rsp),%rax
jne    11d0 <get_gateway+0x80>
testb  $0x2,0x10(%rsp)
je     11d0 <get_gateway+0x80>
mov    %r13,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    (%rsp),%rdx
test   %rdx,%rdx
je     1251 <get_gateway+0x101>
mov    0x18(%rsp),%rcx
mov    %rcx,(%rdx)
jmp    1251 <get_gateway+0x101>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    1251 <get_gateway+0x101>
mov    $0xffffffff,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i64 }
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"%s %x %x %x\00"
define i32 @get_gateway(i8* %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca i32*
%7 = alloca [128 x i8]
%8 = alloca [8 x i8]
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %4
store %struct.in_addr* %1, %struct.in_addr** %5
%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** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 -1, i32* %3
br label %53
16: ; preds = %2
br label %17
17: ; preds = %49, %16
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i64 @fgets(i8* %18, i32 128, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %50
22: ; preds = %17
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%25 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %24, i64* %9, i64* %10, i64* %11)
%26 = icmp eq i32 %25, 4
br i1 %26, label %27, label %49
27: ; preds = %22
%28 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%29 = load i8*, i8** %4
%30 = call i64 @strcmp(i8* %28, i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %49
32: ; preds = %27
%33 = load i64, i64* %9
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %32
%36 = load i64, i64* %11
%37 = and i64 %36, 2
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %35
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
%42 = load %struct.in_addr*, %struct.in_addr** %5
%43 = icmp ne %struct.in_addr* %42, null
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i64, i64* %10
%46 = load %struct.in_addr*, %struct.in_addr** %5
%47 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %46, i32 0, i32 0
store i64 %45, i64* %47
br label %48
48: ; preds = %44, %39
store i32 0, i32* %3
br label %53
49: ; preds = %35, %32, %27, %22
br label %17
50: ; preds = %17
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
store i32 1, i32* %3
br label %53
53: ; preds = %50, %48, %15
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.in_addr = type { i64 }
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"%s %x %x %x\00"
define i32 @get_gateway(i8* %0, %struct.in_addr* writeonly %1) {
%3 = alloca [128 x i8]
%4 = alloca [8 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%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 %44, label %10
10: ; preds = %2
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%13 = bitcast i64* %5 to i8*
%14 = bitcast i64* %6 to i8*
%15 = bitcast i64* %7 to i8*
%16 = call i64 @fgets(i8* %11, i32 128, i32* %8)
%17 = icmp eq i64 %16, 0
br i1 %17, label %40, label %18
18: ; preds = %10, %37
%19 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %12, i64* %5, i64* %6, i64* %7)
%20 = icmp eq i32 %19, 4
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = call i64 @strcmp(i8* %12, i8* %0)
%23 = icmp eq i64 %22, 0
%24 = load i64, i64* %5
%25 = icmp eq i64 %24, 0
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %27, label %37
27: ; preds = %21
%28 = load i64, i64* %7
%29 = and i64 %28, 2
%30 = icmp eq i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %27
%32 = call i32 @fclose(i32* %8)
%33 = icmp eq %struct.in_addr* %1, null
br i1 %33, label %42, label %34
34: ; preds = %31
%35 = load i64, i64* %6
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
store i64 %35, i64* %36
br label %42
37: ; preds = %27, %21, %18
%38 = call i64 @fgets(i8* %11, i32 128, i32* %8)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %18
40: ; preds = %37, %10
%41 = call i32 @fclose(i32* %8)
br label %42
42: ; preds = %31, %34, %40
%43 = phi i32 [ 1, %40 ], [ 0, %34 ], [ 0, %31 ]
br label %44
44: ; preds = %2, %42
%45 = phi i32 [ %43, %42 ], [ -1, %2 ]
ret i32 %45
}