#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 */
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *removePadding(char *buffer) {
  if (!(buffer[8 - 1] >= 1 && buffer[8 - 1] <= 8)) {
    printf("No padding\n");
    return (char *)buffer;
  }
  printf("Padding value of last block: %02x\n", buffer[8 - 1]);
  char *block;
  int size = 8 - buffer[8 - 1];
  block = malloc(size * sizeof(char));
  printf("Size of block after padding removal: %i\n", size);
  printf("Hex representation of last block: ");
  int i;
  for (i = 0; i < size; i++) {
    block[i] = buffer[i];
    printf("%02x ", (unsigned char)block[i]);
  }
  printf("\n");
  return block;
}


x86 assembly (clang, -O0):
<removePadding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl 0x7(%rax),%eax
cmp    $0x1,%eax
jl     114e <removePadding+0x2e>
mov    -0x10(%rbp),%rax
movsbl 0x7(%rax),%eax
cmp    $0x8,%eax
jle    1169 <removePadding+0x49>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122a <removePadding+0x10a>
mov    -0x10(%rbp),%rax
movsbl 0x7(%rax),%esi
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movsbl 0x7(%rax),%ecx
mov    $0x8,%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1214 <removePadding+0xf4>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ca <removePadding+0xaa>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<removePadding>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
movzbl 0x7(%rdi),%esi
lea    -0x1(%rsi),%eax
cmp    $0x8,%al
jae    11e8 <removePadding+0xa8>
lea    0xe9f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x7(%r14),%ebx
mov    $0x8,%r15d
sub    %ebx,%r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
lea    0xe9b(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeb3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x7,%ebx
jg     11dc <removePadding+0x9c>
cmp    $0x2,%r15d
mov    $0x1,%ebp
cmovge %r15d,%ebp
lea    0xeb6(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%esi
mov    %sil,(%r12,%rbx,1)
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11c0 <removePadding+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11f7 <removePadding+0xb7>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%r12
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"No padding\0A\00"
@.str.1 = [35 x i8] c"Padding value of last block: %02x\0A\00"
@.str.2 = [41 x i8] c"Size of block after padding removal: %i\0A\00"
@.str.3 = [35 x i8] c"Hex representation of last block: \00"
@.str.4 = [6 x i8] c"%02x \00"
@.str.5 = [2 x i8] c"\0A\00"
define i8* @removePadding(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i64 7
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp sge i32 %10, 1
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 7
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp sle i32 %16, 8
br i1 %17, label %21, label %18
18: ; preds = %12, %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %3
store i8* %20, i8** %2
br label %67
21: ; preds = %12
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 7
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 7
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = sub nsw i32 8, %30
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = mul i64 %33, 1
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %4
%37 = load i32, i32* %5
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %6
br label %40
40: ; preds = %61, %21
%41 = load i32, i32* %6
%42 = load i32, i32* %5
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %64
44: ; preds = %40
%45 = load i8*, i8** %3
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = load i8*, i8** %4
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 %49, i8* %53
%54 = load i8*, i8** %4
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %44
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 1
store i32 %63, i32* %6
br label %40
64: ; preds = %40
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%66 = load i8*, i8** %4
store i8* %66, i8** %2
br label %67
67: ; preds = %64, %18
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@.str.1 = [35 x i8] c"Padding value of last block: %02x\0A\00"
@.str.2 = [41 x i8] c"Size of block after padding removal: %i\0A\00"
@.str.3 = [35 x i8] c"Hex representation of last block: \00"
@.str.4 = [6 x i8] c"%02x \00"
@str = [11 x i8] c"No padding\00"
define i8* @removePadding(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 7
%3 = load i8, i8* %2
%4 = add i8 %3, -1
%5 = icmp ult i8 %4, 8
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %32
8: ; preds = %1
%9 = zext i8 %3 to i32
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i8, i8* %2
%12 = sext i8 %11 to i32
%13 = sub nsw i32 8, %12
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp slt i8 %11, 8
br i1 %17, label %18, label %30
18: ; preds = %8
%19 = call i32 @llvm.smax.i32(i32 %13, i32 1)
%20 = zext i32 %19 to i64
br label %21
21: ; preds = %18, %21
%22 = phi i64 [ 0, %18 ], [ %28, %21 ]
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = load i8, i8* %23
%25 = getelementptr inbounds i8, i8* %14, i64 %22
store i8 %24, i8* %25
%26 = zext i8 %24 to i32
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = add nuw nsw i64 %22, 1
%29 = icmp eq i64 %28, %20
br i1 %29, label %30, label %21
30: ; preds = %21, %8
%31 = tail call i32 @putchar(i32 10)
br label %32
32: ; preds = %30, %6
%33 = phi i8* [ %14, %30 ], [ %0, %6 ]
ret i8* %33
}