#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 */

/* Variables and functions */
int luasocket_ip_is_private(char *);
int /*<<< orphan*/ printf(char *, char const *, ...);
int sscanf(char const *, char *, char *, char *, char *, char *);
scalar_t__ strcmp(char const *, char *);
int luasocket_ip_allowed(const char *hoststr) {
  if (!hoststr)
    return 1;
  if (strcmp(hoststr, "localhost") == 0) {
    return 0;
  }
  char ip[4] = {0, 0, 0, 0};
  int parse_res =
      sscanf(hoststr, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
  if (parse_res != 4) {
    printf("unable to parse IP: %s - DENIED\n", hoststr);
    return 2;
  }
  int res = luasocket_ip_is_private(ip);
  printf("%s [%d.%d.%d.%d] - %s\n", hoststr, ip[0], ip[1], ip[2], ip[3],
         res == 0 ? "ALLOWED" : "DENIED");
  return res;
}


x86 assembly (clang, -O0):
<luasocket_ip_allowed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <luasocket_ip_allowed+0x23>
movl   $0x1,-0x4(%rbp)
jmp    1254 <luasocket_ip_allowed+0x104>
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1199 <luasocket_ip_allowed+0x49>
movl   $0x0,-0x4(%rbp)
jmp    1254 <luasocket_ip_allowed+0x104>
lea    -0x14(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rdx
lea    -0x14(%rbp),%rcx
add    $0x1,%rcx
lea    -0x14(%rbp),%r8
add    $0x2,%r8
lea    -0x14(%rbp),%r9
add    $0x3,%r9
lea    0xe3a(%rip),%rsi
call   1070 <sscanf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
je     1200 <luasocket_ip_allowed+0xb0>
mov    -0x10(%rbp),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1254 <luasocket_ip_allowed+0x104>
lea    -0x14(%rbp),%rdi
call   1030 <luasocket_ip_is_private@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
movsbl -0x14(%rbp),%edx
movsbl -0x13(%rbp),%ecx
movsbl -0x12(%rbp),%r8d
movsbl -0x11(%rbp),%r9d
mov    -0x1c(%rbp),%r10d
lea    0xe29(%rip),%rax
lea    0xe1a(%rip),%rdi
cmp    $0x0,%r10d
cmove  %rdi,%rax
lea    0xdf4(%rip),%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<luasocket_ip_allowed>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     11e4 <luasocket_ip_allowed+0xa4>
mov    %rdi,%rbx
lea    0xea7(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11eb <luasocket_ip_allowed+0xab>
movl   $0x0,0x14(%rsp)
lea    0x15(%rsp),%rcx
lea    0x16(%rsp),%r8
lea    0x17(%rsp),%r9
lea    0xe85(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <sscanf@plt>
cmp    $0x4,%eax
jne    11ef <luasocket_ip_allowed+0xaf>
lea    0x14(%rsp),%rdi
call   1030 <luasocket_ip_is_private@plt>
mov    %eax,%ebp
movsbl 0x14(%rsp),%edx
movsbl 0x15(%rsp),%ecx
movsbl 0x16(%rsp),%r8d
movsbl 0x17(%rsp),%r9d
test   %eax,%eax
lea    0xe8c(%rip),%rax
lea    0xe8d(%rip),%rsi
cmove  %rax,%rsi
mov    %rsi,(%rsp)
lea    0xe5f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1205 <luasocket_ip_allowed+0xc5>
mov    $0x1,%ebp
jmp    1205 <luasocket_ip_allowed+0xc5>
xor    %ebp,%ebp
jmp    1205 <luasocket_ip_allowed+0xc5>
lea    0xe20(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"localhost\00"
@.str.1 = [12 x i8] c"%d.%d.%d.%d\00"
@.str.2 = [33 x i8] c"unable to parse IP: %s - DENIED\0A\00"
@.str.3 = [23 x i8] c"%s [%d.%d.%d.%d] - %s\0A\00"
@.str.4 = [8 x i8] c"ALLOWED\00"
@.str.5 = [7 x i8] c"DENIED\00"
define i32 @luasocket_ip_allowed(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [4 x i8]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 1, i32* %2
br label %50
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 0, i32* %2
br label %50
15: ; preds = %10
%16 = bitcast [4 x i8]* %4 to i8*
%17 = load i8*, i8** %3
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%22 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %19, i8* %20, i8* %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp ne i32 %23, 4
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = load i8*, i8** %3
%27 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 2, i32* %2
br label %50
28: ; preds = %15
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%30 = call i32 @luasocket_ip_is_private(i8* %29)
store i32 %30, i32* %6
%31 = load i8*, i8** %3
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, 0
%46 = zext i1 %45 to i64
%47 = select i1 %45, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0)
%48 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %31, i32 %34, i32 %37, i32 %40, i32 %43, i8* %47)
%49 = load i32, i32* %6
store i32 %49, i32* %2
br label %50
50: ; preds = %28, %25, %14, %9
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@.str = [10 x i8] c"localhost\00"
@.str.1 = [12 x i8] c"%d.%d.%d.%d\00"
@.str.2 = [33 x i8] c"unable to parse IP: %s - DENIED\0A\00"
@.str.3 = [23 x i8] c"%s [%d.%d.%d.%d] - %s\0A\00"
@.str.4 = [8 x i8] c"ALLOWED\00"
@.str.5 = [7 x i8] c"DENIED\00"
define i32 @luasocket_ip_allowed(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to [4 x i8]*
%4 = icmp eq i8* %0, null
br i1 %4, label %32, label %5
5: ; preds = %1
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %32, label %8
8: ; preds = %5
%9 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
%13 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %10, i8* %11, i8* %12)
%14 = icmp eq i32 %13, 4
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %30
17: ; preds = %8
%18 = call i32 @luasocket_ip_is_private(i8* %9)
%19 = load i8, i8* %9
%20 = sext i8 %19 to i32
%21 = load i8, i8* %10
%22 = sext i8 %21 to i32
%23 = load i8, i8* %11
%24 = sext i8 %23 to i32
%25 = load i8, i8* %12
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %18, 0
%28 = select i1 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0)
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %20, i32 %22, i32 %24, i32 %26, i8* %28)
br label %30
30: ; preds = %17, %15
%31 = phi i32 [ 2, %15 ], [ %18, %17 ]
br label %32
32: ; preds = %5, %1, %30
%33 = phi i32 [ %31, %30 ], [ 1, %1 ], [ 0, %5 ]
ret i32 %33
}