#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 u_char;
typedef int /*<<< orphan*/ prefix;
typedef int in_addr_t;

/* Variables and functions */
int first_n_bit_mask(int);
int /*<<< orphan*/ hmac_md5(int *, int, int *, int *);
void *htonl(int);
int ntohl(int);
in_addr_t anonymize(in_addr_t a, u_char *anon_key) {
  a = ntohl(a);
  struct {
    in_addr_t prefix;
    int len;
  } prefix;
  in_addr_t output = 0;
  int i;
  for (i = 0; i < 32; ++i) {
    prefix.prefix = htonl(a & first_n_bit_mask(i));
    prefix.len = htonl(i);
    u_char digest[16];
    if (anon_key == NULL) {
      u_char key[16];
      anon_key = key;
    }
    hmac_md5(anon_key, sizeof(prefix), (u_char *)(&prefix), digest);
    int flip = digest[0] & 1;
    in_addr_t bit_mask = 1 << (31 - i);
    output = output | ((flip << (31 - i)) ^ (a & bit_mask));
  }
  return output;
}


x86 assembly (clang, -O0):
<anonymize>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <ntohl@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x20,-0x20(%rbp)
jge    1221 <anonymize+0xe1>
mov    -0x4(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <first_n_bit_mask@plt>
mov    -0xac(%rbp),%edi
and    %eax,%edi
call   1040 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <anonymize+0x77>
lea    -0xa0(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rdx
lea    -0x60(%rbp),%rcx
mov    $0x8,%esi
call   1030 <hmac_md5@plt>
mov    -0x60(%rbp),%eax
and    $0x1,%eax
mov    %eax,-0xa4(%rbp)
mov    $0x1f,%ecx
sub    -0x20(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0xa8(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xa4(%rbp),%edx
mov    $0x1f,%ecx
sub    -0x20(%rbp),%ecx
shl    %cl,%edx
mov    %edx,%ecx
mov    -0x4(%rbp),%edx
and    -0xa8(%rbp),%edx
xor    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116b <anonymize+0x2b>
mov    -0x1c(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anonymize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%rbx
call   1060 <ntohl@plt>
mov    %eax,%r14d
xor    %ebp,%ebp
mov    $0x1f,%r15d
lea    0x10(%rsp),%r13
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %ebp,%edi
call   1050 <first_n_bit_mask@plt>
and    %r14d,%eax
mov    %eax,%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
test   %rbx,%rbx
lea    0x50(%rsp),%rax
cmove  %rax,%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
lea    0x8(%rsp),%rdx
mov    %r13,%rcx
call   1030 <hmac_md5@plt>
mov    0x10(%rsp),%eax
mov    $0x80000000,%edx
mov    %ebp,%ecx
shr    %cl,%edx
and    $0x1,%eax
mov    %r15d,%ecx
shl    %cl,%eax
and    %r14d,%edx
xor    %eax,%edx
or     %edx,%r12d
add    $0x1,%ebp
add    $0xffffffff,%r15d
cmp    $0x20,%ebp
jne    1170 <anonymize+0x30>
mov    %r12d,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.anon = type { i32, i32 }
define i32 @anonymize(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.anon
%6 = alloca i32
%7 = alloca i32
%8 = alloca [16 x i32]
%9 = alloca [16 x i32]
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%12 = load i32, i32* %3
%13 = call i32 @ntohl(i32 %12)
store i32 %13, i32* %3
store i32 0, i32* %6
store i32 0, i32* %7
br label %14
14: ; preds = %54, %2
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 32
br i1 %16, label %17, label %57
17: ; preds = %14
%18 = load i32, i32* %3
%19 = load i32, i32* %7
%20 = call i32 @first_n_bit_mask(i32 %19)
%21 = and i32 %18, %20
%22 = call i8* @htonl(i32 %21)
%23 = ptrtoint i8* %22 to i32
%24 = getelementptr inbounds %struct.anon, %struct.anon* %5, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %7
%26 = call i8* @htonl(i32 %25)
%27 = ptrtoint i8* %26 to i32
%28 = getelementptr inbounds %struct.anon, %struct.anon* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32*, i32** %4
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %33
31: ; preds = %17
%32 = getelementptr inbounds [16 x i32], [16 x i32]* %9, i64 0, i64 0
store i32* %32, i32** %4
br label %33
33: ; preds = %31, %17
%34 = load i32*, i32** %4
%35 = bitcast %struct.anon* %5 to i32*
%36 = getelementptr inbounds [16 x i32], [16 x i32]* %8, i64 0, i64 0
%37 = call i32 @hmac_md5(i32* %34, i32 8, i32* %35, i32* %36)
%38 = getelementptr inbounds [16 x i32], [16 x i32]* %8, i64 0, i64 0
%39 = load i32, i32* %38
%40 = and i32 %39, 1
store i32 %40, i32* %10
%41 = load i32, i32* %7
%42 = sub nsw i32 31, %41
%43 = shl i32 1, %42
store i32 %43, i32* %11
%44 = load i32, i32* %6
%45 = load i32, i32* %10
%46 = load i32, i32* %7
%47 = sub nsw i32 31, %46
%48 = shl i32 %45, %47
%49 = load i32, i32* %3
%50 = load i32, i32* %11
%51 = and i32 %49, %50
%52 = xor i32 %48, %51
%53 = or i32 %44, %52
store i32 %53, i32* %6
br label %54
54: ; preds = %33
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %14
57: ; preds = %14
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
%struct.anon = type { i32, i32 }
define i32 @anonymize(i32 %0, i32* %1) {
%3 = alloca %struct.anon
%4 = alloca [16 x i32]
%5 = alloca [16 x i32]
%6 = tail call i32 @ntohl(i32 %0)
%7 = bitcast %struct.anon* %3 to i8*
%8 = getelementptr inbounds %struct.anon, %struct.anon* %3, i64 0, i32 0
%9 = getelementptr inbounds %struct.anon, %struct.anon* %3, i64 0, i32 1
%10 = bitcast [16 x i32]* %4 to i8*
%11 = getelementptr inbounds [16 x i32], [16 x i32]* %5, i64 0, i64 0
%12 = getelementptr inbounds [16 x i32], [16 x i32]* %4, i64 0, i64 0
br label %13
13: ; preds = %2, %13
%14 = phi i32* [ %1, %2 ], [ %26, %13 ]
%15 = phi i32 [ 0, %2 ], [ %36, %13 ]
%16 = phi i32 [ 0, %2 ], [ %35, %13 ]
%17 = call i32 @first_n_bit_mask(i32 %15)
%18 = and i32 %17, %6
%19 = call i8* @htonl(i32 %18)
%20 = ptrtoint i8* %19 to i64
%21 = trunc i64 %20 to i32
store i32 %21, i32* %8
%22 = call i8* @htonl(i32 %15)
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
store i32 %24, i32* %9
%25 = icmp eq i32* %14, null
%26 = select i1 %25, i32* %11, i32* %14
%27 = call i32 @hmac_md5(i32* %26, i32 8, i32* %8, i32* %12)
%28 = load i32, i32* %12
%29 = and i32 %28, 1
%30 = sub nuw nsw i32 31, %15
%31 = lshr i32 -2147483648, %15
%32 = shl nuw i32 %29, %30
%33 = and i32 %31, %6
%34 = xor i32 %32, %33
%35 = or i32 %34, %16
%36 = add nuw nsw i32 %15, 1
%37 = icmp eq i32 %36, 32
br i1 %37, label %38, label %13
38: ; preds = %13
ret i32 %35
}