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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int system(char *);
int kernel_router() {
  char buf[100];
  int fd, rc;
  int router;
  fd = open("/proc/sys/net/ipv6/conf/all/forwarding", O_RDONLY);
  if (fd < 0)
    return -1;
  rc = read(fd, buf, 99);
  if (rc < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  buf[rc] = '\0';
  router = atoi(buf);
  if (!router)
    return 0;
  rc = system("ip -6 route show | grep -q '^default '");
  return WIFEXITED(rc) && WEXITSTATUS(rc) == 1 ? 1 : 2;
}


x86 assembly (clang, -O0):
<kernel_router>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    11a9 <kernel_router+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <kernel_router+0xee>
mov    -0x74(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x63,%edx
call   1050 <read@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jge    11db <kernel_router+0x6b>
mov    -0x74(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <kernel_router+0xee>
mov    -0x74(%rbp),%edi
call   1040 <close@plt>
movslq -0x78(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
jne    120e <kernel_router+0x9e>
movl   $0x0,-0x4(%rbp)
jmp    125e <kernel_router+0xee>
lea    0xe12(%rip),%rdi
call   1030 <system@plt>
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x7d(%rbp)
je     1248 <kernel_router+0xd8>
mov    -0x78(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x1,%eax
sete   %al
mov    %al,-0x7d(%rbp)
mov    -0x7d(%rbp),%dl
mov    $0x2,%eax
mov    $0x1,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kernel_router>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1206 <kernel_router+0x96>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x63,%edx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %ebx,%ebx
js     1206 <kernel_router+0x96>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1080 <atoi@plt>
test   %eax,%eax
je     11fb <kernel_router+0x8b>
lea    0xe59(%rip),%rdi
call   1030 <system@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     1200 <kernel_router+0x90>
mov    %ebx,%edi
call   1060 <WEXITSTATUS@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
sete   %cl
mov    $0x2,%r14d
sub    %ecx,%r14d
jmp    1206 <kernel_router+0x96>
xor    %r14d,%r14d
jmp    1206 <kernel_router+0x96>
mov    $0x2,%r14d
mov    %r14d,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"/proc/sys/net/ipv6/conf/all/forwarding\00"
@O_RDONLY = global i32 0
@.str.1 = [39 x i8] c"ip -6 route show | grep -q '^default '\00"
define i32 @kernel_router() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %44
11: ; preds = %0
%12 = load i32, i32* %3
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 99)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i32, i32* %3
%19 = call i32 @close(i32 %18)
store i32 -1, i32* %1
br label %44
20: ; preds = %11
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %20
store i32 0, i32* %1
br label %44
31: ; preds = %20
%32 = call i32 @system(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = load i32, i32* %4
%38 = call i32 @WEXITSTATUS(i32 %37)
%39 = icmp eq i32 %38, 1
br label %40
40: ; preds = %36, %31
%41 = phi i1 [ false, %31 ], [ %39, %36 ]
%42 = zext i1 %41 to i64
%43 = select i1 %41, i32 1, i32 2
store i32 %43, i32* %1
br label %44
44: ; preds = %40, %30, %17, %10
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [39 x i8] c"/proc/sys/net/ipv6/conf/all/forwarding\00"
@O_RDONLY = global i32 0
@.str.1 = [39 x i8] c"ip -6 route show | grep -q '^default '\00"
define i32 @kernel_router() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %0
%7 = call i32 @read(i32 %4, i8* %2, i32 99)
%8 = icmp slt i32 %7, 0
%9 = tail call i32 @close(i32 %4)
br i1 %8, label %23, label %10
10: ; preds = %6
%11 = zext i32 %7 to i64
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 @atoi(i8* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = tail call i32 @system(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i64 @WIFEXITED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = tail call i32 @WEXITSTATUS(i32 %16)
%21 = icmp eq i32 %20, 1
%22 = select i1 %21, i32 1, i32 2
br label %23
23: ; preds = %6, %15, %19, %10, %0
%24 = phi i32 [ -1, %0 ], [ 0, %10 ], [ 2, %15 ], [ %22, %19 ], [ -1, %6 ]
ret i32 %24
}