#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 char uint8_t;

/* Variables and functions */
int MFRC522_Write(char const, char *);
int /*<<< orphan*/ memcpy(char *, char const *, int const);
int /*<<< orphan*/ printf(char *, char const, ...);
int /*<<< orphan*/ puts(char *);
int MFRC522_Debug_Write(const char blockaddr, const char *Write_Data,
                        const int len) {
  int ret;
  uint8_t buffer[16] = "";
  printf("Try to write block %d with %d byte data...", blockaddr, len);
  if (blockaddr == 0 || (blockaddr % 4) == 0x03) {
    puts("cannot write control block");
    return -1;
  }
  memcpy(buffer, Write_Data, len <= 16 ? len : 16);
  ret = MFRC522_Write(blockaddr, buffer);
  if (ret == (0)) {
    puts("OK");
    return 0;
  } else {
    printf("Failed, error 0x%02X\r\n", ret);
    return ret;
  }
}


x86 assembly (clang, -O0):
<MFRC522_Debug_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x5(%rbp),%al
mov    -0x14(%rbp),%edx
lea    0xe7e(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x5(%rbp),%eax
cmp    $0x0,%eax
je     11ae <MFRC522_Debug_Write+0x5e>
movsbl -0x5(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x3,%edx
jne    11c6 <MFRC522_Debug_Write+0x76>
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <MFRC522_Debug_Write+0x106>
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x10,-0x14(%rbp)
jg     11eb <MFRC522_Debug_Write+0x9b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    11f8 <MFRC522_Debug_Write+0xa8>
mov    $0x10,%eax
mov    %eax,-0x44(%rbp)
jmp    11f8 <MFRC522_Debug_Write+0xa8>
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x5(%rbp),%al
lea    -0x30(%rbp),%rsi
movsbl %al,%edi
call   1070 <MFRC522_Write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    123c <MFRC522_Debug_Write+0xec>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1256 <MFRC522_Debug_Write+0x106>
mov    -0x18(%rbp),%eax
lea    0xe03(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MFRC522_Debug_Write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
lea    0xea3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %bl,%bl
je     11bc <MFRC522_Debug_Write+0x7c>
mov    %ebx,%eax
sar    $0x7,%al
shr    $0x6,%al
add    %bl,%al
and    $0xfc,%al
mov    %ebx,%ecx
sub    %al,%cl
cmp    $0x3,%cl
je     11bc <MFRC522_Debug_Write+0x7c>
cmp    $0x10,%ebp
mov    $0x10,%edx
cmovl  %ebp,%edx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
call   1050 <memcpy@plt>
movsbl %bl,%edi
mov    %rbp,%rsi
call   1060 <MFRC522_Write@plt>
test   %eax,%eax
je     11cf <MFRC522_Debug_Write+0x8f>
mov    %eax,%ebx
movsbl %bl,%esi
lea    0xe96(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11dd <MFRC522_Debug_Write+0x9d>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    11dd <MFRC522_Debug_Write+0x9d>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Try to write block %d with %d byte data...\00"
@.str.1 = [27 x i8] c"cannot write control block\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [23 x i8] c"Failed, error 0x%02X\0D\0A\00"
define i32 @MFRC522_Debug_Write(i8 signext %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [16 x i8]
store i8 %0, i8* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = bitcast [16 x i8]* %9 to i8*
%11 = load i8, i8* %5
%12 = load i32, i32* %7
%13 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8 signext %11, i32 %12)
%14 = load i8, i8* %5
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %3
%18 = load i8, i8* %5
%19 = sext i8 %18 to i32
%20 = srem i32 %19, 4
%21 = icmp eq i32 %20, 3
br i1 %21, label %22, label %24
22: ; preds = %17, %3
%23 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
24: ; preds = %17
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = icmp sle i32 %27, 16
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* %7
br label %32
31: ; preds = %24
br label %32
32: ; preds = %31, %29
%33 = phi i32 [ %30, %29 ], [ 16, %31 ]
%34 = call i32 @memcpy(i8* %25, i8* %26, i32 %33)
%35 = load i8, i8* %5
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%37 = call i32 @MFRC522_Write(i8 signext %35, i8* %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %47
42: ; preds = %32
%43 = load i32, i32* %8
%44 = trunc i32 %43 to i8
%45 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8 signext %44)
%46 = load i32, i32* %8
store i32 %46, i32* %4
br label %47
47: ; preds = %42, %40, %22
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [43 x i8] c"Try to write block %d with %d byte data...\00"
@.str.1 = [27 x i8] c"cannot write control block\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [23 x i8] c"Failed, error 0x%02X\0D\0A\00"
define i32 @MFRC522_Debug_Write(i8 signext %0, i8* %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8 signext %0, i32 %2)
%7 = icmp eq i8 %0, 0
%8 = srem i8 %0, 4
%9 = icmp eq i8 %8, 3
%10 = or i1 %7, %9
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %24
13: ; preds = %3
%14 = icmp slt i32 %2, 16
%15 = select i1 %14, i32 %2, i32 16
%16 = call i32 @memcpy(i8* %5, i8* %1, i32 %15)
%17 = call i32 @MFRC522_Write(i8 signext %0, i8* %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %24
21: ; preds = %13
%22 = trunc i32 %17 to i8
%23 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8 signext %22)
br label %24
24: ; preds = %21, %19, %11
%25 = phi i32 [ -1, %11 ], [ 0, %19 ], [ %17, %21 ]
ret i32 %25
}