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

/* Variables and functions */
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ memxor(void *, int *, int);
int /*<<< orphan*/ present_p(void *, int *);
int present_sbox(int);
void present_generic_enc(void *buffer, uint8_t *ctx, uint8_t ksize_B,
                         void (*update)(uint8_t *, uint8_t)) {
  uint8_t i, j, tmp[8], k[ksize_B];
  memcpy(k, ctx, ksize_B);
  memxor(buffer, k, 8);
  for (i = 1; i < 32; ++i) {
    j = 7;
    do {
      tmp[j] = present_sbox(((uint8_t *)buffer)[j]);
    } while (j--);
    present_p(buffer, tmp);
    update(k, i);
    memxor(buffer, k, 8);
  }
}


x86 assembly (clang, -O0):
<present_generic_enc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x68(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    $0x8,%edx
call   1030 <memxor@plt>
movl   $0x1,-0x24(%rbp)
cmpl   $0x20,-0x24(%rbp)
jge    121c <present_generic_enc+0xdc>
movl   $0x7,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1060 <present_sbox@plt>
mov    %eax,%ecx
movslq -0x28(%rbp),%rax
mov    %ecx,-0x50(%rbp,%rax,4)
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x28(%rbp)
cmp    $0x0,%eax
jne    11b4 <present_generic_enc+0x74>
mov    -0x8(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1040 <present_p@plt>
mov    -0x68(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%esi
call   *%rax
mov    -0x68(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    $0x8,%edx
call   1030 <memxor@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a3 <present_generic_enc+0x63>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<present_generic_enc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r14
mov    %rdi,%rbx
mov    %rsp,-0x58(%rbp)
mov    %edx,%eax
mov    %rsp,%r15
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <memxor@plt>
mov    $0x1,%r12d
lea    -0x50(%rbp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x1c(%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x34(%rbp)
mov    0x18(%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x38(%rbp)
mov    0x14(%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x3c(%rbp)
mov    0x10(%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x40(%rbp)
mov    0xc(%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x44(%rbp)
mov    0x8(%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x48(%rbp)
mov    0x4(%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x4c(%rbp)
mov    (%rbx),%edi
call   1060 <present_sbox@plt>
mov    %eax,-0x50(%rbp)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <present_p@plt>
mov    %r15,%rdi
mov    %r12d,%esi
call   *%r14
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <memxor@plt>
add    $0x1,%r12d
cmp    $0x20,%r12d
jne    11a0 <present_generic_enc+0x60>
mov    -0x58(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @present_generic_enc(i8* %0, i32* %1, i32 %2, void (i32*, i32)* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca void (i32*, i32)*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [8 x i32]
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store void (i32*, i32)* %3, void (i32*, i32)** %8
%14 = load i32, i32* %7
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %12
%17 = alloca i32, i64 %15
store i64 %15, i64* %13
%18 = load i32*, i32** %6
%19 = load i32, i32* %7
%20 = call i32 @memcpy(i32* %17, i32* %18, i32 %19)
%21 = load i8*, i8** %5
%22 = call i32 @memxor(i8* %21, i32* %17, i32 8)
store i32 1, i32* %9
br label %23
23: ; preds = %50, %4
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 32
br i1 %25, label %26, label %53
26: ; preds = %23
store i32 7, i32* %10
br label %27
27: ; preds = %38, %26
%28 = load i8*, i8** %5
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = call i32 @present_sbox(i32 %33)
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [8 x i32], [8 x i32]* %11, i64 0, i64 %36
store i32 %34, i32* %37
br label %38
38: ; preds = %27
%39 = load i32, i32* %10
%40 = add nsw i32 %39, -1
store i32 %40, i32* %10
%41 = icmp ne i32 %39, 0
br i1 %41, label %27, label %42
42: ; preds = %38
%43 = load i8*, i8** %5
%44 = getelementptr inbounds [8 x i32], [8 x i32]* %11, i64 0, i64 0
%45 = call i32 @present_p(i8* %43, i32* %44)
%46 = load void (i32*, i32)*, void (i32*, i32)** %8
%47 = load i32, i32* %9
%48 = load i8*, i8** %5
%49 = call i32 @memxor(i8* %48, i32* %17, i32 8)
br label %50
50: ; preds = %42
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %23
53: ; preds = %23
%54 = load i8*, i8** %12
ret void
}

LLVM IR (-O2):
define void @present_generic_enc(i8* %0, i32* %1, i32 %2, void (i32*, i32)* nocapture %3) {
%5 = alloca [8 x i32]
%6 = bitcast [8 x i32]* %5 to i8*
%7 = zext i32 %2 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i32, i64 %7
%10 = call i32 @memcpy(i32* %9, i32* %1, i32 %2)
%11 = call i32 @memxor(i8* %0, i32* %9, i32 8)
%12 = bitcast i8* %0 to i32*
%13 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%14 = getelementptr inbounds i8, i8* %0, i64 28
%15 = bitcast i8* %14 to i32*
%16 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 7
%17 = getelementptr inbounds i8, i8* %0, i64 24
%18 = bitcast i8* %17 to i32*
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 6
%20 = getelementptr inbounds i8, i8* %0, i64 20
%21 = bitcast i8* %20 to i32*
%22 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 5
%23 = getelementptr inbounds i8, i8* %0, i64 16
%24 = bitcast i8* %23 to i32*
%25 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 4
%26 = getelementptr inbounds i8, i8* %0, i64 12
%27 = bitcast i8* %26 to i32*
%28 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 3
%29 = getelementptr inbounds i8, i8* %0, i64 8
%30 = bitcast i8* %29 to i32*
%31 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 2
%32 = getelementptr inbounds i8, i8* %0, i64 4
%33 = bitcast i8* %32 to i32*
%34 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 1
br label %35
35: ; preds = %4, %35
%36 = phi i32 [ 1, %4 ], [ %55, %35 ]
%37 = load i32, i32* %15
%38 = call i32 @present_sbox(i32 %37)
store i32 %38, i32* %16
%39 = load i32, i32* %18
%40 = call i32 @present_sbox(i32 %39)
store i32 %40, i32* %19
%41 = load i32, i32* %21
%42 = call i32 @present_sbox(i32 %41)
store i32 %42, i32* %22
%43 = load i32, i32* %24
%44 = call i32 @present_sbox(i32 %43)
store i32 %44, i32* %25
%45 = load i32, i32* %27
%46 = call i32 @present_sbox(i32 %45)
store i32 %46, i32* %28
%47 = load i32, i32* %30
%48 = call i32 @present_sbox(i32 %47)
store i32 %48, i32* %31
%49 = load i32, i32* %33
%50 = call i32 @present_sbox(i32 %49)
store i32 %50, i32* %34
%51 = load i32, i32* %12
%52 = call i32 @present_sbox(i32 %51)
store i32 %52, i32* %13
%53 = call i32 @present_p(i8* %0, i32* %13)
%54 = call i32 @memxor(i8* %0, i32* %9, i32 8)
%55 = add nuw nsw i32 %36, 1
%56 = icmp eq i32 %55, 32
br i1 %56, label %57, label %35
57: ; preds = %35
ret void
}