#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 scalar_t__ uint32_t;
typedef int /*<<< orphan*/ template;
struct sock_fprog {
  int len;
  struct sock_filter *filter;
};
struct sock_filter {
  int member_0;
  int member_2;
  int member_3;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ ip;
typedef int /*<<< orphan*/ fprog;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ATTACH_FILTER;
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(struct sock_filter **, struct sock_filter *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct sock_fprog *, int);
int filterIP(int sock, uint32_t ip) {
  int i;
  uint32_t placeholder = 0xb7b7b7b7;
  uint32_t *lookhere;
  struct sock_filter template[] = {
      {0x28, 0, 0, 0x0000000c}, {0x15, 0, 5, 0x00000800},
      {0x20, 0, 0, 0x0000001e}, {0x15, 0, 3, placeholder},
      {0x30, 0, 0, 0x00000017}, {0x15, 0, 1, 0x00000006},
      {0x6, 0, 0, 0x0000ffff},  {0x6, 0, 0, 0x00000000},
  };
  struct sock_fprog fprog;
  struct sock_filter **fil;
  fil = (struct sock_filter **)malloc(sizeof(template));
  if (fil == NULL) {
    perror("can't allocate memory");
    exit(1);
  }
  memcpy(fil, template, sizeof(template));
  lookhere = (uint32_t *)fil;
  for (i = 0; i < (sizeof(template) - sizeof(ip)); i++) {
    if (lookhere[i] == placeholder) {
      lookhere[i] = ip;
    }
  }
  fprog.len = sizeof(template) / sizeof(template[0]);
  fprog.filter = (struct sock_filter *)fil;
  setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
  return (0);
}


x86 assembly (clang, -O0):
<filterIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xb7b7b7b7,%eax
mov    %rax,-0x20(%rbp)
movl   $0x28,-0xb0(%rbp)
movl   $0x0,-0xac(%rbp)
movl   $0x0,-0xa8(%rbp)
movl   $0xc,-0xa4(%rbp)
movl   $0x15,-0xa0(%rbp)
movl   $0x0,-0x9c(%rbp)
movl   $0x5,-0x98(%rbp)
movl   $0x800,-0x94(%rbp)
movl   $0x20,-0x90(%rbp)
movl   $0x0,-0x8c(%rbp)
movl   $0x0,-0x88(%rbp)
movl   $0x1e,-0x84(%rbp)
movl   $0x15,-0x80(%rbp)
movl   $0x0,-0x7c(%rbp)
movl   $0x3,-0x78(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0x74(%rbp)
movl   $0x30,-0x70(%rbp)
movl   $0x0,-0x6c(%rbp)
movl   $0x0,-0x68(%rbp)
movl   $0x17,-0x64(%rbp)
movl   $0x15,-0x60(%rbp)
movl   $0x0,-0x5c(%rbp)
movl   $0x1,-0x58(%rbp)
movl   $0x6,-0x54(%rbp)
movl   $0x6,-0x50(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0xffff,-0x44(%rbp)
movl   $0x6,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,-0xc8(%rbp)
cmpq   $0x0,-0xc8(%rbp)
jne    12a4 <filterIP+0x154>
lea    0xd6b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc8(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1040 <memcpy@plt>
mov    -0xc8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x78,%rax
jae    1315 <filterIP+0x1c5>
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
cmp    -0x20(%rbp),%rax
jne    1302 <filterIP+0x1b2>
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1307 <filterIP+0x1b7>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12ce <filterIP+0x17e>
movl   $0x8,-0xc0(%rbp)
mov    -0xc8(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2cb1(%rip),%rax
mov    (%rax),%esi
mov    0x2ca0(%rip),%rax
mov    (%rax),%edx
lea    -0xc0(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filterIP>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
movaps 0xe96(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe9a(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe9e(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xea2(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xea6(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xeaa(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xeae(%rip),%xmm0
movaps %xmm0,0x70(%rsp)
movaps 0xeb2(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
mov    $0x80,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     128e <filterIP+0x13e>
mov    %rax,%rbp
mov    $0xb7b7b7b7,%ebx
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
mov    $0x80,%edx
call   1040 <memcpy@plt>
mov    $0x2,%eax
jmp    120a <filterIP+0xba>
nopw   0x0(%rax,%rax,1)
add    $0x3,%rax
cmp    $0x7a,%rax
je     124f <filterIP+0xff>
cmp    %rbx,-0x10(%rbp,%rax,8)
je     1230 <filterIP+0xe0>
cmp    %rbx,-0x8(%rbp,%rax,8)
je     123c <filterIP+0xec>
cmp    %rbx,0x0(%rbp,%rax,8)
jne    1200 <filterIP+0xb0>
jmp    1248 <filterIP+0xf8>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,-0x10(%rbp,%rax,8)
cmp    %rbx,-0x8(%rbp,%rax,8)
jne    1218 <filterIP+0xc8>
mov    %r15,-0x8(%rbp,%rax,8)
cmp    %rbx,0x0(%rbp,%rax,8)
jne    1200 <filterIP+0xb0>
mov    %r15,0x0(%rbp,%rax,8)
jmp    1200 <filterIP+0xb0>
movl   $0x8,(%rsp)
mov    %rbp,0x8(%rsp)
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %r14d,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sock_filter = type { i32, i32, i32, i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@.str = [22 x i8] c"can't allocate memory\00"
@SOL_SOCKET = global i32 0
@SO_ATTACH_FILTER = global i32 0
define i32 @filterIP(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64*
%8 = alloca [8 x %struct.sock_filter]
%9 = alloca %struct.sock_fprog
%10 = alloca %struct.sock_filter**
store i32 %0, i32* %3
store i64 %1, i64* %4
store i64 3082270647, i64* %6
%11 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %8, i64 0, i64 0
%12 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %11, i32 0, i32 0
store i32 40, i32* %12
%13 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %11, i32 0, i32 1
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %11, i32 0, i32 2
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %11, i32 0, i32 3
store i32 12, i32* %15
%16 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %11, i64 1
%17 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %16, i32 0, i32 0
store i32 21, i32* %17
%18 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %16, i32 0, i32 1
store i32 0, i32* %18
%19 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %16, i32 0, i32 2
store i32 5, i32* %19
%20 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %16, i32 0, i32 3
store i32 2048, i32* %20
%21 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %16, i64 1
%22 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %21, i32 0, i32 0
store i32 32, i32* %22
%23 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %21, i32 0, i32 1
store i32 0, i32* %23
%24 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %21, i32 0, i32 2
store i32 0, i32* %24
%25 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %21, i32 0, i32 3
store i32 30, i32* %25
%26 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %21, i64 1
%27 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 0
store i32 21, i32* %27
%28 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 1
store i32 0, i32* %28
%29 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 2
store i32 3, i32* %29
%30 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 3
%31 = load i64, i64* %6
%32 = trunc i64 %31 to i32
store i32 %32, i32* %30
%33 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i64 1
%34 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %33, i32 0, i32 0
store i32 48, i32* %34
%35 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %33, i32 0, i32 1
store i32 0, i32* %35
%36 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %33, i32 0, i32 2
store i32 0, i32* %36
%37 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %33, i32 0, i32 3
store i32 23, i32* %37
%38 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %33, i64 1
%39 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %38, i32 0, i32 0
store i32 21, i32* %39
%40 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %38, i32 0, i32 1
store i32 0, i32* %40
%41 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %38, i32 0, i32 2
store i32 1, i32* %41
%42 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %38, i32 0, i32 3
store i32 6, i32* %42
%43 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %38, i64 1
%44 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %43, i32 0, i32 0
store i32 6, i32* %44
%45 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %43, i32 0, i32 1
store i32 0, i32* %45
%46 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %43, i32 0, i32 2
store i32 0, i32* %46
%47 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %43, i32 0, i32 3
store i32 65535, i32* %47
%48 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %43, i64 1
%49 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %48, i32 0, i32 0
store i32 6, i32* %49
%50 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %48, i32 0, i32 1
store i32 0, i32* %50
%51 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %48, i32 0, i32 2
store i32 0, i32* %51
%52 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %48, i32 0, i32 3
store i32 0, i32* %52
%53 = call i64 @malloc(i32 128)
%54 = inttoptr i64 %53 to %struct.sock_filter**
store %struct.sock_filter** %54, %struct.sock_filter*** %10
%55 = load %struct.sock_filter**, %struct.sock_filter*** %10
%56 = icmp eq %struct.sock_filter** %55, null
br i1 %56, label %57, label %60
57: ; preds = %2
%58 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %2
%61 = load %struct.sock_filter**, %struct.sock_filter*** %10
%62 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %8, i64 0, i64 0
%63 = call i32 @memcpy(%struct.sock_filter** %61, %struct.sock_filter* %62, i32 128)
%64 = load %struct.sock_filter**, %struct.sock_filter*** %10
%65 = bitcast %struct.sock_filter** %64 to i64*
store i64* %65, i64** %7
store i32 0, i32* %5
br label %66
66: ; preds = %85, %60
%67 = load i32, i32* %5
%68 = sext i32 %67 to i64
%69 = icmp ult i64 %68, 120
br i1 %69, label %70, label %88
70: ; preds = %66
%71 = load i64*, i64** %7
%72 = load i32, i32* %5
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i64, i64* %71, i64 %73
%75 = load i64, i64* %74
%76 = load i64, i64* %6
%77 = icmp eq i64 %75, %76
br i1 %77, label %78, label %84
78: ; preds = %70
%79 = load i64, i64* %4
%80 = load i64*, i64** %7
%81 = load i32, i32* %5
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i64, i64* %80, i64 %82
store i64 %79, i64* %83
br label %84
84: ; preds = %78, %70
br label %85
85: ; preds = %84
%86 = load i32, i32* %5
%87 = add nsw i32 %86, 1
store i32 %87, i32* %5
br label %66
88: ; preds = %66
%89 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %9, i32 0, i32 0
store i32 8, i32* %89
%90 = load %struct.sock_filter**, %struct.sock_filter*** %10
%91 = bitcast %struct.sock_filter** %90 to %struct.sock_filter*
%92 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %9, i32 0, i32 1
store %struct.sock_filter* %91, %struct.sock_filter** %92
%93 = load i32, i32* %3
%94 = load i32, i32* @SOL_SOCKET
%95 = load i32, i32* @SO_ATTACH_FILTER
%96 = call i32 @setsockopt(i32 %93, i32 %94, i32 %95, %struct.sock_fprog* %9, i32 16)
ret i32 0
}

LLVM IR (-O2):
%struct.sock_filter = type { i32, i32, i32, i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@.str = [22 x i8] c"can't allocate memory\00"
@SOL_SOCKET = global i32 0
@SO_ATTACH_FILTER = global i32 0
define i32 @filterIP(i32 %0, i64 %1) {
%3 = alloca [8 x %struct.sock_filter]
%4 = alloca %struct.sock_fprog
%5 = bitcast [8 x %struct.sock_filter]* %3 to i8*
%6 = bitcast [8 x %struct.sock_filter]* %3 to <4 x i32>*
store <4 x i32> <i32 40, i32 0, i32 0, i32 12>, <4 x i32>* %6
%7 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 1, i32 0
%8 = bitcast i32* %7 to <4 x i32>*
store <4 x i32> <i32 21, i32 0, i32 5, i32 2048>, <4 x i32>* %8
%9 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 2, i32 0
%10 = bitcast i32* %9 to <4 x i32>*
store <4 x i32> <i32 32, i32 0, i32 0, i32 30>, <4 x i32>* %10
%11 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 3, i32 0
%12 = bitcast i32* %11 to <4 x i32>*
store <4 x i32> <i32 21, i32 0, i32 3, i32 -1212696649>, <4 x i32>* %12
%13 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 4, i32 0
%14 = bitcast i32* %13 to <4 x i32>*
store <4 x i32> <i32 48, i32 0, i32 0, i32 23>, <4 x i32>* %14
%15 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 5, i32 0
%16 = bitcast i32* %15 to <4 x i32>*
store <4 x i32> <i32 21, i32 0, i32 1, i32 6>, <4 x i32>* %16
%17 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 6, i32 0
%18 = bitcast i32* %17 to <4 x i32>*
store <4 x i32> <i32 6, i32 0, i32 0, i32 65535>, <4 x i32>* %18
%19 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 7, i32 0
%20 = bitcast i32* %19 to <4 x i32>*
store <4 x i32> <i32 6, i32 0, i32 0, i32 0>, <4 x i32>* %20
%21 = bitcast %struct.sock_fprog* %4 to i8*
%22 = tail call i64 @malloc(i32 128)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = inttoptr i64 %22 to %struct.sock_filter**
%29 = getelementptr inbounds [8 x %struct.sock_filter], [8 x %struct.sock_filter]* %3, i64 0, i64 0
%30 = call i32 @memcpy(%struct.sock_filter** %28, %struct.sock_filter* %29, i32 128)
%31 = inttoptr i64 %22 to i64*
br label %32
32: ; preds = %50, %27
%33 = phi i64 [ 0, %27 ], [ %51, %50 ]
%34 = getelementptr inbounds i64, i64* %31, i64 %33
%35 = load i64, i64* %34
%36 = icmp eq i64 %35, 3082270647
br i1 %36, label %37, label %38
37: ; preds = %32
store i64 %1, i64* %34
br label %38
38: ; preds = %32, %37
%39 = add nuw nsw i64 %33, 1
%40 = getelementptr inbounds i64, i64* %31, i64 %39
%41 = load i64, i64* %40
%42 = icmp eq i64 %41, 3082270647
br i1 %42, label %43, label %44
43: ; preds = %38
store i64 %1, i64* %40
br label %44
44: ; preds = %43, %38
%45 = add nuw nsw i64 %33, 2
%46 = getelementptr inbounds i64, i64* %31, i64 %45
%47 = load i64, i64* %46
%48 = icmp eq i64 %47, 3082270647
br i1 %48, label %49, label %50
49: ; preds = %44
store i64 %1, i64* %46
br label %50
50: ; preds = %49, %44
%51 = add nuw nsw i64 %33, 3
%52 = icmp eq i64 %51, 120
br i1 %52, label %53, label %32
53: ; preds = %50
%54 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i64 0, i32 0
store i32 8, i32* %54
%55 = inttoptr i64 %22 to %struct.sock_filter*
%56 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i64 0, i32 1
store %struct.sock_filter* %55, %struct.sock_filter** %56
%57 = load i32, i32* @SOL_SOCKET
%58 = load i32, i32* @SO_ATTACH_FILTER
%59 = call i32 @setsockopt(i32 %0, i32 %57, i32 %58, %struct.sock_fprog* %4, i32 16)
ret i32 0
}