#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 */
unsigned char *malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
unsigned char *sha3_pad(unsigned char *data, int size) {
  int i;
  int margin = (72 - size % 72);
  unsigned char *P = malloc(size + margin);
  memcpy(P, data, size);
  P[size] = 0x80;
  for (i = 1; i < margin; i++) {
    P[size + i] = 0;
  }
  P[size + margin - 1] = 0x02;
  return P;
}


x86 assembly (clang, -O0):
<sha3_pad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x48,%ecx
cltd
idiv   %ecx
mov    $0x48,%eax
sub    %edx,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
add    -0x14(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x80,(%rax,%rcx,1)
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a1 <sha3_pad+0x81>
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1176 <sha3_pad+0x56>
mov    -0x20(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x2,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha3_pad>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,(%rsp)
movslq %esi,%r13
imul   $0x38e38e39,%r13,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x24,%rax
add    %ecx,%eax
shl    $0x3,%eax
lea    (%rax,%rax,8),%eax
mov    %r13d,%r12d
sub    %eax,%r12d
mov    $0x48,%ebx
sub    %r12d,%ebx
lea    (%rbx,%r15,1),%r14d
mov    %r14d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    (%rsp),%rsi
mov    %r13d,%edx
call   1040 <memcpy@plt>
movb   $0x80,0x0(%rbp,%r13,1)
cmp    $0x46,%r12d
jg     11b9 <sha3_pad+0x89>
add    $0x1,%r15d
movslq %r15d,%rax
mov    %rbp,%rdi
add    %rax,%rdi
cmp    $0x3,%ebx
mov    $0x2,%edx
cmovge %ebx,%edx
add    $0xffffffff,%edx
xor    %esi,%esi
call   1030 <memset@plt>
movslq %r14d,%rax
movb   $0x2,-0x1(%rax,%rbp,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @sha3_pad(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = srem i32 %8, 72
%10 = sub nsw i32 72, %9
store i32 %10, i32* %6
%11 = load i32, i32* %4
%12 = load i32, i32* %6
%13 = add nsw i32 %11, %12
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = load i8*, i8** %3
%17 = load i32, i32* %4
%18 = call i32 @memcpy(i8* %15, i8* %16, i32 %17)
%19 = load i8*, i8** %7
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 -128, i8* %22
store i32 1, i32* %5
br label %23
23: ; preds = %34, %2
%24 = load i32, i32* %5
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = load i8*, i8** %7
%29 = load i32, i32* %4
%30 = load i32, i32* %5
%31 = add nsw i32 %29, %30
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %27
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %23
37: ; preds = %23
%38 = load i8*, i8** %7
%39 = load i32, i32* %4
%40 = load i32, i32* %6
%41 = add nsw i32 %39, %40
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %38, i64 %43
store i8 2, i8* %44
%45 = load i8*, i8** %7
ret i8* %45
}

LLVM IR (-O2):
define i8* @sha3_pad(i8* %0, i32 %1) {
%3 = srem i32 %1, 72
%4 = sub nsw i32 72, %3
%5 = add nsw i32 %4, %1
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 @memcpy(i8* %6, i8* %0, i32 %1)
%8 = sext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %6, i64 %8
store i8 -128, i8* %9
%10 = icmp slt i32 %3, 71
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = add i32 %1, 1
%13 = sext i32 %12 to i64
%14 = getelementptr i8, i8* %6, i64 %13
%15 = call i32 @llvm.smax.i32(i32 %4, i32 2)
%16 = add nsw i32 %15, -1
%17 = zext i32 %16 to i64
br label %18
18: ; preds = %11, %2
%19 = add nsw i32 %5, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %6, i64 %20
store i8 2, i8* %21
ret i8* %6
}