#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int net_add_gateway(int /*<<< orphan*/);
int /*<<< orphan*/ net_del_gateway(int /*<<< orphan*/);
scalar_t__ net_search_gateway(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int net_set_gateway(in_addr_t addr) {
  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 -1;
  }
  fgets(buff, 130, fp);
  while (fgets(buff, 130, fp) != NULL) {
    if (net_search_gateway(buff, &gate_addr) == 0) {
      net_del_gateway(gate_addr);
    }
  }
  fclose(fp);
  return net_add_gateway(addr);
}


x86 assembly (clang, -O0):
<net_set_gateway>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe6b(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    11da <net_set_gateway+0x5a>
lea    0xe43(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <net_set_gateway+0xd2>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x82,%esi
call   1060 <fgets@plt>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x82,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     123b <net_set_gateway+0xbb>
lea    -0x90(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <net_search_gateway@plt>
cmp    $0x0,%rax
jne    1236 <net_set_gateway+0xb6>
mov    -0xc(%rbp),%edi
call   1080 <net_del_gateway@plt>
jmp    11f2 <net_set_gateway+0x72>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%edi
call   1040 <net_add_gateway@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_set_gateway>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %edi,%r14d
lea    0xe69(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     123e <net_set_gateway+0xbe>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x82,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbp,%rdi
mov    $0x82,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1220 <net_set_gateway+0xa0>
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%r15
jmp    1205 <net_set_gateway+0x85>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    $0x82,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1220 <net_set_gateway+0xa0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <net_search_gateway@plt>
test   %rax,%rax
jne    11f0 <net_set_gateway+0x70>
mov    0xc(%rsp),%edi
call   1080 <net_del_gateway@plt>
jmp    11f0 <net_set_gateway+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14d,%edi
call   1050 <net_add_gateway@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbb(%rip),%rdi
call   10a0 <perror@plt>
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1230 <net_set_gateway+0xb0>

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"
define i32 @net_set_gateway(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [132 x i8]
%6 = alloca i32*
store i32 %0, i32* %3
%7 = 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* %7, i32** %6
%8 = load i32*, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
13: ; preds = %1
%14 = getelementptr inbounds [132 x i8], [132 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %6
%16 = call i32* @fgets(i8* %14, i32 130, i32* %15)
br label %17
17: ; preds = %29, %13
%18 = getelementptr inbounds [132 x i8], [132 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %18, i32 130, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %30
22: ; preds = %17
%23 = getelementptr inbounds [132 x i8], [132 x i8]* %5, i64 0, i64 0
%24 = call i64 @net_search_gateway(i8* %23, i32* %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %4
%28 = call i32 @net_del_gateway(i32 %27)
br label %29
29: ; preds = %26, %22
br label %17
30: ; preds = %17
%31 = load i32*, i32** %6
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* %3
%34 = call i32 @net_add_gateway(i32 %33)
store i32 %34, i32* %2
br label %35
35: ; preds = %30, %10
%36 = load i32, i32* %2
ret i32 %36
}

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