#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;
typedef int ssize_t;
typedef scalar_t__ int8_t;
typedef int int64_t;
typedef int int32_t;
typedef int int16_t;
typedef int /*<<< orphan*/ buf_le;
typedef int /*<<< orphan*/ buf_be;

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
int struct_unpack(int *, int, char *, scalar_t__ *, int *, int *, int *);
void test_struct_unpack_endian(void) {
  uint8_t buf_le[] = {0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  uint8_t buf_be[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03};
  ssize_t size;
  int8_t b;
  int16_t h;
  int32_t i;
  int64_t q;
  ssize_t res1, res2;
  q = i = h = b = 0;
  size = struct_unpack(buf_le, sizeof(buf_le), "<bhiq", &b, &h, &i, &q);
  res1 = (size == sizeof(buf_le) && b == 0 && h == 1 && i == 2 && q == 3);
  q = i = h = b = 0;
  size = struct_unpack(buf_be, sizeof(buf_be), ">bhiq", &b, &h, &i, &q);
  res2 = (size == sizeof(buf_be) && b == 0 && h == 1 && i == 2 && q == 3);
  printf("Unpack endian test: ");
  if (res1 && res2)
    printf("PASS\n");
  else
    printf("FAIL\n");
}


x86 assembly (clang, -O0):
<test_struct_unpack_endian>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x3c,%edx
call   1040 <memset@plt>
movl   $0x1,-0x3c(%rbp)
movl   $0x2,-0x34(%rbp)
movl   $0x3,-0x24(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x3c,%edx
call   1040 <memset@plt>
movl   $0x1,-0x78(%rbp)
movl   $0x2,-0x68(%rbp)
movl   $0x3,-0x48(%rbp)
movq   $0x0,-0x90(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0x0,-0x98(%rbp)
movl   $0x0,-0x9c(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x3c,%esi
lea    0xe42(%rip),%rdx
lea    -0x90(%rbp),%rcx
lea    -0x94(%rbp),%r8
lea    -0x98(%rbp),%r9
lea    -0x9c(%rbp),%rax
mov    %rax,(%rsp)
call   1050 <struct_unpack@plt>
mov    %eax,-0x84(%rbp)
movslq -0x84(%rbp),%rcx
xor    %eax,%eax
cmp    $0x3c,%rcx
mov    %al,-0xa5(%rbp)
jne    1252 <test_struct_unpack_endian+0x122>
xor    %eax,%eax
cmpq   $0x0,-0x90(%rbp)
mov    %al,-0xa5(%rbp)
jne    1252 <test_struct_unpack_endian+0x122>
xor    %eax,%eax
cmpl   $0x1,-0x94(%rbp)
mov    %al,-0xa5(%rbp)
jne    1252 <test_struct_unpack_endian+0x122>
xor    %eax,%eax
cmpl   $0x2,-0x98(%rbp)
mov    %al,-0xa5(%rbp)
jne    1252 <test_struct_unpack_endian+0x122>
cmpl   $0x3,-0x9c(%rbp)
sete   %al
mov    %al,-0xa5(%rbp)
mov    -0xa5(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xa0(%rbp)
movq   $0x0,-0x90(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0x0,-0x98(%rbp)
movl   $0x0,-0x9c(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x3c,%esi
lea    0xd6a(%rip),%rdx
lea    -0x90(%rbp),%rcx
lea    -0x94(%rbp),%r8
lea    -0x98(%rbp),%r9
lea    -0x9c(%rbp),%rax
mov    %rax,(%rsp)
call   1050 <struct_unpack@plt>
mov    %eax,-0x84(%rbp)
movslq -0x84(%rbp),%rcx
xor    %eax,%eax
cmp    $0x3c,%rcx
mov    %al,-0xa6(%rbp)
jne    1330 <test_struct_unpack_endian+0x200>
xor    %eax,%eax
cmpq   $0x0,-0x90(%rbp)
mov    %al,-0xa6(%rbp)
jne    1330 <test_struct_unpack_endian+0x200>
xor    %eax,%eax
cmpl   $0x1,-0x94(%rbp)
mov    %al,-0xa6(%rbp)
jne    1330 <test_struct_unpack_endian+0x200>
xor    %eax,%eax
cmpl   $0x2,-0x98(%rbp)
mov    %al,-0xa6(%rbp)
jne    1330 <test_struct_unpack_endian+0x200>
cmpl   $0x3,-0x9c(%rbp)
sete   %al
mov    %al,-0xa6(%rbp)
mov    -0xa6(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xa4(%rbp)
lea    0xcc4(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0xa0(%rbp)
je     1378 <test_struct_unpack_endian+0x248>
cmpl   $0x0,-0xa4(%rbp)
je     1378 <test_struct_unpack_endian+0x248>
lea    0xcb3(%rip),%rdi
call   1030 <printf@plt>
jmp    1384 <test_struct_unpack_endian+0x254>
lea    0xca8(%rip),%rdi
call   1030 <printf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_struct_unpack_endian>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x70(%rsp)
movups %xmm0,0x8c(%rsp)
movaps %xmm0,0x80(%rsp)
movl   $0x1,0x64(%rsp)
movl   $0x2,0x6c(%rsp)
movl   $0x3,0x7c(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x30(%rsp)
movups %xmm0,0x4c(%rsp)
movaps %xmm0,0x40(%rsp)
movl   $0x1,0x28(%rsp)
movl   $0x2,0x38(%rsp)
movl   $0x3,0x58(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x14(%rsp)
lea    0x14(%rsp),%r14
mov    %r14,(%rsp)
lea    0xe33(%rip),%rdx
lea    0x60(%rsp),%rdi
lea    0x18(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    $0x3c,%esi
call   1050 <struct_unpack@plt>
xor    %ebp,%ebp
cmp    $0x3c,%eax
jne    1211 <test_struct_unpack_endian+0xe1>
cmpq   $0x0,0x18(%rsp)
jne    1211 <test_struct_unpack_endian+0xe1>
cmpl   $0x1,0x10(%rsp)
jne    1211 <test_struct_unpack_endian+0xe1>
cmpl   $0x2,0xc(%rsp)
jne    1211 <test_struct_unpack_endian+0xe1>
cmpl   $0x3,0x14(%rsp)
sete   %bpl
movq   $0x0,0x18(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x14(%rsp)
mov    %r14,(%rsp)
lea    0xdc9(%rip),%rdx
lea    0x20(%rsp),%rdi
lea    0x18(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    $0x3c,%esi
call   1050 <struct_unpack@plt>
cmp    $0x3c,%eax
jne    1298 <test_struct_unpack_endian+0x168>
cmpq   $0x0,0x18(%rsp)
jne    1298 <test_struct_unpack_endian+0x168>
cmpl   $0x1,0x10(%rsp)
jne    1298 <test_struct_unpack_endian+0x168>
cmpl   $0x2,0xc(%rsp)
jne    1298 <test_struct_unpack_endian+0x168>
cmpl   $0x3,0x14(%rsp)
sete   %bl
lea    0xd87(%rip),%rdi
call   1040 <printf@plt>
test   %bl,%bpl
je     12a4 <test_struct_unpack_endian+0x174>
lea    0xd90(%rip),%rdi
jmp    12ab <test_struct_unpack_endian+0x17b>
lea    0xd6d(%rip),%rdi
call   1040 <printf@plt>
lea    0xd76(%rip),%rdi
call   1030 <puts@plt>
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"<bhiq\00"
@.str.1 = [6 x i8] c">bhiq\00"
@.str.2 = [21 x i8] c"Unpack endian test: \00"
@.str.3 = [6 x i8] c"PASS\0A\00"
@.str.4 = [6 x i8] c"FAIL\0A\00"
define void @test_struct_unpack_endian() {
%1 = alloca [15 x i32]
%2 = alloca [15 x i32]
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = bitcast [15 x i32]* %1 to i8*
%11 = bitcast i8* %10 to [15 x i32]*
%12 = getelementptr inbounds [15 x i32], [15 x i32]* %11, i32 0, i32 1
store i32 1, i32* %12
%13 = getelementptr inbounds [15 x i32], [15 x i32]* %11, i32 0, i32 3
store i32 2, i32* %13
%14 = getelementptr inbounds [15 x i32], [15 x i32]* %11, i32 0, i32 7
store i32 3, i32* %14
%15 = bitcast [15 x i32]* %2 to i8*
%16 = bitcast i8* %15 to [15 x i32]*
%17 = getelementptr inbounds [15 x i32], [15 x i32]* %16, i32 0, i32 2
store i32 1, i32* %17
%18 = getelementptr inbounds [15 x i32], [15 x i32]* %16, i32 0, i32 6
store i32 2, i32* %18
%19 = getelementptr inbounds [15 x i32], [15 x i32]* %16, i32 0, i32 14
store i32 3, i32* %19
store i64 0, i64* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%20 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 0
%21 = call i32 @struct_unpack(i32* %20, i32 60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64* %4, i32* %5, i32* %6, i32* %7)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = icmp eq i64 %23, 60
br i1 %24, label %25, label %37
25: ; preds = %0
%26 = load i64, i64* %4
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, 3
br label %37
37: ; preds = %34, %31, %28, %25, %0
%38 = phi i1 [ false, %31 ], [ false, %28 ], [ false, %25 ], [ false, %0 ], [ %36, %34 ]
%39 = zext i1 %38 to i32
store i32 %39, i32* %8
store i64 0, i64* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%40 = getelementptr inbounds [15 x i32], [15 x i32]* %2, i64 0, i64 0
%41 = call i32 @struct_unpack(i32* %40, i32 60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64* %4, i32* %5, i32* %6, i32* %7)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = icmp eq i64 %43, 60
br i1 %44, label %45, label %57
45: ; preds = %37
%46 = load i64, i64* %4
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %45
%49 = load i32, i32* %5
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i32, i32* %6
%53 = icmp eq i32 %52, 2
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* %7
%56 = icmp eq i32 %55, 3
br label %57
57: ; preds = %54, %51, %48, %45, %37
%58 = phi i1 [ false, %51 ], [ false, %48 ], [ false, %45 ], [ false, %37 ], [ %56, %54 ]
%59 = zext i1 %58 to i32
store i32 %59, i32* %9
%60 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %57
%64 = load i32, i32* %9
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %70
68: ; preds = %63, %57
%69 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %68, %66
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"<bhiq\00"
@.str.1 = [6 x i8] c">bhiq\00"
@.str.2 = [21 x i8] c"Unpack endian test: \00"
@str = [5 x i8] c"FAIL\00"
@str.5 = [5 x i8] c"PASS\00"
define void @test_struct_unpack_endian() {
%1 = alloca [15 x i32]
%2 = alloca [15 x i32]
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast [15 x i32]* %1 to i8*
%8 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 1
store i32 1, i32* %8
%9 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 3
store i32 2, i32* %9
%10 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 7
store i32 3, i32* %10
%11 = bitcast [15 x i32]* %2 to i8*
%12 = getelementptr inbounds [15 x i32], [15 x i32]* %2, i64 0, i64 2
store i32 1, i32* %12
%13 = getelementptr inbounds [15 x i32], [15 x i32]* %2, i64 0, i64 6
store i32 2, i32* %13
%14 = getelementptr inbounds [15 x i32], [15 x i32]* %2, i64 0, i64 14
store i32 3, i32* %14
%15 = bitcast i64* %3 to i8*
%16 = bitcast i32* %4 to i8*
%17 = bitcast i32* %5 to i8*
%18 = bitcast i32* %6 to i8*
store i64 0, i64* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%19 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 0
%20 = call i32 @struct_unpack(i32* %19, i32 60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64* %3, i32* %4, i32* %5, i32* %6)
%21 = icmp eq i32 %20, 60
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 0
%24 = select i1 %21, i1 %23, i1 false
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 1
%27 = select i1 %24, i1 %26, i1 false
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, 2
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %31, label %34
31: ; preds = %0
%32 = load i32, i32* %6
%33 = icmp eq i32 %32, 3
br label %34
34: ; preds = %31, %0
%35 = phi i1 [ false, %0 ], [ %33, %31 ]
store i64 0, i64* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%36 = getelementptr inbounds [15 x i32], [15 x i32]* %2, i64 0, i64 0
%37 = call i32 @struct_unpack(i32* %36, i32 60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64* %3, i32* %4, i32* %5, i32* %6)
%38 = icmp eq i32 %37, 60
%39 = load i64, i64* %3
%40 = icmp eq i64 %39, 0
%41 = select i1 %38, i1 %40, i1 false
%42 = load i32, i32* %4
%43 = icmp eq i32 %42, 1
%44 = select i1 %41, i1 %43, i1 false
%45 = load i32, i32* %5
%46 = icmp eq i32 %45, 2
%47 = select i1 %44, i1 %46, i1 false
br i1 %47, label %50, label %48
48: ; preds = %34
%49 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %55
50: ; preds = %34
%51 = load i32, i32* %6
%52 = icmp eq i32 %51, 3
%53 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%54 = select i1 %35, i1 %52, i1 false
br i1 %54, label %56, label %55
55: ; preds = %48, %50
br label %56
56: ; preds = %50, %55
%57 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0), %55 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @str.5, i64 0, i64 0), %50 ]
%58 = call i32 @puts(i8* dereferenceable(1) %57)
ret void
}