#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[2];
  char j_string[4];
  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];
    strncpy(j_string, (char *)input + (bit_group_start + 1), 4);
    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    125c <s_box_function+0xec>
imul   $0x6,-0x34(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x22(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
lea    -0x26(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
add    %rax,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    -0x22(%rbp),%rdi
call   1080 <translate_binary_string_to_int@plt>
mov    %eax,-0x14(%rbp)
lea    -0x26(%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    1238 <s_box_function+0xc8>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x4,%rsi
call   1040 <strcpy@plt>
jmp    1249 <s_box_function+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x4,%rsi
call   1090 <strcat@plt>
jmp    124e <s_box_function+0xde>
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,0xa(%rsp)
mov    0x5(%r13),%al
mov    %al,0xb(%rsp)
lea    0x1(%r13),%rsi
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0xa(%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
nopw   0x0(%rax,%rax,1)
movzbl 0x6(%r13,%r14,1),%eax
mov    %al,0xa(%rsp)
movzbl 0xb(%r13,%r14,1),%eax
mov    %al,0xb(%rsp)
lea    (%r12,%r14,1),%rsi
lea    0xc(%rsp),%r15
mov    %r15,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0xa(%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    1200 <s_box_function+0x90>
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 [2 x i8]
%9 = alloca [4 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 = %63, %1
%14 = load i32, i32* %11
%15 = icmp slt i32 %14, 8
br i1 %15, label %16, label %66
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 [2 x i8], [2 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 [2 x i8], [2 x i8]* %8, i64 0, i64 1
store i8 %30, i8* %31
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%33 = load i8*, i8** %2
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
%38 = call i32 @strncpy(i8* %32, i8* %37, i32 4)
%39 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%40 = call i32 @translate_binary_string_to_int(i8* %39)
store i32 %40, i32* %4
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%42 = call i32 @translate_binary_string_to_int(i8* %41)
store i32 %42, i32* %5
%43 = load i32, i32* %11
%44 = add nsw i32 %43, 1
%45 = load i32, i32* %4
%46 = load i32, i32* %5
%47 = call i32 @find_s_value(i32 %44, i32 %45, i32 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = call i8* @translate_char_to_binary_string(i32 %48)
store i8* %49, i8** %10
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %16
%53 = load i8*, i8** %3
%54 = load i8*, i8** %10
%55 = getelementptr inbounds i8, i8* %54, i64 4
%56 = call i32 @strcpy(i8* %53, i8* %55)
br label %62
57: ; preds = %16
%58 = load i8*, i8** %3
%59 = load i8*, i8** %10
%60 = getelementptr inbounds i8, i8* %59, i64 4
%61 = call i32 @strcat(i8* %58, i8* %60)
br label %62
62: ; preds = %57, %52
br label %63
63: ; preds = %62
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 1
store i32 %65, i32* %11
br label %13
66: ; preds = %13
%67 = load i8*, i8** %3
ret i8* %67
}

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