#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 int /*<<< orphan*/ result_le;
typedef int /*<<< orphan*/ result_be;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ memcmp(int *, int *, int);
int /*<<< orphan*/ printf(char *);
int struct_pack(int *, int, char *, int /*<<< orphan*/, int, int, long long,
                float, double);
void test_struct_pack_endian(void) {
  uint8_t buf[100];
  ssize_t size;
  uint8_t result_le[] = {0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00,
                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
                         0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x40};
  uint8_t result_be[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
                         0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x40, 0x80, 0x00,
                         0x00, 0x40, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  ssize_t res1, res2;
  size = struct_pack(buf, sizeof(buf), "<bhiqfd", 0, 1, 2, 3LL, 4.0F, 5.0);
  res1 = (size == sizeof(result_le) &&
          memcmp(buf, result_le, sizeof(result_le)) == 0);
  size = struct_pack(buf, sizeof(buf), ">bhiqfd", 0, 1, 2, 3LL, 4.0F, 5.0);
  res2 = (size == sizeof(result_be) &&
          memcmp(buf, result_be, sizeof(result_be)) == 0);
  printf("Pack endian test: ");
  if (res1 && res2)
    printf("PASS\n");
  else
    printf("FAIL\n");
}


x86 assembly (clang, -O0):
<test_struct_pack_endian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x290,%rsp
lea    -0x200(%rbp),%rdi
lea    0xeb7(%rip),%rsi
mov    $0x6c,%edx
call   1050 <memcpy@plt>
lea    -0x270(%rbp),%rdi
lea    0xf0f(%rip),%rsi
mov    $0x6c,%edx
call   1050 <memcpy@plt>
lea    -0x190(%rbp),%rdi
mov    $0x190,%esi
lea    0xf5e(%rip),%rdx
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    $0x2,%r9d
movss  0xe5c(%rip),%xmm0
movsd  0xe5c(%rip),%xmm1
movq   $0x3,(%rsp)
call   1060 <struct_pack@plt>
mov    %eax,-0x194(%rbp)
movslq -0x194(%rbp),%rcx
xor    %eax,%eax
cmp    $0x6c,%rcx
mov    %al,-0x279(%rbp)
jne    11fd <test_struct_pack_endian+0xbd>
lea    -0x190(%rbp),%rdi
lea    -0x200(%rbp),%rsi
mov    $0x6c,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
sete   %al
mov    %al,-0x279(%rbp)
mov    -0x279(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x274(%rbp)
lea    -0x190(%rbp),%rdi
mov    $0x190,%esi
lea    0xed3(%rip),%rdx
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    $0x2,%r9d
movss  0xdc9(%rip),%xmm0
movsd  0xdc9(%rip),%xmm1
movq   $0x3,(%rsp)
call   1060 <struct_pack@plt>
mov    %eax,-0x194(%rbp)
movslq -0x194(%rbp),%rcx
xor    %eax,%eax
cmp    $0x6c,%rcx
mov    %al,-0x27a(%rbp)
jne    1290 <test_struct_pack_endian+0x150>
lea    -0x190(%rbp),%rdi
lea    -0x270(%rbp),%rsi
mov    $0x6c,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
sete   %al
mov    %al,-0x27a(%rbp)
mov    -0x27a(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x278(%rbp)
lea    0xe54(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x274(%rbp)
je     12d8 <test_struct_pack_endian+0x198>
cmpl   $0x0,-0x278(%rbp)
je     12d8 <test_struct_pack_endian+0x198>
lea    0xe41(%rip),%rdi
call   1030 <printf@plt>
jmp    12e4 <test_struct_pack_endian+0x1a4>
lea    0xe36(%rip),%rdi
call   1030 <printf@plt>
add    $0x290,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_struct_pack_endian>:
push   %rbp
push   %rbx
sub    $0x288,%rsp
movups 0xf1c(%rip),%xmm0
movups %xmm0,0xdc(%rsp)
movaps 0xf01(%rip),%xmm0
movaps %xmm0,0xd0(%rsp)
movaps 0xee2(%rip),%xmm0
movaps %xmm0,0xc0(%rsp)
movaps 0xec3(%rip),%xmm0
movaps %xmm0,0xb0(%rsp)
movaps 0xea4(%rip),%xmm0
movaps %xmm0,0xa0(%rsp)
movaps 0xe85(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0xe66(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
movups 0xf23(%rip),%xmm0
movups %xmm0,0x6c(%rsp)
movaps 0xf0b(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xeef(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xed3(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xeb7(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe9b(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe7f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x3,(%rsp)
lea    0xed7(%rip),%rdx
lea    0xf0(%rsp),%rdi
xor    %ebp,%ebp
movss  0xdd9(%rip),%xmm0
movsd  0xdd9(%rip),%xmm1
mov    $0x190,%esi
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    $0x2,%r9d
call   1060 <struct_pack@plt>
cmp    $0x6c,%eax
jne    126d <test_struct_pack_endian+0x12d>
lea    0xf0(%rsp),%rdi
lea    0x80(%rsp),%rsi
mov    $0x6c,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
sete   %bpl
movq   $0x3,(%rsp)
lea    0xe78(%rip),%rdx
lea    0xf0(%rsp),%rdi
movss  0xd74(%rip),%xmm0
movsd  0xd74(%rip),%xmm1
mov    $0x190,%esi
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    $0x2,%r9d
call   1060 <struct_pack@plt>
cmp    $0x6c,%eax
jne    12e8 <test_struct_pack_endian+0x1a8>
lea    0xf0(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    $0x6c,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
sete   %bl
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
test   %bl,%bpl
je     12f4 <test_struct_pack_endian+0x1b4>
lea    0xe2e(%rip),%rdi
jmp    12fb <test_struct_pack_endian+0x1bb>
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
add    $0x288,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.test_struct_pack_endian.result_le = [27 x i32] [i32 0, i32 1, i32 0, i32 2, i32 0, i32 0, i32 0, i32 3, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 128, i32 64, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 20, i32 64]
@__const.test_struct_pack_endian.result_be = [27 x i32] [i32 0, i32 0, i32 1, i32 0, i32 0, i32 0, i32 2, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 3, i32 64, i32 128, i32 0, i32 0, i32 64, i32 20, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0]
@.str = [8 x i8] c"<bhiqfd\00"
@.str.1 = [8 x i8] c">bhiqfd\00"
@.str.2 = [19 x i8] c"Pack 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_pack_endian() {
%1 = alloca [100 x i32]
%2 = alloca i32
%3 = alloca [27 x i32]
%4 = alloca [27 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = bitcast [27 x i32]* %3 to i8*
%8 = bitcast [27 x i32]* %4 to i8*
%9 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%10 = call i32 @struct_pack(i32* %9, i32 400, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 0, i32 1, i32 2, i64 3, float 4.000000e+00, double 5.000000e+00)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = icmp eq i64 %12, 108
br i1 %13, label %14, label %19
14: ; preds = %0
%15 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%16 = getelementptr inbounds [27 x i32], [27 x i32]* %3, i64 0, i64 0
%17 = call i64 @memcmp(i32* %15, i32* %16, i32 108)
%18 = icmp eq i64 %17, 0
br label %19
19: ; preds = %14, %0
%20 = phi i1 [ false, %0 ], [ %18, %14 ]
%21 = zext i1 %20 to i32
store i32 %21, i32* %5
%22 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%23 = call i32 @struct_pack(i32* %22, i32 400, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 1, i32 2, i64 3, float 4.000000e+00, double 5.000000e+00)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = icmp eq i64 %25, 108
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%29 = getelementptr inbounds [27 x i32], [27 x i32]* %4, i64 0, i64 0
%30 = call i64 @memcmp(i32* %28, i32* %29, i32 108)
%31 = icmp eq i64 %30, 0
br label %32
32: ; preds = %27, %19
%33 = phi i1 [ false, %19 ], [ %31, %27 ]
%34 = zext i1 %33 to i32
store i32 %34, i32* %6
%35 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %5
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %32
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %45
43: ; preds = %38, %32
%44 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %41
ret void
}

LLVM IR (-O2):
@__const.test_struct_pack_endian.result_le = [27 x i32] [i32 0, i32 1, i32 0, i32 2, i32 0, i32 0, i32 0, i32 3, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 128, i32 64, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 20, i32 64]
@__const.test_struct_pack_endian.result_be = [27 x i32] [i32 0, i32 0, i32 1, i32 0, i32 0, i32 0, i32 2, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 3, i32 64, i32 128, i32 0, i32 0, i32 64, i32 20, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0]
@.str = [8 x i8] c"<bhiqfd\00"
@.str.1 = [8 x i8] c">bhiqfd\00"
@.str.2 = [19 x i8] c"Pack endian test: \00"
@str = [5 x i8] c"FAIL\00"
@str.5 = [5 x i8] c"PASS\00"
define void @test_struct_pack_endian() {
%1 = alloca [100 x i32]
%2 = alloca [27 x i32]
%3 = alloca [27 x i32]
%4 = bitcast [100 x i32]* %1 to i8*
%5 = bitcast [27 x i32]* %2 to i8*
%6 = bitcast [27 x i32]* %3 to i8*
%7 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%8 = call i32 @struct_pack(i32* %7, i32 400, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 0, i32 1, i32 2, i64 3, float 4.000000e+00, double 5.000000e+00)
%9 = icmp eq i32 %8, 108
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = getelementptr inbounds [27 x i32], [27 x i32]* %2, i64 0, i64 0
%12 = call i64 @memcmp(i32* %7, i32* %11, i32 108)
%13 = icmp eq i64 %12, 0
br label %14
14: ; preds = %10, %0
%15 = phi i1 [ false, %0 ], [ %13, %10 ]
%16 = call i32 @struct_pack(i32* %7, i32 400, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 1, i32 2, i64 3, float 4.000000e+00, double 5.000000e+00)
%17 = icmp eq i32 %16, 108
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %26
20: ; preds = %14
%21 = getelementptr inbounds [27 x i32], [27 x i32]* %3, i64 0, i64 0
%22 = call i64 @memcmp(i32* %7, i32* %21, i32 108)
%23 = icmp eq i64 %22, 0
%24 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%25 = select i1 %15, i1 %23, i1 false
br i1 %25, label %27, label %26
26: ; preds = %18, %20
br label %27
27: ; preds = %20, %26
%28 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0), %26 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @str.5, i64 0, i64 0), %20 ]
%29 = call i32 @puts(i8* dereferenceable(1) %28)
ret void
}