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

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok_r(char *, char *, char **);
int link_detect_ns(char *eth_name, int *detected) {
  int n_dev_len, ns_len = 0;
  char line[200];
  char ath0[120];
  char cmd[250];
  FILE *pipe;
  char *rets;
  char *token;
  strcpy(ath0, eth_name);
  n_dev_len = strlen(ath0);
  if (n_dev_len < 1 || n_dev_len > 120) {
    printf("dev length too long\n");
    return -1;
  }
  memset(line, 0, sizeof(line));
  sprintf(cmd, "ethtool %s | grep detect", ath0);
  pipe = popen(cmd, "r");
  if (!pipe) {
    printf("Failed to open pipe, cmd: %s\n", cmd);
    return -1;
  }
  rets = fgets(line, sizeof(line), pipe);
  if (rets != NULL) {
    char *pointer;
    token = strtok_r(line, " ", &pointer);
    printf("token %s\n", token);
    token = strtok_r(NULL, " ", &pointer);
    printf("token %s\n", token);
    token = strtok_r(NULL, " ", &pointer);
    printf("token %s\n", token);
  } else {
    pclose(pipe);
    printf("interface %s not exitst\n", ath0);
    return -1;
  }
  if (memcmp(token, "no", 2) == 0) {
    *detected = 0;
  } else if (memcmp(token, "yes", 3) == 0) {
    *detected = 1;
  } else {
    pclose(pipe);
    *detected = 2;
    return -1;
  }
  pclose(pipe);
  return 0;
}


x86 assembly (clang, -O0):
<link_detect_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x290,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x170(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x170(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jl     11ed <link_detect_ns+0x4d>
cmpl   $0x78,-0x1c(%rbp)
jle    1207 <link_detect_ns+0x67>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1414 <link_detect_ns+0x274>
lea    -0xf0(%rbp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1070 <memset@plt>
lea    -0x270(%rbp),%rdi
lea    -0x170(%rbp),%rdx
lea    0xde6(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x270(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   10b0 <popen@plt>
mov    %rax,-0x278(%rbp)
cmpq   $0x0,-0x278(%rbp)
jne    127d <link_detect_ns+0xdd>
lea    -0x270(%rbp),%rsi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1414 <link_detect_ns+0x274>
lea    -0xf0(%rbp),%rdi
mov    -0x278(%rbp),%rdx
mov    $0xc8,%esi
call   10a0 <fgets@plt>
mov    %rax,-0x280(%rbp)
cmpq   $0x0,-0x280(%rbp)
je     134b <link_detect_ns+0x1ab>
lea    -0xf0(%rbp),%rdi
lea    0xd96(%rip),%rsi
lea    -0x290(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x288(%rbp)
mov    -0x288(%rbp),%rsi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd63(%rip),%rsi
lea    -0x290(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x288(%rbp)
mov    -0x288(%rbp),%rsi
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd30(%rip),%rsi
lea    -0x290(%rbp),%rdx
call   1080 <strtok_r@plt>
mov    %rax,-0x288(%rbp)
mov    -0x288(%rbp),%rsi
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1378 <link_detect_ns+0x1d8>
mov    -0x278(%rbp),%rdi
call   1060 <pclose@plt>
lea    -0x170(%rbp),%rsi
lea    0xcf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1414 <link_detect_ns+0x274>
mov    -0x288(%rbp),%rdi
lea    0xced(%rip),%rsi
mov    $0x2,%edx
call   1090 <memcmp@plt>
cmp    $0x0,%rax
jne    13a9 <link_detect_ns+0x209>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
jmp    1401 <link_detect_ns+0x261>
mov    -0x288(%rbp),%rdi
lea    0xcbf(%rip),%rsi
mov    $0x3,%edx
call   1090 <memcmp@plt>
cmp    $0x0,%rax
jne    13da <link_detect_ns+0x23a>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
jmp    13fc <link_detect_ns+0x25c>
mov    -0x278(%rbp),%rdi
call   1060 <pclose@plt>
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1414 <link_detect_ns+0x274>
jmp    1401 <link_detect_ns+0x261>
mov    -0x278(%rbp),%rdi
call   1060 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x290,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<link_detect_ns>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x268,%rsp
mov    %rsi,%r14
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0xffffff87,%eax
cmp    $0xffffff87,%eax
ja     11fa <link_detect_ns+0x4a>
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    135f <link_detect_ns+0x1af>
lea    0x90(%rsp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1080 <memset@plt>
lea    0xdeb(%rip),%rsi
lea    0x160(%rsp),%rbx
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   10d0 <sprintf@plt>
lea    0xde8(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <popen@plt>
test   %rax,%rax
je     131c <link_detect_ns+0x16c>
mov    %rax,%r15
lea    0x90(%rsp),%rdi
mov    $0xc8,%esi
mov    %rax,%rdx
call   10b0 <fgets@plt>
test   %rax,%rax
je     132d <link_detect_ns+0x17d>
lea    0xdcf(%rip),%r12
lea    0x90(%rsp),%rdi
lea    0x8(%rsp),%r13
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <strtok_r@plt>
lea    0xdb2(%rip),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <strtok_r@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <strtok_r@plt>
mov    %rax,%r12
mov    %rbp,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd88(%rip),%rsi
mov    %r12,%rdi
mov    $0x2,%edx
call   10a0 <memcmp@plt>
mov    $0x0,%ecx
test   %rax,%rax
je     1354 <link_detect_ns+0x1a4>
lea    0xd6d(%rip),%rsi
mov    %r12,%rdi
mov    $0x3,%edx
call   10a0 <memcmp@plt>
test   %rax,%rax
je     134f <link_detect_ns+0x19f>
mov    %r15,%rdi
call   1070 <pclose@plt>
movl   $0x2,(%r14)
mov    $0xffffffff,%ebx
jmp    135f <link_detect_ns+0x1af>
lea    0xcf8(%rip),%rdi
lea    0x160(%rsp),%rsi
jmp    1341 <link_detect_ns+0x191>
mov    %r15,%rdi
call   1070 <pclose@plt>
lea    0xd09(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
jmp    135f <link_detect_ns+0x1af>
mov    $0x1,%ecx
mov    %ecx,(%r14)
mov    %r15,%rdi
call   1070 <pclose@plt>
mov    %ebx,%eax
add    $0x268,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"dev length too long\0A\00"
@.str.1 = [25 x i8] c"ethtool %s | grep detect\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [30 x i8] c"Failed to open pipe, cmd: %s\0A\00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [10 x i8] c"token %s\0A\00"
@.str.6 = [25 x i8] c"interface %s not exitst\0A\00"
@.str.7 = [3 x i8] c"no\00"
@.str.8 = [4 x i8] c"yes\00"
define i32 @link_detect_ns(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [200 x i8]
%9 = alloca [120 x i8]
%10 = alloca [250 x i8]
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %7
%15 = getelementptr inbounds [120 x i8], [120 x i8]* %9, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = call i32 @strcpy(i8* %15, i8* %16)
%18 = getelementptr inbounds [120 x i8], [120 x i8]* %9, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 1
br i1 %21, label %25, label %22
22: ; preds = %2
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 120
br i1 %24, label %25, label %27
25: ; preds = %22, %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
27: ; preds = %22
%28 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i32 0, i32 200)
%30 = getelementptr inbounds [250 x i8], [250 x i8]* %10, i64 0, i64 0
%31 = getelementptr inbounds [120 x i8], [120 x i8]* %9, i64 0, i64 0
%32 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [250 x i8], [250 x i8]* %10, i64 0, i64 0
%34 = call i32* @popen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %11
%35 = load i32*, i32** %11
%36 = icmp ne i32* %35, null
br i1 %36, label %40, label %37
37: ; preds = %27
%38 = getelementptr inbounds [250 x i8], [250 x i8]* %10, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %38)
store i32 -1, i32* %3
br label %82
40: ; preds = %27
%41 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%42 = load i32*, i32** %11
%43 = call i8* @fgets(i8* %41, i32 200, i32* %42)
store i8* %43, i8** %12
%44 = load i8*, i8** %12
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %57
46: ; preds = %40
%47 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%48 = call i8* @strtok_r(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %14)
store i8* %48, i8** %13
%49 = load i8*, i8** %13
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %14)
store i8* %51, i8** %13
%52 = load i8*, i8** %13
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %52)
%54 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %14)
store i8* %54, i8** %13
%55 = load i8*, i8** %13
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %55)
br label %62
57: ; preds = %40
%58 = load i32*, i32** %11
%59 = call i32 @pclose(i32* %58)
%60 = getelementptr inbounds [120 x i8], [120 x i8]* %9, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %60)
store i32 -1, i32* %3
br label %82
62: ; preds = %46
%63 = load i8*, i8** %13
%64 = call i64 @memcmp(i8* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = load i32*, i32** %5
store i32 0, i32* %67
br label %79
68: ; preds = %62
%69 = load i8*, i8** %13
%70 = call i64 @memcmp(i8* %69, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 3)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = load i32*, i32** %5
store i32 1, i32* %73
br label %78
74: ; preds = %68
%75 = load i32*, i32** %11
%76 = call i32 @pclose(i32* %75)
%77 = load i32*, i32** %5
store i32 2, i32* %77
store i32 -1, i32* %3
br label %82
78: ; preds = %72
br label %79
79: ; preds = %78, %66
%80 = load i32*, i32** %11
%81 = call i32 @pclose(i32* %80)
store i32 0, i32* %3
br label %82
82: ; preds = %79, %74, %57, %37, %25
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str.1 = [25 x i8] c"ethtool %s | grep detect\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [30 x i8] c"Failed to open pipe, cmd: %s\0A\00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [10 x i8] c"token %s\0A\00"
@.str.6 = [25 x i8] c"interface %s not exitst\0A\00"
@.str.7 = [3 x i8] c"no\00"
@.str.8 = [4 x i8] c"yes\00"
@str = [20 x i8] c"dev length too long\00"
define i32 @link_detect_ns(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [200 x i8]
%4 = alloca [120 x i8]
%5 = alloca [250 x i8]
%6 = alloca i8*
%7 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [120 x i8], [120 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [250 x i8], [250 x i8]* %5, i64 0, i64 0
%10 = call i32 @strcpy(i8* %8, i8* %0)
%11 = call i32 @strlen(i8* %8)
%12 = add i32 %11, -121
%13 = icmp ult i32 %12, -120
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %47
16: ; preds = %2
%17 = call i32 @memset(i8* %7, i32 0, i32 200)
%18 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%19 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %9)
br label %47
23: ; preds = %16
%24 = call i8* @fgets(i8* %7, i32 200, i32* %19)
%25 = icmp eq i8* %24, null
br i1 %25, label %36, label %26
26: ; preds = %23
%27 = bitcast i8** %6 to i8*
%28 = call i8* @strtok_r(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %6)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %28)
%30 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %6)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %30)
%32 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8** %6)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %32)
%34 = call i64 @memcmp(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2)
%35 = icmp eq i64 %34, 0
br i1 %35, label %44, label %39
36: ; preds = %23
%37 = call i32 @pclose(i32* %19)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %8)
br label %47
39: ; preds = %26
%40 = call i64 @memcmp(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i32 3)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %39
%43 = call i32 @pclose(i32* %19)
store i32 2, i32* %1
br label %47
44: ; preds = %39, %26
%45 = phi i32 [ 0, %26 ], [ 1, %39 ]
store i32 %45, i32* %1
%46 = call i32 @pclose(i32* %19)
br label %47
47: ; preds = %44, %42, %36, %21, %14
%48 = phi i32 [ -1, %14 ], [ 0, %44 ], [ -1, %42 ], [ -1, %36 ], [ -1, %21 ]
ret i32 %48
}