#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 find_s_value(int, int, int);
unsigned char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int translate_binary_string_to_int(char *);
unsigned char *translate_char_to_binary_string(int);
unsigned char *s_box_function(unsigned char *input) {
  unsigned char *s_string = malloc(32 * sizeof(char));
  int i;
  int j;
  int s_value;
  int bit_group_start;
  char i_string[3];
  char j_string[5];
  unsigned char *temporary_string;
  for (int counter = 0; counter < 8; counter++) {
    bit_group_start = counter * 6;
    i_string[0] = input[bit_group_start];
    i_string[1] = input[bit_group_start + 5];
    i_string[2] = '\0';
    strncpy(j_string, (char *)input + (bit_group_start + 1), 4);
    j_string[4] = '\0';
    i = translate_binary_string_to_int(i_string);
    j = translate_binary_string_to_int(j_string);
    s_value = find_s_value(counter + 1, i, j);
    temporary_string = translate_char_to_binary_string(s_value);
    if (counter == 0) {
      strcpy((char *)s_string, (char *)temporary_string + 4);
    } else {
      strcat((char *)s_string, (char *)temporary_string + 4);
    }
  }
  return s_string;
}


x86 assembly (clang, -O0):
<s_box_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x20,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x8,-0x34(%rbp)
jge    1264 <s_box_function+0xf4>
imul   $0x6,-0x34(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x23(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x22(%rbp)
movb   $0x0,-0x21(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x24(%rbp)
lea    -0x23(%rbp),%rdi
call   1080 <translate_binary_string_to_int@plt>
mov    %eax,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
call   1080 <translate_binary_string_to_int@plt>
mov    %eax,-0x18(%rbp)
mov    -0x34(%rbp),%edi
add    $0x1,%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <find_s_value@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <translate_char_to_binary_string@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1240 <s_box_function+0xd0>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x4,%rsi
call   1040 <strcpy@plt>
jmp    1251 <s_box_function+0xe1>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x4,%rsi
call   1090 <strcat@plt>
jmp    1256 <s_box_function+0xe6>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1191 <s_box_function+0x21>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_box_function>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r13
mov    $0x20,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    0x0(%r13),%al
mov    %al,0x8(%rsp)
mov    0x5(%r13),%al
mov    %al,0x9(%rsp)
movb   $0x0,0xa(%rsp)
lea    0x1(%r13),%rsi
lea    0xb(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xf(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <translate_binary_string_to_int@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <translate_binary_string_to_int@plt>
mov    $0x1,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <find_s_value@plt>
mov    %eax,%edi
call   1060 <translate_char_to_binary_string@plt>
lea    0x4(%rax),%rsi
mov    %r14,0x10(%rsp)
mov    %r14,%rdi
call   1040 <strcpy@plt>
lea    0x7(%r13),%r12
mov    $0x2,%ebp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x6(%r13,%r14,1),%eax
mov    %al,0x8(%rsp)
movzbl 0xb(%r13,%r14,1),%eax
mov    %al,0x9(%rsp)
movb   $0x0,0xa(%rsp)
lea    (%r12,%r14,1),%rsi
lea    0xb(%rsp),%r15
mov    %r15,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xf(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <translate_binary_string_to_int@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1080 <translate_binary_string_to_int@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <find_s_value@plt>
mov    %eax,%edi
call   1060 <translate_char_to_binary_string@plt>
lea    0x4(%rax),%rsi
mov    0x10(%rsp),%rdi
call   1090 <strcat@plt>
add    $0x6,%r14
add    $0x1,%ebp
cmp    $0x2a,%r14
jne    1210 <s_box_function+0xa0>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @s_box_function(i8* %0) {
%2 = alloca [3 x i8]
%3 = alloca [5 x i8]
%4 = tail call dereferenceable_or_null(32) i8* @malloc(i32 32)
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 1
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 2
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 4
%10 = load i8, i8* %0
store i8 %10, i8* %5
%11 = getelementptr inbounds i8, i8* %0, i64 5
%12 = load i8, i8* %11
store i8 %12, i8* %7
store i8 0, i8* %8
%13 = getelementptr inbounds i8, i8* %0, i64 1
%14 = call i32 @strncpy(i8* %6, i8* %13, i32 4)
store i8 0, i8* %9
%15 = call i32 @translate_binary_string_to_int(i8* %5)
%16 = call i32 @translate_binary_string_to_int(i8* %6)
%17 = call i32 @find_s_value(i32 1, i32 %15, i32 %16)
%18 = call i8* @translate_char_to_binary_string(i32 %17)
%19 = getelementptr inbounds i8, i8* %18, i64 4
%20 = call i32 @strcpy(i8* %4, i8* %19)
br label %22
21: ; preds = %22
ret i8* %4
22: ; preds = %1, %22
%23 = phi i64 [ 1, %1 ], [ %35, %22 ]
%24 = mul nuw nsw i64 %23, 6
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = load i8, i8* %25
store i8 %26, i8* %5
%27 = add nuw nsw i64 %24, 5
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = load i8, i8* %28
store i8 %29, i8* %7
store i8 0, i8* %8
%30 = or i64 %24, 1
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = call i32 @strncpy(i8* %6, i8* %31, i32 4)
store i8 0, i8* %9
%33 = call i32 @translate_binary_string_to_int(i8* %5)
%34 = call i32 @translate_binary_string_to_int(i8* %6)
%35 = add nuw nsw i64 %23, 1
%36 = trunc i64 %35 to i32
%37 = call i32 @find_s_value(i32 %36, i32 %33, i32 %34)
%38 = call i8* @translate_char_to_binary_string(i32 %37)
%39 = getelementptr inbounds i8, i8* %38, i64 4
%40 = call i32 @strcat(i8* %4, i8* %39)
%41 = icmp eq i64 %35, 8
br i1 %41, label %21, label %22
}