#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 /*<<< orphan*/ printf(char *, int, int);
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, char *, int);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
scalar_t__ validate_hex_pattern(char *);
int process_pattern_4_hex(char *pattern, int *ilen, unsigned int *pat_le,
                          unsigned int *pat_be) {
  int len;
  int pat;
  if (validate_hex_pattern(pattern) < 0)
    return -1;
  len = (int)strlen(pattern);
  if ((strncmp(pattern, "0x", 2) == 0) || (strncmp(pattern, "0X", 2) == 0)) {
    pat = (int)strtol(pattern, NULL, 0);
    len -= 2;
  } else {
    pat = (int)strtol(pattern, NULL, 16);
  }
  switch (len) {
  case 1:
  case 2:
    *ilen = 1;
    *pat_le = (unsigned int)pat;
    *pat_be = (unsigned int)pat;
    break;
  case 3:
  case 4:
    *ilen = 2;
    *pat_le = (unsigned int)pat;
    *pat_be = (((((unsigned int)pat) & 0xFF00) >> 8) |
               ((((unsigned int)pat) & 0x00FF) << 8));
    break;
  default:
    *ilen = 4;
    *pat_le = (unsigned int)pat;
    *pat_be = (((((unsigned int)pat) & 0x00FF0000) >> 8) |
               ((((unsigned int)pat) & 0xFF000000) >> 24) |
               ((((unsigned int)pat) & 0x0000FF00) << 8) |
               ((((unsigned int)pat) & 0x000000FF) << 24));
    break;
  }
  printf(" Hex pattern: \'0x%x\' width \'%d\'\n", pat, len);
  return len;
}


x86 assembly (clang, -O0):
<process_pattern_4_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <validate_hex_pattern@plt>
cmp    $0x0,%rax
jge    1187 <process_pattern_4_hex+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <process_pattern_4_hex+0x19d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe62(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11d1 <process_pattern_4_hex+0x81>
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f1 <process_pattern_4_hex+0xa1>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x2c(%rbp)
jmp    1206 <process_pattern_4_hex+0xb6>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
add    $0xffffffff,%eax
sub    $0x2,%eax
jb     1231 <process_pattern_4_hex+0xe1>
jmp    121d <process_pattern_4_hex+0xcd>
mov    -0x34(%rbp),%eax
add    $0xfffffffd,%eax
sub    $0x2,%eax
jb     1252 <process_pattern_4_hex+0x102>
jmp    1289 <process_pattern_4_hex+0x139>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    12d5 <process_pattern_4_hex+0x185>
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
and    $0xff00,%ecx
shr    $0x8,%ecx
mov    -0x30(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
or     %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    12d5 <process_pattern_4_hex+0x185>
mov    -0x18(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
and    $0xff0000,%ecx
shr    $0x8,%ecx
mov    -0x30(%rbp),%eax
and    $0xff000000,%eax
shr    $0x18,%eax
or     %eax,%ecx
mov    -0x30(%rbp),%eax
and    $0xff00,%eax
shl    $0x8,%eax
or     %eax,%ecx
mov    -0x30(%rbp),%eax
and    $0xff,%eax
shl    $0x18,%eax
or     %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xd24(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_pattern_4_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
call   1070 <validate_hex_pattern@plt>
test   %rax,%rax
js     11c0 <process_pattern_4_hex+0x70>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11c8 <process_pattern_4_hex+0x78>
lea    0xe69(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11c8 <process_pattern_4_hex+0x78>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,%rsi
jmp    11db <process_pattern_4_hex+0x8b>
mov    $0xffffffff,%r13d
jmp    1233 <process_pattern_4_hex+0xe3>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %rax,%rsi
add    $0xfffffffe,%r13d
lea    -0x3(%r13),%eax
cmp    $0x2,%eax
jb     11fc <process_pattern_4_hex+0xac>
lea    -0x1(%r13),%eax
cmp    $0x1,%eax
ja     1212 <process_pattern_4_hex+0xc2>
movl   $0x1,(%r12)
mov    %esi,(%r15)
mov    %esi,%eax
jmp    1221 <process_pattern_4_hex+0xd1>
movl   $0x2,(%r12)
mov    %esi,(%r15)
mov    %esi,%eax
rol    $0x8,%ax
movzwl %ax,%eax
jmp    1221 <process_pattern_4_hex+0xd1>
movl   $0x4,(%r12)
mov    %esi,(%r15)
mov    %esi,%eax
bswap  %eax
mov    %eax,(%r14)
lea    0xddb(%rip),%rdi
mov    %r13d,%edx
call   1050 <printf@plt>
mov    %r13d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"0X\00"
@.str.2 = [33 x i8] c" Hex pattern: '0x%x' width '%d'\0A\00"
define i32 @process_pattern_4_hex(i8* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%12 = load i8*, i8** %6
%13 = call i64 @validate_hex_pattern(i8* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %82
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = call i64 @strlen(i8* %17)
%19 = trunc i64 %18 to i32
store i32 %19, i32* %10
%20 = load i8*, i8** %6
%21 = call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = load i8*, i8** %6
%25 = call i64 @strncmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %23, %16
%28 = load i8*, i8** %6
%29 = call i64 @strtol(i8* %28, i32* null, i32 0)
%30 = trunc i64 %29 to i32
store i32 %30, i32* %11
%31 = load i32, i32* %10
%32 = sub nsw i32 %31, 2
store i32 %32, i32* %10
br label %37
33: ; preds = %23
%34 = load i8*, i8** %6
%35 = call i64 @strtol(i8* %34, i32* null, i32 16)
%36 = trunc i64 %35 to i32
store i32 %36, i32* %11
br label %37
37: ; preds = %33, %27
%38 = load i32, i32* %10
switch i32 %38, label %57 [
i32 1, label %39
i32 2, label %39
i32 3, label %45
i32 4, label %45
]
39: ; preds = %37, %37
%40 = load i32*, i32** %7
store i32 1, i32* %40
%41 = load i32, i32* %11
%42 = load i32*, i32** %8
store i32 %41, i32* %42
%43 = load i32, i32* %11
%44 = load i32*, i32** %9
store i32 %43, i32* %44
br label %77
45: ; preds = %37, %37
%46 = load i32*, i32** %7
store i32 2, i32* %46
%47 = load i32, i32* %11
%48 = load i32*, i32** %8
store i32 %47, i32* %48
%49 = load i32, i32* %11
%50 = and i32 %49, 65280
%51 = lshr i32 %50, 8
%52 = load i32, i32* %11
%53 = and i32 %52, 255
%54 = shl i32 %53, 8
%55 = or i32 %51, %54
%56 = load i32*, i32** %9
store i32 %55, i32* %56
br label %77
57: ; preds = %37
%58 = load i32*, i32** %7
store i32 4, i32* %58
%59 = load i32, i32* %11
%60 = load i32*, i32** %8
store i32 %59, i32* %60
%61 = load i32, i32* %11
%62 = and i32 %61, 16711680
%63 = lshr i32 %62, 8
%64 = load i32, i32* %11
%65 = and i32 %64, -16777216
%66 = lshr i32 %65, 24
%67 = or i32 %63, %66
%68 = load i32, i32* %11
%69 = and i32 %68, 65280
%70 = shl i32 %69, 8
%71 = or i32 %67, %70
%72 = load i32, i32* %11
%73 = and i32 %72, 255
%74 = shl i32 %73, 24
%75 = or i32 %71, %74
%76 = load i32*, i32** %9
store i32 %75, i32* %76
br label %77
77: ; preds = %57, %45, %39
%78 = load i32, i32* %11
%79 = load i32, i32* %10
%80 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %78, i32 %79)
%81 = load i32, i32* %10
store i32 %81, i32* %5
br label %82
82: ; preds = %77, %15
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"0X\00"
@.str.2 = [33 x i8] c" Hex pattern: '0x%x' width '%d'\0A\00"
define i32 @process_pattern_4_hex(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = tail call i64 @validate_hex_pattern(i8* %0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %34, label %7
7: ; preds = %4
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = trunc i64 %8 to i32
%10 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12, %7
%16 = tail call i64 @strtol(i8* nocapture %0, i32* null, i32 0)
%17 = add nsw i32 %9, -2
br label %20
18: ; preds = %12
%19 = tail call i64 @strtol(i8* nocapture %0, i32* null, i32 16)
br label %20
20: ; preds = %18, %15
%21 = phi i32 [ %17, %15 ], [ %9, %18 ]
%22 = phi i64 [ %16, %15 ], [ %19, %18 ]
%23 = trunc i64 %22 to i32
switch i32 %21, label %29 [
i32 1, label %24
i32 2, label %24
i32 3, label %25
i32 4, label %25
]
24: ; preds = %20, %20
store i32 1, i32* %1
store i32 %23, i32* %2
br label %31
25: ; preds = %20, %20
store i32 2, i32* %1
store i32 %23, i32* %2
%26 = trunc i64 %22 to i16
%27 = tail call i16 @llvm.bswap.i16(i16 %26)
%28 = zext i16 %27 to i32
br label %31
29: ; preds = %20
store i32 4, i32* %1
store i32 %23, i32* %2
%30 = tail call i32 @llvm.bswap.i32(i32 %23)
br label %31
31: ; preds = %29, %25, %24
%32 = phi i32 [ %30, %29 ], [ %28, %25 ], [ %23, %24 ]
store i32 %32, i32* %3
%33 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %21)
br label %34
34: ; preds = %4, %31
%35 = phi i32 [ %21, %31 ], [ -1, %4 ]
ret i32 %35
}