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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
char *strstr(char *, char *);
int find_intf(char **intf, uint8_t *size, int did) {
  char cmd[64];
  snprintf(cmd, 64, "grep -r '%x' /sys/class/net/*/device/device", did);
  FILE *fp = popen(cmd, "r");
  if (fp == NULL) {
    printf("Failed open pipe to run cmd : %s\n", cmd);
    return -1;
  }
  char buf[1024];
  uint8_t index = 0;
  while (fgets(buf, sizeof(buf), fp) != NULL) {
    char *start = strstr(buf, "eth");
    if (start == NULL) {
      printf("Error in parse interface name\n");
      pclose(fp);
      return -1;
    }
    char *end = strstr(buf, "/device");
    if (end == NULL) {
      printf("Error in parse interface name\n");
      pclose(fp);
      return -1;
    }
    uint8_t len = end - start;
    char *interf = (char *)malloc(sizeof(char) * (len + 1));
    snprintf(interf, len + 1, "%s", start);
    intf[index] = interf;
    index++;
  }
  if (index != 4) {
    printf("Failed to find all ethernet interface\n");
    pclose(fp);
    return -1;
  }
  *size = 4;
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<find_intf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x40,%esi
lea    0xe67(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11dd <find_intf+0x6d>
lea    -0x60(%rbp),%rsi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1369 <find_intf+0x1f9>
movl   $0x0,-0x474(%rbp)
lea    -0x470(%rbp),%rdi
mov    -0x68(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     131f <find_intf+0x1af>
lea    -0x470(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x480(%rbp)
cmpq   $0x0,-0x480(%rbp)
jne    1251 <find_intf+0xe1>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x68(%rbp),%rdi
call   1040 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1369 <find_intf+0x1f9>
lea    -0x470(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x488(%rbp)
cmpq   $0x0,-0x488(%rbp)
jne    129c <find_intf+0x12c>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x68(%rbp),%rdi
call   1040 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1369 <find_intf+0x1f9>
mov    -0x488(%rbp),%rax
mov    -0x480(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x48c(%rbp)
mov    -0x48c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x498(%rbp)
mov    -0x498(%rbp),%rdi
mov    -0x48c(%rbp),%esi
add    $0x1,%esi
mov    -0x480(%rbp),%rcx
lea    0xd85(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x498(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x474(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x474(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x474(%rbp)
jmp    11e7 <find_intf+0x77>
cmpl   $0x4,-0x474(%rbp)
je     134f <find_intf+0x1df>
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x68(%rbp),%rdi
call   1040 <pclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1369 <find_intf+0x1f9>
mov    -0x18(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x68(%rbp),%rdi
call   1040 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_intf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x458,%rsp
mov    %edx,%ecx
mov    %rsi,%rbp
mov    %rdi,0x8(%rsp)
lea    0xe5e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     1292 <find_intf+0x112>
mov    %rax,%r13
lea    0x50(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12ac <find_intf+0x12c>
mov    %rbp,(%rsp)
lea    0x50(%rsp),%rbx
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
lea    0xe46(%rip),%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     12b5 <find_intf+0x135>
mov    %rax,%r14
mov    %rbx,%rdi
lea    0xdff(%rip),%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     12b5 <find_intf+0x135>
mov    %rax,%rbp
sub    %r14d,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %ebp,%esi
lea    0xe02(%rip),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x8(%rsp),%rax
mov    %r12,(%rax,%r15,8)
add    $0x1,%r15
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1200 <find_intf+0x80>
cmp    $0x4,%r15d
mov    (%rsp),%rax
jne    12ac <find_intf+0x12c>
movl   $0x4,(%rax)
xor    %ebx,%ebx
jmp    12c6 <find_intf+0x146>
lea    0xd95(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
jmp    12ce <find_intf+0x14e>
lea    0xda4(%rip),%rdi
jmp    12bc <find_intf+0x13c>
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %r13,%rdi
call   1050 <pclose@plt>
mov    %ebx,%eax
add    $0x458,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"grep -r '%x' /sys/class/net/*/device/device\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"Failed open pipe to run cmd : %s\0A\00"
@.str.3 = [4 x i8] c"eth\00"
@.str.4 = [31 x i8] c"Error in parse interface name\0A\00"
@.str.5 = [8 x i8] c"/device\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [39 x i8] c"Failed to find all ethernet interface\0A\00"
define i32 @find_intf(i8** %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [64 x i8]
%9 = alloca i32*
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%17 = load i32, i32* %7
%18 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %16, i32 64, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%20 = call i32* @popen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %9
%21 = load i32*, i32** %9
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 -1, i32* %4
br label %87
26: ; preds = %3
store i32 0, i32* %11
br label %27
27: ; preds = %50, %26
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%29 = load i32*, i32** %9
%30 = call i32* @fgets(i8* %28, i32 1024, i32* %29)
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %76
32: ; preds = %27
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%34 = call i8* @strstr(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i8* %34, i8** %12
%35 = load i8*, i8** %12
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32*, i32** %9
%40 = call i32 @pclose(i32* %39)
store i32 -1, i32* %4
br label %87
41: ; preds = %32
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%43 = call i8* @strstr(i8* %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i8* %43, i8** %13
%44 = load i8*, i8** %13
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32*, i32** %9
%49 = call i32 @pclose(i32* %48)
store i32 -1, i32* %4
br label %87
50: ; preds = %41
%51 = load i8*, i8** %13
%52 = load i8*, i8** %12
%53 = ptrtoint i8* %51 to i64
%54 = ptrtoint i8* %52 to i64
%55 = sub i64 %53, %54
%56 = trunc i64 %55 to i32
store i32 %56, i32* %14
%57 = load i32, i32* %14
%58 = add nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = mul i64 1, %59
%61 = trunc i64 %60 to i32
%62 = call i64 @malloc(i32 %61)
%63 = inttoptr i64 %62 to i8*
store i8* %63, i8** %15
%64 = load i8*, i8** %15
%65 = load i32, i32* %14
%66 = add nsw i32 %65, 1
%67 = load i8*, i8** %12
%68 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %64, i32 %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %15
%70 = load i8**, i8*** %5
%71 = load i32, i32* %11
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %70, i64 %72
store i8* %69, i8** %73
%74 = load i32, i32* %11
%75 = add nsw i32 %74, 1
store i32 %75, i32* %11
br label %27
76: ; preds = %27
%77 = load i32, i32* %11
%78 = icmp ne i32 %77, 4
br i1 %78, label %79, label %83
79: ; preds = %76
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%81 = load i32*, i32** %9
%82 = call i32 @pclose(i32* %81)
store i32 -1, i32* %4
br label %87
83: ; preds = %76
%84 = load i32*, i32** %6
store i32 4, i32* %84
%85 = load i32*, i32** %9
%86 = call i32 @pclose(i32* %85)
store i32 0, i32* %4
br label %87
87: ; preds = %83, %79, %46, %37, %23
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
@.str = [44 x i8] c"grep -r '%x' /sys/class/net/*/device/device\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [34 x i8] c"Failed open pipe to run cmd : %s\0A\00"
@.str.3 = [4 x i8] c"eth\00"
@.str.5 = [8 x i8] c"/device\00"
@.str.6 = [3 x i8] c"%s\00"
@str = [38 x i8] c"Failed to find all ethernet interface\00"
@str.9 = [30 x i8] c"Error in parse interface name\00"
define i32 @find_intf(i8** nocapture writeonly %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %2)
%8 = call i32* @popen(i8* %6, 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 %10, label %12
10: ; preds = %3
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %49
12: ; preds = %3
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = call i32* @fgets(i8* %13, i32 1024, i32* %8)
%15 = icmp eq i32* %14, null
br i1 %15, label %43, label %16
16: ; preds = %12, %27
%17 = phi i64 [ %37, %27 ], [ 0, %12 ]
%18 = call i8* @strstr(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.9, i64 0, i64 0))
br label %46
22: ; preds = %16
%23 = call i8* @strstr(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.9, i64 0, i64 0))
br label %46
27: ; preds = %22
%28 = ptrtoint i8* %23 to i64
%29 = ptrtoint i8* %18 to i64
%30 = sub i64 %28, %29
%31 = trunc i64 %30 to i32
%32 = add nsw i32 %31, 1
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
%35 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %34, i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %18)
%36 = getelementptr inbounds i8*, i8** %0, i64 %17
store i8* %34, i8** %36
%37 = add nuw i64 %17, 1
%38 = call i32* @fgets(i8* %13, i32 1024, i32* %8)
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %16
40: ; preds = %27
%41 = trunc i64 %37 to i32
%42 = icmp eq i32 %41, 4
br i1 %42, label %45, label %43
43: ; preds = %12, %40
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %46
45: ; preds = %40
store i32 4, i32* %1
br label %46
46: ; preds = %25, %20, %45, %43
%47 = phi i32 [ -1, %43 ], [ 0, %45 ], [ -1, %20 ], [ -1, %25 ]
%48 = call i32 @pclose(i32* %8)
br label %49
49: ; preds = %46, %10
%50 = phi i32 [ -1, %10 ], [ %47, %46 ]
ret i32 %50
}