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

/* Variables and functions */
int /*<<< orphan*/ arp_disp(char *, int, int, char *, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int sscanf(char *, char *, char *, int *, int *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int arp_show(char *device) {
  struct sockaddr sa;
  char ip[100];
  char hwa[100];
  char mask[100];
  char line[200];
  char dev[100];
  int type, flags;
  FILE *fp;
  int num;
  if ((fp = fopen("/proc/net/arp", "r")) == NULL) {
    perror("/proc/net/arp");
    return -1;
  }
  if (fgets(line, sizeof(line), fp) != (char *)NULL) {
    strcpy(mask, "-");
    strcpy(dev, "-");
    for (; fgets(line, sizeof(line), fp);) {
      num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n", ip, &type, &flags,
                   hwa, mask, dev);
      if (num < 4)
        break;
      if (device[0] && strcmp(dev, device))
        continue;
      arp_disp(ip, type, flags, hwa, mask, dev);
    }
  }
  (void)fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<arp_show>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2d0,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x2b0(%rbp)
cmp    $0x0,%rax
jne    11cb <arp_show+0x4b>
lea    0xe46(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <arp_show+0x186>
lea    -0x230(%rbp),%rdi
mov    -0x2b0(%rbp),%rdx
mov    $0xc8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12f3 <arp_show+0x173>
lea    -0x160(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x2a0(%rbp),%rdi
lea    0xe02(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x230(%rbp),%rdi
mov    -0x2b0(%rbp),%rdx
mov    $0xc8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12ee <arp_show+0x16e>
lea    -0x230(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    -0xf0(%rbp),%r9
lea    -0x160(%rbp),%r10
lea    -0x2a0(%rbp),%rax
lea    0xdb6(%rip),%rsi
lea    -0x2a4(%rbp),%rcx
lea    -0x2a8(%rbp),%r8
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1080 <sscanf@plt>
mov    %eax,-0x2b4(%rbp)
cmpl   $0x4,-0x2b4(%rbp)
jge    1290 <arp_show+0x110>
jmp    12ee <arp_show+0x16e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12bf <arp_show+0x13f>
lea    -0x2a0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12bf <arp_show+0x13f>
jmp    1213 <arp_show+0x93>
lea    -0x80(%rbp),%rdi
mov    -0x2a4(%rbp),%esi
mov    -0x2a8(%rbp),%edx
lea    -0xf0(%rbp),%rcx
lea    -0x160(%rbp),%r8
lea    -0x2a0(%rbp),%r9
call   1040 <arp_disp@plt>
jmp    1213 <arp_show+0x93>
jmp    12f3 <arp_show+0x173>
mov    -0x2b0(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arp_show>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x298,%rsp
mov    %rdi,%r14
lea    0xe65(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12c4 <arp_show+0x144>
mov    %rax,%rbx
lea    0x80(%rsp),%rdi
mov    $0xc8,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12a8 <arp_show+0x128>
lea    0xe38(%rip),%rbp
lea    0x10(%rsp),%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0x150(%rsp),%r12
mov    %r12,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0x80(%rsp),%rdi
mov    $0xc8,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12a8 <arp_show+0x128>
lea    0x80(%rsp),%rbp
lea    0x230(%rsp),%r13
lea    0x1c0(%rsp),%r15
jmp    1260 <arp_show+0xe0>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %r13,%rdi
mov    %r15,%rcx
lea    0x10(%rsp),%r8
mov    %r12,%r9
call   1040 <arp_disp@plt>
mov    %rbp,%rdi
mov    $0xc8,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12a8 <arp_show+0x128>
mov    %rbp,%rdi
lea    0xda8(%rip),%rsi
mov    %r13,%rdx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %r15,%r9
push   %r12
lea    0x18(%rsp),%rax
push   %rax
call   1080 <sscanf@plt>
add    $0x10,%rsp
cmp    $0x4,%eax
jl     12a8 <arp_show+0x128>
cmpb   $0x0,(%r14)
je     1230 <arp_show+0xb0>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    124b <arp_show+0xcb>
jmp    1230 <arp_show+0xb0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x298,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12b2 <arp_show+0x132>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"/proc/net/arp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [32 x i8] c"%s 0x%x 0x%x %100s %100s %100s\0A\00"
define i32 @arp_show(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr
%5 = alloca [100 x i8]
%6 = alloca [100 x i8]
%7 = alloca [100 x i8]
%8 = alloca [200 x i8]
%9 = alloca [100 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
store i8* %0, i8** %3
%14 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %12
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
18: ; preds = %1
%19 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%20 = load i32*, i32** %12
%21 = call i8* @fgets(i8* %19, i32 200, i32* %20)
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %64
23: ; preds = %18
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%27 = call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %55, %54, %23
%29 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%30 = load i32*, i32** %12
%31 = call i8* @fgets(i8* %29, i32 200, i32* %30)
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %63
33: ; preds = %28
%34 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%39 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %35, i32* %10, i32* %11, i8* %36, i8* %37, i8* %38)
store i32 %39, i32* %13
%40 = load i32, i32* %13
%41 = icmp slt i32 %40, 4
br i1 %41, label %42, label %43
42: ; preds = %33
br label %63
43: ; preds = %33
%44 = load i8*, i8** %3
%45 = getelementptr inbounds i8, i8* %44, i64 0
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %43
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%51 = load i8*, i8** %3
%52 = call i64 @strcmp(i8* %50, i8* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
br label %28
55: ; preds = %49, %43
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%57 = load i32, i32* %10
%58 = load i32, i32* %11
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%61 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%62 = call i32 @arp_disp(i8* %56, i32 %57, i32 %58, i8* %59, i8* %60, i8* %61)
br label %28
63: ; preds = %42, %28
br label %64
64: ; preds = %63, %18
%65 = load i32*, i32** %12
%66 = call i32 @fclose(i32* %65)
store i32 0, i32* %2
br label %67
67: ; preds = %64, %16
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/net/arp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [32 x i8] c"%s 0x%x 0x%x %100s %100s %100s\0A\00"
define i32 @arp_show(i8* %0) {
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = alloca [200 x i8]
%6 = alloca [100 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%12 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%14 = bitcast i32* %7 to i8*
%15 = bitcast i32* %8 to i8*
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %46
20: ; preds = %1
%21 = call i8* @fgets(i8* %12, i32 200, i32* %16)
%22 = icmp eq i8* %21, null
br i1 %22, label %44, label %23
23: ; preds = %20
%24 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = call i8* @fgets(i8* %12, i32 200, i32* %16)
%27 = icmp eq i8* %26, null
br i1 %27, label %44, label %28
28: ; preds = %23, %41
%29 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %9, i32* %7, i32* %8, i8* %10, i8* %11, i8* %13)
%30 = icmp slt i32 %29, 4
br i1 %30, label %44, label %31
31: ; preds = %28
%32 = load i8, i8* %0
%33 = icmp eq i8 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = call i64 @strcmp(i8* %13, i8* %0)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34, %31
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 @arp_disp(i8* %9, i32 %38, i32 %39, i8* %10, i8* %11, i8* %13)
br label %41
41: ; preds = %37, %34
%42 = call i8* @fgets(i8* %12, i32 200, i32* %16)
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %28
44: ; preds = %28, %41, %23, %20
%45 = call i32 @fclose(i32* %16)
br label %46
46: ; preds = %44, %18
%47 = phi i32 [ -1, %18 ], [ 0, %44 ]
ret i32 %47
}