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

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
scalar_t__ struct_pack(int /*<<< orphan*/ *, int, char *, int /*<<< orphan*/);
void test_struct_pack_errors(void) {
  uint8_t buf[100];
  ssize_t res1, res2, res3, res4, res5;
  res1 = struct_pack(NULL, sizeof(buf), "i", 0);
  res2 = struct_pack(buf, 0, "i", 0);
  res3 = struct_pack(buf, sizeof(buf), NULL, 0);
  res4 = struct_pack(buf, sizeof(buf), "abc", 0);
  res5 = struct_pack(buf, sizeof(buf), "1", 0);
  printf("Struct pack errors test: ");
  if (res1 < 0 && res2 < 0 && res3 < 0 && res4 < 0 && res5 < 0)
    printf("PASS\n");
  else
    printf("FAIL\n");
}


x86 assembly (clang, -O0):
<test_struct_pack_errors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
xor    %eax,%eax
mov    %eax,%edi
mov    $0x190,%esi
lea    0xec5(%rip),%rdx
xor    %ecx,%ecx
call   1040 <struct_pack@plt>
mov    %rax,-0x198(%rbp)
lea    -0x190(%rbp),%rdi
xor    %ecx,%ecx
lea    0xea7(%rip),%rdx
mov    %ecx,%esi
call   1040 <struct_pack@plt>
mov    %rax,-0x1a0(%rbp)
lea    -0x190(%rbp),%rdi
mov    $0x190,%esi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <struct_pack@plt>
mov    %rax,-0x1a8(%rbp)
lea    -0x190(%rbp),%rdi
mov    $0x190,%esi
lea    0xe6a(%rip),%rdx
xor    %ecx,%ecx
call   1040 <struct_pack@plt>
mov    %rax,-0x1b0(%rbp)
lea    -0x190(%rbp),%rdi
mov    $0x190,%esi
lea    0xe4d(%rip),%rdx
xor    %ecx,%ecx
call   1040 <struct_pack@plt>
mov    %rax,-0x1b8(%rbp)
lea    0xe3a(%rip),%rdi
call   1030 <printf@plt>
cmpq   $0x0,-0x198(%rbp)
jge    122a <test_struct_pack_errors+0x10a>
cmpq   $0x0,-0x1a0(%rbp)
jge    122a <test_struct_pack_errors+0x10a>
cmpq   $0x0,-0x1a8(%rbp)
jge    122a <test_struct_pack_errors+0x10a>
cmpq   $0x0,-0x1b0(%rbp)
jge    122a <test_struct_pack_errors+0x10a>
cmpq   $0x0,-0x1b8(%rbp)
jge    122a <test_struct_pack_errors+0x10a>
lea    0xe02(%rip),%rdi
call   1030 <printf@plt>
jmp    1236 <test_struct_pack_errors+0x116>
lea    0xdf7(%rip),%rdi
call   1030 <printf@plt>
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_struct_pack_errors>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x198,%rsp
lea    0xeb8(%rip),%rbx
xor    %edi,%edi
mov    $0x190,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <struct_pack@plt>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <struct_pack@plt>
mov    %rax,%rbp
mov    %r15,%rdi
mov    $0x190,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <struct_pack@plt>
mov    %rax,%r12
lea    0xe76(%rip),%rdx
mov    %r15,%rdi
mov    $0x190,%esi
xor    %ecx,%ecx
call   1050 <struct_pack@plt>
mov    %rax,%r13
lea    0xe61(%rip),%rdx
mov    %r15,%rdi
mov    $0x190,%esi
xor    %ecx,%ecx
call   1050 <struct_pack@plt>
mov    %rax,%rbx
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
and    %r14,%rbp
and    %r13,%rbx
and    %r12,%rbx
lea    0xe54(%rip),%rax
lea    0xe48(%rip),%rdi
test   %rbx,%rbp
cmovs  %rax,%rdi
call   1030 <puts@plt>
add    $0x198,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"i\00"
@.str.1 = [4 x i8] c"abc\00"
@.str.2 = [2 x i8] c"1\00"
@.str.3 = [26 x i8] c"Struct pack errors test: \00"
@.str.4 = [6 x i8] c"PASS\0A\00"
@.str.5 = [6 x i8] c"FAIL\0A\00"
define void @test_struct_pack_errors() {
%1 = alloca [100 x i32]
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = call i64 @struct_pack(i32* null, i32 400, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 0)
store i64 %7, i64* %2
%8 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%9 = call i64 @struct_pack(i32* %8, i32 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 0)
store i64 %9, i64* %3
%10 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%11 = call i64 @struct_pack(i32* %10, i32 400, i8* null, i32 0)
store i64 %11, i64* %4
%12 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%13 = call i64 @struct_pack(i32* %12, i32 400, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i64 %13, i64* %5
%14 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%15 = call i64 @struct_pack(i32* %14, i32 400, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i64 %15, i64* %6
%16 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%17 = load i64, i64* %2
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %0
%20 = load i64, i64* %3
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %19
%23 = load i64, i64* %4
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i64, i64* %5
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i64, i64* %6
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %35
33: ; preds = %28, %25, %22, %19, %0
%34 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %31
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"i\00"
@.str.1 = [4 x i8] c"abc\00"
@.str.2 = [2 x i8] c"1\00"
@.str.3 = [26 x i8] c"Struct pack errors test: \00"
@str = [5 x i8] c"FAIL\00"
@str.6 = [5 x i8] c"PASS\00"
define void @test_struct_pack_errors() {
%1 = alloca [100 x i32]
%2 = bitcast [100 x i32]* %1 to i8*
%3 = tail call i64 @struct_pack(i32* null, i32 400, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%5 = call i64 @struct_pack(i32* %4, i32 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 0)
%6 = call i64 @struct_pack(i32* %4, i32 400, i8* null, i32 0)
%7 = call i64 @struct_pack(i32* %4, i32 400, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 0)
%8 = call i64 @struct_pack(i32* %4, i32 400, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 0)
%9 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%10 = icmp slt i64 %3, 0
%11 = icmp slt i64 %5, 0
%12 = select i1 %10, i1 %11, i1 false
%13 = icmp slt i64 %6, 0
%14 = select i1 %12, i1 %13, i1 false
%15 = icmp slt i64 %7, 0
%16 = select i1 %14, i1 %15, i1 false
%17 = icmp slt i64 %8, 0
%18 = select i1 %16, i1 %17, i1 false
%19 = select i1 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0)
%20 = call i32 @puts(i8* dereferenceable(1) %19)
ret void
}