#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 ssize_t;
typedef int /*<<< orphan*/ record;

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
scalar_t__ strncmp(char *, char *, int);
int struct_unpack(char *, int, char *, char *, int, unsigned short *,
                  unsigned short *, char *);
void test_struct_example_2(void) {
  char record[] = "raymond   \x32\x12\x08\x01\x08";
  struct {
    char name[11];
    unsigned short serialnum;
    unsigned short school;
    signed char gradelevel;
  } student;
  ssize_t size;
  size = struct_unpack(record, sizeof(record), "<10sHHb", &student.name[0],
                       sizeof(student.name), &student.serialnum,
                       &student.school, &student.gradelevel);
  printf("Example 2 test: ");
  if (size == sizeof(record) - 1 &&
      strncmp("raymond   ", student.name, sizeof(student.name)) == 0 &&
      student.serialnum == 4658 && student.school == 264 &&
      student.gradelevel == 8)
    printf("PASS\n");
  else
    printf("FAIL\n");
}


x86 assembly (clang, -O0):
<test_struct_example_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xec1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xebe(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rcx
lea    -0x28(%rbp),%r9
add    $0xc,%r9
lea    -0x28(%rbp),%r10
add    $0xe,%r10
lea    -0x28(%rbp),%rax
add    $0x10,%rax
mov    $0x10,%esi
lea    0xe96(%rip),%rdx
mov    $0xb,%r8d
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1050 <struct_unpack@plt>
mov    %eax,-0x2c(%rbp)
lea    0xe80(%rip),%rdi
call   1040 <printf@plt>
movslq -0x2c(%rbp),%rax
cmp    $0xf,%rax
jne    1206 <test_struct_example_2+0xd6>
lea    -0x28(%rbp),%rsi
lea    0xe73(%rip),%rdi
mov    $0xb,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1206 <test_struct_example_2+0xd6>
movzwl -0x1c(%rbp),%eax
cmp    $0x1232,%eax
jne    1206 <test_struct_example_2+0xd6>
movzwl -0x1a(%rbp),%eax
cmp    $0x108,%eax
jne    1206 <test_struct_example_2+0xd6>
movsbl -0x18(%rbp),%eax
cmp    $0x8,%eax
jne    1206 <test_struct_example_2+0xd6>
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
jmp    1212 <test_struct_example_2+0xe2>
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_struct_example_2>:
push   %rbx
sub    $0x30,%rsp
movaps 0xeb4(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0x14(%rsp),%r9
lea    0x16(%rsp),%rax
lea    0x18(%rsp),%rbx
lea    0xea9(%rip),%rdx
lea    0x20(%rsp),%rdi
lea    0x8(%rsp),%rcx
mov    $0x10,%esi
mov    $0xb,%r8d
push   %rbx
push   %rax
call   1060 <struct_unpack@plt>
add    $0x10,%rsp
mov    %eax,%ebx
lea    0xe88(%rip),%rdi
call   1050 <printf@plt>
cmp    $0xf,%ebx
jne    11dd <test_struct_example_2+0x9d>
lea    0xe88(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    $0xb,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11dd <test_struct_example_2+0x9d>
movzwl 0x14(%rsp),%eax
cmp    $0x1232,%eax
jne    11dd <test_struct_example_2+0x9d>
movzwl 0x16(%rsp),%eax
cmp    $0x108,%eax
jne    11dd <test_struct_example_2+0x9d>
cmpb   $0x8,0x18(%rsp)
jne    11dd <test_struct_example_2+0x9d>
lea    0xe5e(%rip),%rdi
jmp    11e4 <test_struct_example_2+0xa4>
lea    0xe50(%rip),%rdi
call   1040 <puts@plt>
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.anon = type { [11 x i8], i16, i16, i8 }
@__const.test_struct_example_2.record = [16 x i8] c"raymond 2\12\08\01\08\00"
@.str = [8 x i8] c"<10sHHb\00"
@.str.1 = [17 x i8] c"Example 2 test: \00"
@.str.2 = [11 x i8] c"raymond \00"
@.str.3 = [6 x i8] c"PASS\0A\00"
@.str.4 = [6 x i8] c"FAIL\0A\00"
define void @test_struct_example_2() {
%1 = alloca [16 x i8]
%2 = alloca %struct.anon
%3 = alloca i32
%4 = bitcast [16 x i8]* %1 to i8*
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 0
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 0
%8 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 1
%9 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 2
%10 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 3
%11 = call i32 @struct_unpack(i8* %5, i32 16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %7, i32 11, i16* %8, i16* %9, i8* %10)
store i32 %11, i32* %3
%12 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = icmp eq i64 %14, 15
br i1 %15, label %16, label %38
16: ; preds = %0
%17 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 0
%18 = getelementptr inbounds [11 x i8], [11 x i8]* %17, i64 0, i64 0
%19 = call i64 @strncmp(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %18, i32 11)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %16
%22 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 1
%23 = load i16, i16* %22
%24 = zext i16 %23 to i32
%25 = icmp eq i32 %24, 4658
br i1 %25, label %26, label %38
26: ; preds = %21
%27 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 2
%28 = load i16, i16* %27
%29 = zext i16 %28 to i32
%30 = icmp eq i32 %29, 264
br i1 %30, label %31, label %38
31: ; preds = %26
%32 = getelementptr inbounds %struct.anon, %struct.anon* %2, i32 0, i32 3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 8
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %40
38: ; preds = %31, %26, %21, %16, %0
%39 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %36
ret void
}

LLVM IR (-O2):
%struct.anon = type { [11 x i8], i16, i16, i8 }
@__const.test_struct_example_2.record = [16 x i8] c"raymond 2\12\08\01\08\00"
@.str = [8 x i8] c"<10sHHb\00"
@.str.1 = [17 x i8] c"Example 2 test: \00"
@.str.2 = [11 x i8] c"raymond \00"
@str = [5 x i8] c"FAIL\00"
@str.5 = [5 x i8] c"PASS\00"
define void @test_struct_example_2() {
%1 = alloca [16 x i8]
%2 = alloca %struct.anon
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds %struct.anon, %struct.anon* %2, i64 0, i32 0, i64 0
%5 = getelementptr inbounds %struct.anon, %struct.anon* %2, i64 0, i32 1
%6 = getelementptr inbounds %struct.anon, %struct.anon* %2, i64 0, i32 2
%7 = getelementptr inbounds %struct.anon, %struct.anon* %2, i64 0, i32 3
%8 = call i32 @struct_unpack(i8* %3, i32 16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %4, i32 11, i16* %5, i16* %6, i8* %7)
%9 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32 %8, 15
br i1 %10, label %11, label %23
11: ; preds = %0
%12 = call i64 @strncmp(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 11)
%13 = icmp eq i64 %12, 0
%14 = load i16, i16* %5
%15 = icmp eq i16 %14, 4658
%16 = select i1 %13, i1 %15, i1 false
%17 = load i16, i16* %6
%18 = icmp eq i16 %17, 264
%19 = select i1 %16, i1 %18, i1 false
%20 = load i8, i8* %7
%21 = icmp eq i8 %20, 8
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %24, label %23
23: ; preds = %11, %0
br label %24
24: ; preds = %11, %23
%25 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0), %23 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @str.5, i64 0, i64 0), %11 ]
%26 = call i32 @puts(i8* dereferenceable(1) %25)
ret void
}