#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 /*<<< orphan*/ xkey;

/* Variables and functions */
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, unsigned int);
int wep_decrypt_packet(unsigned char *, unsigned int, int,
                       unsigned char const *);
unsigned test_wep_decrypt(const unsigned char *px, unsigned length,
                          unsigned char *new_px, unsigned *r_new_length) {
  unsigned i;
  struct XKey {
    unsigned len;
    const char *key;
  } xkey[] = {
      {40, "\x12\x34\x56\x19\x72"},
      {40, "\x77\x03\x90\x08\x59"},
      {40, "\x69\x93\x71\x62\x53"},
      {40, "\x11\x28\x52\x46\x14"},
      {104, "\xa3\x42\xee\x54\xc1\x2e\x5d\x23\x1f\xfe\xe0\x02\x00"},
      {40, "\x11\x11\x11\x11\x11"},
      {40, "\x4b\x78\x52\xd7\x80"},
      {40, "\x00\x00\x00\x00\x00"},
      {40, "\x12\x34\x56\x78\x90"},
      {104, "\x10\x01\x11\x11\x00\x00\x11\x11\x00\x00\x11\x11\x01"},
      {104, "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90"},
  };
  int foo;
  if (length > 4)
    *r_new_length = length - 4;
  else
    return 0;
  for (i = 0; i < sizeof(xkey) / sizeof(xkey[0]); i++) {
    memcpy(new_px, px, length);
    foo = wep_decrypt_packet(new_px + 24, length - 24, xkey[i].len / 8,
                             (const unsigned char *)xkey[i].key);
    if (foo == 0)
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_wep_decrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0xe0(%rbp),%rdi
lea    0x2c28(%rip),%rsi
mov    $0xb0,%edx
call   1030 <memcpy@plt>
cmpl   $0x4,-0x14(%rbp)
jbe    116d <test_wep_decrypt+0x4d>
mov    -0x14(%rbp),%ecx
sub    $0x4,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1179 <test_wep_decrypt+0x59>
movl   $0x0,-0x4(%rbp)
jmp    1218 <test_wep_decrypt+0xf8>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    $0xb,%rax
jae    1211 <test_wep_decrypt+0xf1>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rdi
add    $0x18,%rdi
mov    -0x14(%rbp),%esi
sub    $0x18,%esi
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
lea    -0xe0(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%edx
shr    $0x3,%edx
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
lea    -0xe0(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rcx
call   1040 <wep_decrypt_packet@plt>
mov    %eax,-0xe4(%rbp)
cmpl   $0x0,-0xe4(%rbp)
jne    11fe <test_wep_decrypt+0xde>
movl   $0x1,-0x4(%rbp)
jmp    1218 <test_wep_decrypt+0xf8>
jmp    1203 <test_wep_decrypt+0xe3>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1180 <test_wep_decrypt+0x60>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_wep_decrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,(%rsp)
xor    %eax,%eax
cmp    $0x5,%esi
jb     119e <test_wep_decrypt+0x7e>
mov    %rdx,%r12
mov    %esi,%ebp
lea    -0x4(%rbp),%eax
mov    %eax,(%rcx)
lea    0x18(%rdx),%r13
lea    -0x18(%rbp),%ebx
mov    $0x8,%r14d
lea    0x2c1c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    (%rsp),%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
mov    -0x8(%r14,%r15,1),%edx
shr    $0x3,%edx
mov    (%r14,%r15,1),%rcx
mov    %r13,%rdi
mov    %ebx,%esi
call   1040 <wep_decrypt_packet@plt>
test   %eax,%eax
je     1199 <test_wep_decrypt+0x79>
add    $0x10,%r14
cmp    $0xb8,%r14
jne    1160 <test_wep_decrypt+0x40>
xor    %eax,%eax
jmp    119e <test_wep_decrypt+0x7e>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.XKey = type { i32, i8* }
@.str = [6 x i8] c"\124V\19r\00"
@.str.1 = [6 x i8] c"w\03\90\08Y\00"
@.str.2 = [6 x i8] c"i\93qbS\00"
@.str.3 = [6 x i8] c"\11(RF\14\00"
@.str.4 = [14 x i8] c"\A3B\EET\C1.]#\1F\FE\E0\02\00\00"
@.str.5 = [6 x i8] c"\11\11\11\11\11\00"
@.str.6 = [6 x i8] c"KxR\D7\80\00"
@.str.7 = [6 x i8] zeroinitializer
@.str.8 = [6 x i8] c"\124Vx\90\00"
@.str.9 = [14 x i8] c"\10\01\11\11\00\00\11\11\00\00\11\11\01\00"
@.str.10 = [14 x i8] c"\124Vx\90\AB\CD\EF\124Vx\90\00"
@__const.test_wep_decrypt.xkey = [11 x %struct.XKey] [%struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0) }, %struct.XKey { i32 104, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i32 0, i32 0) }, %struct.XKey { i32 104, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i32 0, i32 0) }, %struct.XKey { i32 104, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i32 0, i32 0) }]
define i32 @test_wep_decrypt(i8* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [11 x %struct.XKey]
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32* %3, i32** %9
%13 = bitcast [11 x %struct.XKey]* %11 to i8*
%14 = load i32, i32* %7
%15 = icmp ugt i32 %14, 4
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = load i32, i32* %7
%18 = sub i32 %17, 4
%19 = load i32*, i32** %9
store i32 %18, i32* %19
br label %21
20: ; preds = %4
store i32 0, i32* %5
br label %55
21: ; preds = %16
store i32 0, i32* %10
br label %22
22: ; preds = %51, %21
%23 = load i32, i32* %10
%24 = zext i32 %23 to i64
%25 = icmp ult i64 %24, 11
br i1 %25, label %26, label %54
26: ; preds = %22
%27 = load i8*, i8** %8
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 24
%33 = load i32, i32* %7
%34 = sub i32 %33, 24
%35 = load i32, i32* %10
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [11 x %struct.XKey], [11 x %struct.XKey]* %11, i64 0, i64 %36
%38 = getelementptr inbounds %struct.XKey, %struct.XKey* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = udiv i32 %39, 8
%41 = load i32, i32* %10
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds [11 x %struct.XKey], [11 x %struct.XKey]* %11, i64 0, i64 %42
%44 = getelementptr inbounds %struct.XKey, %struct.XKey* %43, i32 0, i32 1
%45 = load i8*, i8** %44
%46 = call i32 @wep_decrypt_packet(i8* %32, i32 %34, i32 %40, i8* %45)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %26
store i32 1, i32* %5
br label %55
50: ; preds = %26
br label %51
51: ; preds = %50
%52 = load i32, i32* %10
%53 = add i32 %52, 1
store i32 %53, i32* %10
br label %22
54: ; preds = %22
store i32 0, i32* %5
br label %55
55: ; preds = %54, %49, %20
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
%struct.XKey = type { i32, i8* }
@.str = [6 x i8] c"\124V\19r\00"
@.str.1 = [6 x i8] c"w\03\90\08Y\00"
@.str.2 = [6 x i8] c"i\93qbS\00"
@.str.3 = [6 x i8] c"\11(RF\14\00"
@.str.4 = [14 x i8] c"\A3B\EET\C1.]#\1F\FE\E0\02\00\00"
@.str.5 = [6 x i8] c"\11\11\11\11\11\00"
@.str.6 = [6 x i8] c"KxR\D7\80\00"
@.str.7 = [6 x i8] zeroinitializer
@.str.8 = [6 x i8] c"\124Vx\90\00"
@.str.9 = [14 x i8] c"\10\01\11\11\00\00\11\11\00\00\11\11\01\00"
@.str.10 = [14 x i8] c"\124Vx\90\AB\CD\EF\124Vx\90\00"
@__const.test_wep_decrypt.xkey = [11 x %struct.XKey] [%struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i32 0, i32 0) }, %struct.XKey { i32 104, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i32 0, i32 0) }, %struct.XKey { i32 40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i32 0, i32 0) }, %struct.XKey { i32 104, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i32 0, i32 0) }, %struct.XKey { i32 104, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i32 0, i32 0) }]
define i32 @test_wep_decrypt(i8* %0, i32 %1, i8* %2, i32* nocapture writeonly %3) {
%5 = icmp ugt i32 %1, 4
br i1 %5, label %6, label %23
6: ; preds = %4
%7 = add i32 %1, -4
store i32 %7, i32* %3
%8 = getelementptr inbounds i8, i8* %2, i64 24
%9 = add i32 %1, -24
br label %13
10: ; preds = %13
%11 = add nuw nsw i64 %14, 1
%12 = icmp eq i64 %11, 11
br i1 %12, label %23, label %13
13: ; preds = %6, %10
%14 = phi i64 [ 0, %6 ], [ %11, %10 ]
%15 = tail call i32 @memcpy(i8* %2, i8* %0, i32 %1)
%16 = getelementptr inbounds [11 x %struct.XKey], [11 x %struct.XKey]* @__const.test_wep_decrypt.xkey, i64 0, i64 %14, i32 0
%17 = load i32, i32* %16
%18 = lshr i32 %17, 3
%19 = getelementptr inbounds [11 x %struct.XKey], [11 x %struct.XKey]* @__const.test_wep_decrypt.xkey, i64 0, i64 %14, i32 1
%20 = load i8*, i8** %19
%21 = tail call i32 @wep_decrypt_packet(i8* %8, i32 %9, i32 %18, i8* %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %10
23: ; preds = %10, %13, %4
%24 = phi i32 [ 0, %4 ], [ 0, %10 ], [ 1, %13 ]
ret i32 %24
}