#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 *, unsigned int);
int sscanf(unsigned char *, char *, unsigned int *);
int AppendCRC(unsigned char *hexstr, unsigned char *packet, int pSize) {
  char *start = packet + pSize;
  unsigned int u;
  int i = 0;
  while (i < 4 && sscanf(hexstr, "%2x", &u) == 1) {
    *start++ = u;
    hexstr += 2;
    i++;
    printf("copied: %x\n", u);
  }
  return 0;
}


x86 assembly (clang, -O0):
<AppendCRC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x4,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jge    1175 <AppendCRC+0x55>
mov    -0x8(%rbp),%rdi
lea    0xe9d(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1185 <AppendCRC+0x65>
jmp    11c4 <AppendCRC+0xa4>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
jmp    1149 <AppendCRC+0x29>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AppendCRC>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeca(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11f6 <AppendCRC+0xd6>
movslq %ebp,%r15
mov    0x4(%rsp),%esi
mov    %sil,(%r14,%r15,1)
lea    0x2(%rbx),%rbp
lea    0xea5(%rip),%rdi
call   1030 <printf@plt>
lea    0xe95(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rdi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11f6 <AppendCRC+0xd6>
mov    0x4(%rsp),%esi
mov    %sil,0x1(%r14,%r15,1)
lea    0x4(%rbx),%rbp
lea    0xe73(%rip),%rdi
call   1030 <printf@plt>
lea    0xe63(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rdi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11f6 <AppendCRC+0xd6>
mov    0x4(%rsp),%esi
mov    %sil,0x2(%r14,%r15,1)
add    $0x6,%rbx
lea    0xe41(%rip),%rdi
call   1030 <printf@plt>
lea    0xe31(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11f6 <AppendCRC+0xd6>
mov    0x4(%rsp),%esi
mov    %sil,0x3(%r14,%r15,1)
lea    0xe13(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%2x\00"
@.str.1 = [12 x i8] c"copied: %x\0A\00"
define i32 @AppendCRC(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8* %13, i8** %7
store i32 0, i32* %9
br label %14
14: ; preds = %23, %3
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 4
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %8)
%20 = icmp eq i32 %19, 1
br label %21
21: ; preds = %17, %14
%22 = phi i1 [ false, %14 ], [ %20, %17 ]
br i1 %22, label %23, label %34
23: ; preds = %21
%24 = load i32, i32* %8
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %7
store i8 %25, i8* %26
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 2
store i8* %29, i8** %4
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %14
34: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%2x\00"
@.str.1 = [12 x i8] c"copied: %x\0A\00"
define i32 @AppendCRC(i8* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i32
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = bitcast i32* %4 to i8*
%8 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %38
10: ; preds = %3
%11 = load i32, i32* %4
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %12, i8* %6
%14 = getelementptr inbounds i8, i8* %0, i64 2
%15 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%16 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %38
18: ; preds = %10
%19 = load i32, i32* %4
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %20, i8* %13
%22 = getelementptr inbounds i8, i8* %0, i64 4
%23 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%24 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %38
26: ; preds = %18
%27 = load i32, i32* %4
%28 = trunc i32 %27 to i8
store i8 %28, i8* %21
%29 = getelementptr inbounds i8, i8* %0, i64 6
%30 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%31 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = getelementptr inbounds i8, i8* %21, i64 1
%35 = load i32, i32* %4
%36 = trunc i32 %35 to i8
store i8 %36, i8* %34
%37 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %38
38: ; preds = %33, %26, %18, %10, %3
ret i32 0
}