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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *inet_ntoa(struct in_addr);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
bool is_addr_vald(struct in_addr addr) {
  char addr_a[16];
  strncpy(addr_a, inet_ntoa(addr), strlen(inet_ntoa(addr)));
  FILE *whitelist = fopen("whitelist", "r");
  char buf[BUFSIZ];
  while (fgets(buf, BUFSIZ, whitelist) != NULL) {
    if (buf[strlen(buf) - 1] == '\n')
      buf[strlen(buf) - 1] = '\0';
    if (strncmp(buf, addr_a, strlen(buf)) == 0)
      return true;
  }
  return false;
}


x86 assembly (clang, -O0):
<is_addr_vald>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    -0x20(%rbp),%rdi
call   1030 <strncpy@plt>
lea    0xe63(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1276 <is_addr_vald+0x116>
mov    -0x48(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1234 <is_addr_vald+0xd4>
mov    -0x48(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rdi
lea    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1271 <is_addr_vald+0x111>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    1284 <is_addr_vald+0x124>
jmp    11d9 <is_addr_vald+0x79>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_addr_vald>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
call   1050 <inet_ntoa@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    -0x40(%rbp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    0xe5e(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    %rsp,%r12
mov    0x2e25(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
jmp    11fa <is_addr_vald+0x9a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1230 <is_addr_vald+0xd0>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1237 <is_addr_vald+0xd7>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11e0 <is_addr_vald+0x80>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    11e0 <is_addr_vald+0x80>
mov    $0x1,%eax
jmp    1239 <is_addr_vald+0xd9>
xor    %eax,%eax
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@.str = [10 x i8] c"whitelist\00"
@.str.1 = [2 x i8] c"r\00"
@BUFSIZ = global i32 0
define i32 @is_addr_vald(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.in_addr
%4 = alloca [16 x i8]
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
store i32 %0, i32* %9
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i8* @inet_ntoa(i32 %12)
%14 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i8* @inet_ntoa(i32 %15)
%17 = call i32 @strlen(i8* %16)
%18 = call i32 @strncpy(i8* %10, i8* %13, i32 %17)
%19 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32, i32* @BUFSIZ
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %6
%23 = alloca i8, i64 %21
store i64 %21, i64* %7
br label %24
24: ; preds = %48, %1
%25 = load i32, i32* @BUFSIZ
%26 = load i32*, i32** %5
%27 = call i32* @fgets(i8* %23, i32 %25, i32* %26)
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %49
29: ; preds = %24
%30 = call i32 @strlen(i8* %23)
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %23, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %37, label %42
37: ; preds = %29
%38 = call i32 @strlen(i8* %23)
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %23, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %37, %29
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%44 = call i32 @strlen(i8* %23)
%45 = call i64 @strncmp(i8* %23, i8* %43, i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 1, i32* %2
store i32 1, i32* %8
br label %50
48: ; preds = %42
br label %24
49: ; preds = %24
store i32 0, i32* %2
store i32 1, i32* %8
br label %50
50: ; preds = %49, %47
%51 = load i8*, i8** %6
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@.str = [10 x i8] c"whitelist\00"
@.str.1 = [2 x i8] c"r\00"
@BUFSIZ = global i32 0
define i32 @is_addr_vald(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @inet_ntoa(i32 %0)
%5 = tail call i8* @inet_ntoa(i32 %0)
%6 = tail call i32 @strlen(i8* %5)
%7 = call i32 @strncpy(i8* %3, i8* %4, i32 %6)
%8 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
br label %13
13: ; preds = %29, %1
%14 = load i32, i32* @BUFSIZ
%15 = call i32* @fgets(i8* %12, i32 %14, i32* %8)
%16 = icmp eq i32* %15, null
br i1 %16, label %33, label %17
17: ; preds = %13
%18 = call i32 @strlen(i8* %12)
%19 = add nsw i32 %18, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %12, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = call i32 @strlen(i8* %12)
%26 = add nsw i32 %25, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %12, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %24, %17
%30 = call i32 @strlen(i8* %12)
%31 = call i64 @strncmp(i8* %12, i8* %3, i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %13
33: ; preds = %13, %29
%34 = phi i32 [ 1, %29 ], [ 0, %13 ]
ret i32 %34
}