#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
int struct_unpack(int *, int, char *, short *, short *, long *);
void test_struct_example_1_2(void) {
  short a, b;
  long c;
  ssize_t size;
  uint8_t buf[] = {0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00};
  size = struct_unpack(buf, sizeof(buf), "hhl", &a, &b, &c);
  printf("Example 1.2 test: ");
  if (size == sizeof(buf) && (a == 1) && (b == 2) && (c == 3))
    printf("PASS\n");
  else
    printf("FAIL\n");
}


x86 assembly (clang, -O0):
<test_struct_example_1_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xed1(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xece(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xecb(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xec8(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x20,%esi
lea    0xebc(%rip),%rdx
lea    -0x2(%rbp),%rcx
lea    -0x4(%rbp),%r8
lea    -0x10(%rbp),%r9
call   1040 <struct_unpack@plt>
mov    %eax,-0x14(%rbp)
lea    0xea5(%rip),%rdi
call   1030 <printf@plt>
movslq -0x14(%rbp),%rax
cmp    $0x20,%rax
jne    11c8 <test_struct_example_1_2+0xa8>
movswl -0x2(%rbp),%eax
cmp    $0x1,%eax
jne    11c8 <test_struct_example_1_2+0xa8>
movswl -0x4(%rbp),%eax
cmp    $0x2,%eax
jne    11c8 <test_struct_example_1_2+0xa8>
cmpq   $0x3,-0x10(%rbp)
jne    11c8 <test_struct_example_1_2+0xa8>
lea    0xe79(%rip),%rdi
call   1030 <printf@plt>
jmp    11d4 <test_struct_example_1_2+0xb4>
lea    0xe6e(%rip),%rdi
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_struct_example_1_2>:
push   %rbx
sub    $0x30,%rsp
movaps 0xed4(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xeb8(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xecc(%rip),%rdx
lea    0x10(%rsp),%rdi
lea    0x6(%rsp),%rcx
lea    0x4(%rsp),%r8
lea    0x8(%rsp),%r9
mov    $0x20,%esi
call   1050 <struct_unpack@plt>
mov    %eax,%ebx
lea    0xea9(%rip),%rdi
call   1040 <printf@plt>
movzwl 0x6(%rsp),%eax
xor    $0x1,%eax
movzwl 0x4(%rsp),%ecx
xor    $0x2,%ecx
cmpq   $0x3,0x8(%rsp)
lea    0xe9f(%rip),%rdi
lea    0xe93(%rip),%rdx
cmovne %rdx,%rdi
or     %ax,%cx
cmovne %rdx,%rdi
cmp    $0x20,%ebx
cmovne %rdx,%rdi
call   1030 <puts@plt>
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.test_struct_example_1_2.buf = [8 x i32] [i32 1, i32 0, i32 2, i32 0, i32 3, i32 0, i32 0, i32 0]
@.str = [4 x i8] c"hhl\00"
@.str.1 = [19 x i8] c"Example 1.2 test: \00"
@.str.2 = [6 x i8] c"PASS\0A\00"
@.str.3 = [6 x i8] c"FAIL\0A\00"
define void @test_struct_example_1_2() {
%1 = alloca i16
%2 = alloca i16
%3 = alloca i64
%4 = alloca i32
%5 = alloca [8 x i32]
%6 = bitcast [8 x i32]* %5 to i8*
%7 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%8 = call i32 @struct_unpack(i32* %7, i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i16* %1, i16* %2, i64* %3)
store i32 %8, i32* %4
%9 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = icmp eq i64 %11, 32
br i1 %12, label %13, label %26
13: ; preds = %0
%14 = load i16, i16* %1
%15 = sext i16 %14 to i32
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i16, i16* %2
%19 = sext i16 %18 to i32
%20 = icmp eq i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 3
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %28
26: ; preds = %21, %17, %13, %0
%27 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %24
ret void
}

LLVM IR (-O2):
@__const.test_struct_example_1_2.buf = [8 x i32] [i32 1, i32 0, i32 2, i32 0, i32 3, i32 0, i32 0, i32 0]
@.str = [4 x i8] c"hhl\00"
@.str.1 = [19 x i8] c"Example 1.2 test: \00"
@str = [5 x i8] c"FAIL\00"
@str.4 = [5 x i8] c"PASS\00"
define void @test_struct_example_1_2() {
%1 = alloca i16
%2 = alloca i16
%3 = alloca i64
%4 = alloca [8 x i32]
%5 = bitcast i16* %1 to i8*
%6 = bitcast i16* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast [8 x i32]* %4 to i8*
%9 = getelementptr inbounds [8 x i32], [8 x i32]* %4, i64 0, i64 0
%10 = call i32 @struct_unpack(i32* %9, i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i16* %1, i16* %2, i64* %3)
%11 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32 %10, 32
%13 = load i16, i16* %1
%14 = icmp eq i16 %13, 1
%15 = select i1 %12, i1 %14, i1 false
%16 = load i16, i16* %2
%17 = icmp eq i16 %16, 2
%18 = select i1 %15, i1 %17, i1 false
%19 = load i64, i64* %3
%20 = icmp eq i64 %19, 3
%21 = select i1 %18, i1 %20, i1 false
%22 = select i1 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0)
%23 = call i32 @puts(i8* dereferenceable(1) %22)
ret void
}