#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*/ f1star(int const *, int *, int *, int *, int *, int *);
int /*<<< orphan*/ f5star(int const *, int *, int *, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(int *);
int *malloc(int);
scalar_t__ memcmp(int *, int *, int);
int /*<<< orphan*/ print_buffer(char *, int *, int);
int /*<<< orphan*/ stderr;
uint8_t *sqn_ms_derive(const uint8_t const opc[16], uint8_t *key, uint8_t *auts,
                       uint8_t *rand_p) {
  uint8_t ak[6];
  uint8_t *conc_sqn_ms;
  uint8_t *mac_s;
  uint8_t mac_s_computed[(8)];
  uint8_t *sqn_ms;
  uint8_t amf[2] = {0, 0};
  int i;
  conc_sqn_ms = auts;
  mac_s = &auts[6];
  sqn_ms = malloc(((48) / 8));
  f5star(opc, key, rand_p, ak);
  for (i = 0; i < 6; i++) {
    sqn_ms[i] = ak[i] ^ conc_sqn_ms[i];
  }
  print_buffer("sqn_ms_derive() KEY    : ", key, 16);
  print_buffer("sqn_ms_derive() RAND   : ", rand_p, 16);
  print_buffer("sqn_ms_derive() AUTS   : ", auts, 14);
  print_buffer("sqn_ms_derive() AK     : ", ak, 6);
  print_buffer("sqn_ms_derive() SQN_MS : ", sqn_ms, 6);
  print_buffer("sqn_ms_derive() MAC_S  : ", mac_s, 8);
  f1star(opc, key, rand_p, sqn_ms, amf, mac_s_computed);
  print_buffer("MAC_S +: ", mac_s_computed, 8);
  if (memcmp(mac_s_computed, mac_s, 8) != 0) {
    fprintf(stderr, "Failed to verify computed SQN_MS\n");
    free(sqn_ms);
    return NULL;
  }
  return sqn_ms;
}


x86 assembly (clang, -O0):
<sqn_ms_derive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x50(%rbp)
mov    $0x6,%edi
call   1090 <malloc@plt>
mov    %rax,-0x78(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1060 <f5star@plt>
movl   $0x0,-0x84(%rbp)
cmpl   $0x6,-0x84(%rbp)
jge    1234 <sqn_ms_derive+0xb4>
movslq -0x84(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edx
mov    -0x48(%rbp),%rax
movslq -0x84(%rbp),%rcx
xor    (%rax,%rcx,4),%edx
mov    -0x78(%rbp),%rax
movslq -0x84(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11ec <sqn_ms_derive+0x6c>
mov    -0x18(%rbp),%rsi
lea    0xdc1(%rip),%rdi
mov    $0x10,%edx
call   10a0 <print_buffer@plt>
mov    -0x28(%rbp),%rsi
lea    0xdc6(%rip),%rdi
mov    $0x10,%edx
call   10a0 <print_buffer@plt>
mov    -0x20(%rbp),%rsi
lea    0xdcb(%rip),%rdi
mov    $0xe,%edx
call   10a0 <print_buffer@plt>
lea    -0x40(%rbp),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x6,%edx
call   10a0 <print_buffer@plt>
mov    -0x78(%rbp),%rsi
lea    0xdd5(%rip),%rdi
mov    $0x6,%edx
call   10a0 <print_buffer@plt>
mov    -0x50(%rbp),%rsi
lea    0xdda(%rip),%rdi
mov    $0x8,%edx
call   10a0 <print_buffer@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x78(%rbp),%rcx
lea    -0x80(%rbp),%r8
lea    -0x70(%rbp),%r9
call   1040 <f1star@plt>
lea    -0x70(%rbp),%rsi
lea    0xdc2(%rip),%rdi
mov    $0x8,%edx
call   10a0 <print_buffer@plt>
lea    -0x70(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    $0x8,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     132b <sqn_ms_derive+0x1ab>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x78(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1333 <sqn_ms_derive+0x1b3>
mov    -0x78(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sqn_ms_derive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r12
movq   $0x0,0x8(%rsp)
mov    $0x6,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <f5star@plt>
movups 0x0(%rbp),%xmm0
xorps  0x10(%rsp),%xmm0
movups %xmm0,(%rbx)
mov    0x10(%rbp),%eax
xor    0x20(%rsp),%eax
mov    %eax,0x10(%rbx)
mov    0x14(%rbp),%eax
xor    0x24(%rsp),%eax
mov    %eax,0x14(%rbx)
lea    0x18(%rbp),%r13
lea    0xe22(%rip),%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1090 <print_buffer@plt>
lea    0xe28(%rip),%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1090 <print_buffer@plt>
lea    0xe2e(%rip),%rdi
mov    %rbp,%rsi
mov    $0xe,%edx
call   1090 <print_buffer@plt>
lea    0xe34(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    $0x6,%edx
call   1090 <print_buffer@plt>
lea    0xe38(%rip),%rdi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1090 <print_buffer@plt>
lea    0xe3e(%rip),%rdi
mov    %r13,%rsi
mov    $0x8,%edx
call   1090 <print_buffer@plt>
lea    0x8(%rsp),%r8
lea    0x30(%rsp),%rbp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rbp,%r9
call   1040 <f1star@plt>
lea    0xe26(%rip),%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1090 <print_buffer@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x8,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     12b7 <sqn_ms_derive+0x147>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"sqn_ms_derive() KEY : \00"
@.str.1 = [26 x i8] c"sqn_ms_derive() RAND : \00"
@.str.2 = [26 x i8] c"sqn_ms_derive() AUTS : \00"
@.str.3 = [26 x i8] c"sqn_ms_derive() AK : \00"
@.str.4 = [26 x i8] c"sqn_ms_derive() SQN_MS : \00"
@.str.5 = [26 x i8] c"sqn_ms_derive() MAC_S : \00"
@.str.6 = [10 x i8] c"MAC_S +: \00"
@stderr = global i32 0
@.str.7 = [34 x i8] c"Failed to verify computed SQN_MS\0A\00"
define i32* @sqn_ms_derive(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [6 x i32]
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca [8 x i32]
%14 = alloca i32*
%15 = alloca [2 x i32]
%16 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%17 = bitcast [2 x i32]* %15 to i8*
%18 = load i32*, i32** %8
store i32* %18, i32** %11
%19 = load i32*, i32** %8
%20 = getelementptr inbounds i32, i32* %19, i64 6
store i32* %20, i32** %12
%21 = call i32* @malloc(i32 6)
store i32* %21, i32** %14
%22 = load i32*, i32** %6
%23 = load i32*, i32** %7
%24 = load i32*, i32** %9
%25 = getelementptr inbounds [6 x i32], [6 x i32]* %10, i64 0, i64 0
%26 = call i32 @f5star(i32* %22, i32* %23, i32* %24, i32* %25)
store i32 0, i32* %16
br label %27
27: ; preds = %45, %4
%28 = load i32, i32* %16
%29 = icmp slt i32 %28, 6
br i1 %29, label %30, label %48
30: ; preds = %27
%31 = load i32, i32* %16
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [6 x i32], [6 x i32]* %10, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = load i32*, i32** %11
%36 = load i32, i32* %16
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = xor i32 %34, %39
%41 = load i32*, i32** %14
%42 = load i32, i32* %16
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
store i32 %40, i32* %44
br label %45
45: ; preds = %30
%46 = load i32, i32* %16
%47 = add nsw i32 %46, 1
store i32 %47, i32* %16
br label %27
48: ; preds = %27
%49 = load i32*, i32** %7
%50 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32* %49, i32 16)
%51 = load i32*, i32** %9
%52 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32* %51, i32 16)
%53 = load i32*, i32** %8
%54 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32* %53, i32 14)
%55 = getelementptr inbounds [6 x i32], [6 x i32]* %10, i64 0, i64 0
%56 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32* %55, i32 6)
%57 = load i32*, i32** %14
%58 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32* %57, i32 6)
%59 = load i32*, i32** %12
%60 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32* %59, i32 8)
%61 = load i32*, i32** %6
%62 = load i32*, i32** %7
%63 = load i32*, i32** %9
%64 = load i32*, i32** %14
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %15, i64 0, i64 0
%66 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
%67 = call i32 @f1star(i32* %61, i32* %62, i32* %63, i32* %64, i32* %65, i32* %66)
%68 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
%69 = call i32 @print_buffer(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32* %68, i32 8)
%70 = getelementptr inbounds [8 x i32], [8 x i32]* %13, i64 0, i64 0
%71 = load i32*, i32** %12
%72 = call i64 @memcmp(i32* %70, i32* %71, i32 8)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %48
%75 = load i32, i32* @stderr
%76 = call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%77 = load i32*, i32** %14
%78 = call i32 @free(i32* %77)
store i32* null, i32** %5
br label %81
79: ; preds = %48
%80 = load i32*, i32** %14
store i32* %80, i32** %5
br label %81
81: ; preds = %79, %74
%82 = load i32*, i32** %5
ret i32* %82
}

LLVM IR (-O2):
@.str = [26 x i8] c"sqn_ms_derive() KEY : \00"
@.str.1 = [26 x i8] c"sqn_ms_derive() RAND : \00"
@.str.2 = [26 x i8] c"sqn_ms_derive() AUTS : \00"
@.str.3 = [26 x i8] c"sqn_ms_derive() AK : \00"
@.str.4 = [26 x i8] c"sqn_ms_derive() SQN_MS : \00"
@.str.5 = [26 x i8] c"sqn_ms_derive() MAC_S : \00"
@.str.6 = [10 x i8] c"MAC_S +: \00"
@stderr = global i32 0
@.str.7 = [34 x i8] c"Failed to verify computed SQN_MS\0A\00"
define i32* @sqn_ms_derive(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca [6 x i32]
%6 = alloca [8 x i32]
%7 = alloca i64
%8 = bitcast [6 x i32]* %5 to i8*
%9 = bitcast [8 x i32]* %6 to i8*
%10 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%11 = tail call i32* @malloc(i32 6)
%12 = getelementptr inbounds [6 x i32], [6 x i32]* %5, i64 0, i64 0
%13 = call i32 @f5star(i32* %0, i32* %1, i32* %3, i32* %12)
%14 = bitcast [6 x i32]* %5 to <4 x i32>*
%15 = load <4 x i32>, <4 x i32>* %14
%16 = bitcast i32* %2 to <4 x i32>*
%17 = load <4 x i32>, <4 x i32>* %16
%18 = xor <4 x i32> %17, %15
%19 = bitcast i32* %11 to <4 x i32>*
store <4 x i32> %18, <4 x i32>* %19
%20 = getelementptr inbounds [6 x i32], [6 x i32]* %5, i64 0, i64 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds i32, i32* %2, i64 4
%23 = load i32, i32* %22
%24 = xor i32 %23, %21
%25 = getelementptr inbounds i32, i32* %11, i64 4
store i32 %24, i32* %25
%26 = getelementptr inbounds [6 x i32], [6 x i32]* %5, i64 0, i64 5
%27 = load i32, i32* %26
%28 = getelementptr inbounds i32, i32* %2, i64 5
%29 = load i32, i32* %28
%30 = xor i32 %29, %27
%31 = getelementptr inbounds i32, i32* %11, i64 5
store i32 %30, i32* %31
%32 = getelementptr inbounds i32, i32* %2, i64 6
%33 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32* %1, i32 16)
%34 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32 16)
%35 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32* %2, i32 14)
%36 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32* %12, i32 6)
%37 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32* %11, i32 6)
%38 = call i32 @print_buffer(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32* %32, i32 8)
%39 = bitcast i64* %7 to i32*
%40 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 0
%41 = call i32 @f1star(i32* %0, i32* %1, i32* %3, i32* %11, i32* %39, i32* %40)
%42 = call i32 @print_buffer(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32* %40, i32 8)
%43 = call i64 @memcmp(i32* %40, i32* %32, i32 8)
%44 = icmp eq i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %4
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%48 = call i32 @free(i32* %11)
br label %49
49: ; preds = %4, %45
%50 = phi i32* [ null, %45 ], [ %11, %4 ]
ret i32* %50
}