#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ *strstr(char *, char *);
int tokenize(char *, char **, char *, char *, char *);
void get_wanpipe_state(char *device_name, char *state) {
  char router_dir[] = "/proc/net/wanrouter";
  FILE *pipe_fd;
  char line[100];
  int i, len, found = 0;
  int toknum;
  char *token[32];
  struct stat file_stat;
  strcpy(state, "unconfigured");
  if (stat(router_dir, &file_stat) < 0)
    return;
  pipe_fd = popen("cat /proc/net/wanrouter/status", "r");
  if (pipe_fd == NULL)
    return;
  i = 0;
  while (fgets(line, sizeof(line) - 1, pipe_fd)) {
    if (strstr(line, device_name) != NULL) {
      found = 1;
      break;
    }
  }
  pclose(pipe_fd);
  if (!found) {
    return;
  }
  len = strlen(line) + 1;
  toknum = tokenize(line, token, "|", " \t\n", "|");
  if (toknum <= 3)
    return;
  if (strcmp(token[0], device_name))
    return;
  strcpy(state, token[3]);
  return;
}


x86 assembly (clang, -O0):
<get_wanpipe_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe56(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe53(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe51(%rip),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0xac(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rdi
lea    -0x1b8(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    11fb <get_wanpipe_state+0x6b>
jmp    1314 <get_wanpipe_state+0x184>
lea    0xe1f(%rip),%rdi
lea    0xe37(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1222 <get_wanpipe_state+0x92>
jmp    1314 <get_wanpipe_state+0x184>
movl   $0x0,-0xa4(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x63,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1279 <get_wanpipe_state+0xe9>
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1274 <get_wanpipe_state+0xe4>
movl   $0x1,-0xac(%rbp)
jmp    1279 <get_wanpipe_state+0xe9>
jmp    122c <get_wanpipe_state+0x9c>
mov    -0x38(%rbp),%rdi
call   1050 <pclose@plt>
cmpl   $0x0,-0xac(%rbp)
jne    1294 <get_wanpipe_state+0x104>
jmp    1314 <get_wanpipe_state+0x184>
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
lea    -0x1b0(%rbp),%rsi
lea    0xd84(%rip),%r8
lea    0xd7f(%rip),%rcx
mov    %r8,%rdx
call   1090 <tokenize@plt>
mov    %eax,-0xb0(%rbp)
cmpl   $0x3,-0xb0(%rbp)
jg     12e5 <get_wanpipe_state+0x155>
jmp    1314 <get_wanpipe_state+0x184>
mov    -0x1b0(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1304 <get_wanpipe_state+0x174>
jmp    1314 <get_wanpipe_state+0x184>
mov    -0x10(%rbp),%rdi
mov    -0x198(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_wanpipe_state>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1a8,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movaps 0xe55(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x726574,0x20(%rsp)
lea    0xe55(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     129f <get_wanpipe_state+0x10f>
lea    0xe3b(%rip),%rdi
lea    0xe53(%rip),%rsi
call   10a0 <popen@plt>
test   %rax,%rax
je     129f <get_wanpipe_state+0x10f>
mov    %rax,%rbx
lea    0x30(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    $0x63,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1297 <get_wanpipe_state+0x107>
mov    %r12,%rdi
mov    %r15,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     1210 <get_wanpipe_state+0x80>
mov    %rbx,%rdi
call   1050 <pclose@plt>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0xdf3(%rip),%rcx
lea    0xdea(%rip),%rdx
lea    0xa0(%rsp),%rsi
mov    %rbx,%rdi
mov    %rdx,%r8
call   1090 <tokenize@plt>
cmp    $0x4,%eax
jl     129f <get_wanpipe_state+0x10f>
mov    0xa0(%rsp),%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    129f <get_wanpipe_state+0x10f>
mov    0xb8(%rsp),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
jmp    129f <get_wanpipe_state+0x10f>
mov    %rbx,%rdi
call   1050 <pclose@plt>
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.get_wanpipe_state.router_dir = [20 x i8] c"/proc/net/wanrouter\00"
@.str = [13 x i8] c"unconfigured\00"
@.str.1 = [31 x i8] c"cat /proc/net/wanrouter/status\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"|\00"
@.str.4 = [4 x i8] c" \09\0A\00"
define void @get_wanpipe_state(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [20 x i8]
%6 = alloca i32*
%7 = alloca [100 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [32 x i8*]
%13 = alloca %struct.stat
store i8* %0, i8** %3
store i8* %1, i8** %4
%14 = bitcast [20 x i8]* %5 to i8*
store i32 0, i32* %10
%15 = load i8*, i8** %4
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%18 = call i64 @stat(i8* %17, %struct.stat* %13)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
br label %67
21: ; preds = %2
%22 = call i32* @popen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
br label %67
26: ; preds = %21
store i32 0, i32* %8
br label %27
27: ; preds = %38, %26
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = call i64 @fgets(i8* %28, i32 99, i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %27
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%34 = load i8*, i8** %3
%35 = call i32* @strstr(i8* %33, i8* %34)
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 1, i32* %10
br label %39
38: ; preds = %32
br label %27
39: ; preds = %37, %27
%40 = load i32*, i32** %6
%41 = call i32 @pclose(i32* %40)
%42 = load i32, i32* %10
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %39
br label %67
45: ; preds = %39
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 %47, 1
store i32 %48, i32* %9
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%50 = getelementptr inbounds [32 x i8*], [32 x i8*]* %12, i64 0, i64 0
%51 = call i32 @tokenize(i8* %49, i8** %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 %51, i32* %11
%52 = load i32, i32* %11
%53 = icmp sle i32 %52, 3
br i1 %53, label %54, label %55
54: ; preds = %45
br label %67
55: ; preds = %45
%56 = getelementptr inbounds [32 x i8*], [32 x i8*]* %12, i64 0, i64 0
%57 = load i8*, i8** %56
%58 = load i8*, i8** %3
%59 = call i64 @strcmp(i8* %57, i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
br label %67
62: ; preds = %55
%63 = load i8*, i8** %4
%64 = getelementptr inbounds [32 x i8*], [32 x i8*]* %12, i64 0, i64 3
%65 = load i8*, i8** %64
%66 = call i32 @strcpy(i8* %63, i8* %65)
br label %67
67: ; preds = %62, %61, %54, %44, %25, %20
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.get_wanpipe_state.router_dir = [20 x i8] c"/proc/net/wanrouter\00"
@.str = [13 x i8] c"unconfigured\00"
@.str.1 = [31 x i8] c"cat /proc/net/wanrouter/status\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"|\00"
@.str.4 = [4 x i8] c" \09\0A\00"
define void @get_wanpipe_state(i8* %0, i8* %1) {
%3 = alloca [20 x i8]
%4 = alloca [100 x i8]
%5 = alloca [32 x i8*]
%6 = alloca %struct.stat
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%9 = bitcast [32 x i8*]* %5 to i8*
%10 = bitcast %struct.stat* %6 to i8*
%11 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = call i64 @stat(i8* %7, %struct.stat* %6)
%13 = icmp slt i64 %12, 0
br i1 %13, label %39, label %14
14: ; preds = %2
%15 = call i32* @popen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %39, label %17
17: ; preds = %14, %20
%18 = call i64 @fgets(i8* %8, i32 99, i32* %15)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = call i32* @strstr(i8* %8, i8* dereferenceable(1) %0)
%22 = icmp eq i32* %21, null
br i1 %22, label %17, label %25
23: ; preds = %17
%24 = call i32 @pclose(i32* %15)
br label %39
25: ; preds = %20
%26 = call i32 @pclose(i32* %15)
%27 = call i32 @strlen(i8* %8)
%28 = getelementptr inbounds [32 x i8*], [32 x i8*]* %5, i64 0, i64 0
%29 = call i32 @tokenize(i8* %8, i8** %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp slt i32 %29, 4
br i1 %30, label %39, label %31
31: ; preds = %25
%32 = load i8*, i8** %28
%33 = call i64 @strcmp(i8* %32, i8* %0)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = getelementptr inbounds [32 x i8*], [32 x i8*]* %5, i64 0, i64 3
%37 = load i8*, i8** %36
%38 = call i32 @strcpy(i8* %1, i8* %37)
br label %39
39: ; preds = %31, %25, %23, %14, %2, %35
ret void
}