#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*/ CalulateCRC(unsigned char *, int, unsigned char *);
unsigned char MFRC522_ToCard(int, unsigned char *, int, unsigned char *,
                             unsigned int *);
unsigned char MFRC522_Write(unsigned char blockAddr, unsigned char *writeData) {
  unsigned char status;
  unsigned int recvBits;
  unsigned char i;
  unsigned char buff[18];
  buff[0] = 0xA0;
  buff[1] = blockAddr;
  CalulateCRC(buff, 2, &buff[2]);
  status = MFRC522_ToCard(0x0C, buff, 4, buff, &recvBits);
  if ((status != 0) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) {
    status = 2;
  }
  if (status == 0) {
    for (i = 0; i < 16; i++) {
      buff[i] = *(writeData + i);
    }
    CalulateCRC(buff, 16, &buff[16]);
    status = MFRC522_ToCard(0x0C, buff, 18, buff, &recvBits);
    if ((status != 0) || (recvBits != 4) || ((buff[0] & 0x0F) != 0x0A)) {
      status = 2;
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<MFRC522_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0xa0,-0x30(%rbp)
mov    -0x1(%rbp),%al
mov    %al,-0x2f(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rdx
add    $0x2,%rdx
mov    $0x2,%esi
call   1040 <CalulateCRC@plt>
lea    -0x30(%rbp),%rsi
lea    -0x30(%rbp),%rcx
mov    $0xc,%edi
mov    $0x4,%edx
lea    -0x18(%rbp),%r8
call   1030 <MFRC522_ToCard@plt>
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    1197 <MFRC522_Write+0x77>
cmpl   $0x4,-0x18(%rbp)
jne    1197 <MFRC522_Write+0x77>
movzbl -0x30(%rbp),%eax
and    $0xf,%eax
cmp    $0xa,%eax
je     119b <MFRC522_Write+0x7b>
movb   $0x2,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    1240 <MFRC522_Write+0x120>
movb   $0x0,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
cmp    $0x10,%eax
jge    11dc <MFRC522_Write+0xbc>
mov    -0x10(%rbp),%rax
movzbl -0x19(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
movzbl -0x19(%rbp),%eax
mov    %cl,-0x30(%rbp,%rax,1)
mov    -0x19(%rbp),%al
add    $0x1,%al
mov    %al,-0x19(%rbp)
jmp    11ac <MFRC522_Write+0x8c>
lea    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rdx
add    $0x10,%rdx
mov    $0x10,%esi
call   1040 <CalulateCRC@plt>
lea    -0x30(%rbp),%rsi
lea    -0x30(%rbp),%rcx
mov    $0xc,%edi
mov    $0x12,%edx
lea    -0x18(%rbp),%r8
call   1030 <MFRC522_ToCard@plt>
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    1237 <MFRC522_Write+0x117>
cmpl   $0x4,-0x18(%rbp)
jne    1237 <MFRC522_Write+0x117>
movzbl -0x30(%rbp),%eax
and    $0xf,%eax
cmp    $0xa,%eax
je     123b <MFRC522_Write+0x11b>
movb   $0x2,-0x11(%rbp)
jmp    1240 <MFRC522_Write+0x120>
movzbl -0x11(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MFRC522_Write>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
movb   $0xa0,0x10(%rsp)
mov    %dil,0x11(%rsp)
lea    0x12(%rsp),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <CalulateCRC@plt>
lea    0xc(%rsp),%r8
mov    $0xc,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <MFRC522_ToCard@plt>
mov    $0x2,%bl
test   %al,%al
jne    11cd <MFRC522_Write+0xad>
cmpl   $0x4,0xc(%rsp)
jne    11cd <MFRC522_Write+0xad>
mov    0x10(%rsp),%al
and    $0xf,%al
cmp    $0xa,%al
jne    11cd <MFRC522_Write+0xad>
movups (%r14),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x20(%rsp),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   1040 <CalulateCRC@plt>
lea    0xc(%rsp),%r8
mov    $0xc,%edi
mov    %r14,%rsi
mov    $0x12,%edx
mov    %r14,%rcx
call   1030 <MFRC522_ToCard@plt>
test   %al,%al
jne    11cd <MFRC522_Write+0xad>
cmpl   $0x4,0xc(%rsp)
jne    11cd <MFRC522_Write+0xad>
mov    0x10(%rsp),%al
and    $0xf,%al
cmp    $0xa,%al
jne    11cd <MFRC522_Write+0xad>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define zeroext i8 @MFRC522_Write(i8 zeroext %0, i8* %1) {
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8
%8 = alloca [18 x i8]
store i8 %0, i8* %3
store i8* %1, i8** %4
%9 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
store i8 -96, i8* %9
%10 = load i8, i8* %3
%11 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 1
store i8 %10, i8* %11
%12 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%13 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 2
%14 = call i32 @CalulateCRC(i8* %12, i32 2, i8* %13)
%15 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%16 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%17 = call zeroext i8 @MFRC522_ToCard(i32 12, i8* %15, i32 4, i8* %16, i32* %6)
store i8 %17, i8* %5
%18 = load i8, i8* %5
%19 = zext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %30, label %21
21: ; preds = %2
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 4
br i1 %23, label %30, label %24
24: ; preds = %21
%25 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = and i32 %27, 15
%29 = icmp ne i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %24, %21, %2
store i8 2, i8* %5
br label %31
31: ; preds = %30, %24
%32 = load i8, i8* %5
%33 = zext i8 %32 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %74
35: ; preds = %31
store i8 0, i8* %7
br label %36
36: ; preds = %50, %35
%37 = load i8, i8* %7
%38 = zext i8 %37 to i32
%39 = icmp slt i32 %38, 16
br i1 %39, label %40, label %53
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = load i8, i8* %7
%43 = zext i8 %42 to i32
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = load i8, i8* %45
%47 = load i8, i8* %7
%48 = zext i8 %47 to i64
%49 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 %48
store i8 %46, i8* %49
br label %50
50: ; preds = %40
%51 = load i8, i8* %7
%52 = add i8 %51, 1
store i8 %52, i8* %7
br label %36
53: ; preds = %36
%54 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%55 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 16
%56 = call i32 @CalulateCRC(i8* %54, i32 16, i8* %55)
%57 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%58 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%59 = call zeroext i8 @MFRC522_ToCard(i32 12, i8* %57, i32 18, i8* %58, i32* %6)
store i8 %59, i8* %5
%60 = load i8, i8* %5
%61 = zext i8 %60 to i32
%62 = icmp ne i32 %61, 0
br i1 %62, label %72, label %63
63: ; preds = %53
%64 = load i32, i32* %6
%65 = icmp ne i32 %64, 4
br i1 %65, label %72, label %66
66: ; preds = %63
%67 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = and i32 %69, 15
%71 = icmp ne i32 %70, 10
br i1 %71, label %72, label %73
72: ; preds = %66, %63, %53
store i8 2, i8* %5
br label %73
73: ; preds = %72, %66
br label %74
74: ; preds = %73, %31
%75 = load i8, i8* %5
ret i8 %75
}

LLVM IR (-O2):
define zeroext i8 @MFRC522_Write(i8 zeroext %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca [18 x i8]
%5 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
store i8 -96, i8* %7
%8 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 1
store i8 %0, i8* %8
%9 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 2
%10 = call i32 @CalulateCRC(i8* %7, i32 2, i8* %9)
%11 = call zeroext i8 @MFRC522_ToCard(i32 12, i8* %7, i32 4, i8* %7, i32* %3)
%12 = icmp ne i8 %11, 0
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 4
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %33, label %16
16: ; preds = %2
%17 = load i8, i8* %7
%18 = and i8 %17, 15
%19 = icmp eq i8 %18, 10
br i1 %19, label %20, label %33
20: ; preds = %16
%21 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 16
%22 = call i32 @CalulateCRC(i8* %7, i32 16, i8* %21)
%23 = call zeroext i8 @MFRC522_ToCard(i32 12, i8* %7, i32 18, i8* %7, i32* %3)
%24 = icmp ne i8 %23, 0
%25 = load i32, i32* %3
%26 = icmp ne i32 %25, 4
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %32, label %28
28: ; preds = %20
%29 = load i8, i8* %7
%30 = and i8 %29, 15
%31 = icmp eq i8 %30, 10
br i1 %31, label %33, label %32
32: ; preds = %28, %20
br label %33
33: ; preds = %16, %2, %28, %32
%34 = phi i8 [ 2, %32 ], [ 0, %28 ], [ 2, %2 ], [ 2, %16 ]
ret i8 %34
}