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

/* Variables and functions */
int FormIoPacket(int Channel, int Value, unsigned char *Packet) {
  if (Channel < 0 || Channel > 077777)
    return (1);
  if (Value < 0 || Value > 077777)
    return (1);
  if (Packet == NULL)
    return (1);
  Packet[0] = Channel >> 3;
  Packet[1] = 0x40 | ((Channel << 3) & 0x38) | ((Value >> 12) & 0x07);
  Packet[2] = 0x80 | ((Value >> 6) & 0x3F);
  Packet[3] = 0xc0 | (Value & 0x3F);
  return (0);
}


x86 assembly (clang, -O0):
<FormIoPacket>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1125 <FormIoPacket+0x25>
cmpl   $0x7fff,-0x8(%rbp)
jle    1131 <FormIoPacket+0x31>
movl   $0x1,-0x4(%rbp)
jmp    11cb <FormIoPacket+0xcb>
cmpl   $0x0,-0xc(%rbp)
jl     1148 <FormIoPacket+0x48>
cmpl   $0x7fff,-0xc(%rbp)
jle    1154 <FormIoPacket+0x54>
movl   $0x1,-0x4(%rbp)
jmp    11cb <FormIoPacket+0xcb>
cmpq   $0x0,-0x18(%rbp)
jne    116b <FormIoPacket+0x6b>
movl   $0x1,-0x4(%rbp)
jmp    11cb <FormIoPacket+0xcb>
mov    -0x8(%rbp),%eax
sar    $0x3,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
shl    $0x3,%eax
and    $0x38,%eax
or     $0x40,%eax
mov    -0xc(%rbp),%ecx
sar    $0xc,%ecx
and    $0x7,%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0xc(%rbp),%eax
sar    $0x6,%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0xc(%rbp),%eax
and    $0x3f,%eax
or     $0xc0,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x3(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<FormIoPacket>:
mov    %esi,%ecx
or     %edi,%ecx
mov    $0x1,%eax
cmp    $0x7fff,%ecx
ja     1145 <FormIoPacket+0x45>
test   %rdx,%rdx
je     1145 <FormIoPacket+0x45>
mov    %edi,%eax
shr    $0x3,%eax
mov    %al,(%rdx)
shl    $0x3,%edi
and    $0x38,%edi
mov    %esi,%eax
shr    $0xc,%eax
or     %edi,%eax
or     $0x40,%al
mov    %al,0x1(%rdx)
mov    %esi,%eax
shr    $0x6,%eax
and    $0x3f,%al
or     $0x80,%al
mov    %al,0x2(%rdx)
or     $0xc0,%sil
mov    %sil,0x3(%rdx)
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @FormIoPacket(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 32767
br i1 %12, label %13, label %14
13: ; preds = %10, %3
store i32 1, i32* %4
br label %55
14: ; preds = %10
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 32767
br i1 %19, label %20, label %21
20: ; preds = %17, %14
store i32 1, i32* %4
br label %55
21: ; preds = %17
%22 = load i8*, i8** %7
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 1, i32* %4
br label %55
25: ; preds = %21
%26 = load i32, i32* %5
%27 = ashr i32 %26, 3
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 %28, i8* %30
%31 = load i32, i32* %5
%32 = shl i32 %31, 3
%33 = and i32 %32, 56
%34 = or i32 64, %33
%35 = load i32, i32* %6
%36 = ashr i32 %35, 12
%37 = and i32 %36, 7
%38 = or i32 %34, %37
%39 = trunc i32 %38 to i8
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i64 1
store i8 %39, i8* %41
%42 = load i32, i32* %6
%43 = ashr i32 %42, 6
%44 = and i32 %43, 63
%45 = or i32 128, %44
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i64 2
store i8 %46, i8* %48
%49 = load i32, i32* %6
%50 = and i32 %49, 63
%51 = or i32 192, %50
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %7
%54 = getelementptr inbounds i8, i8* %53, i64 3
store i8 %52, i8* %54
store i32 0, i32* %4
br label %55
55: ; preds = %25, %24, %20, %13
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
define i32 @FormIoPacket(i32 %0, i32 %1, i8* writeonly %2) {
%4 = or i32 %1, %0
%5 = icmp ugt i32 %4, 32767
%6 = icmp eq i8* %2, null
%7 = or i1 %5, %6
br i1 %7, label %26, label %8
8: ; preds = %3
%9 = lshr i32 %0, 3
%10 = trunc i32 %9 to i8
store i8 %10, i8* %2
%11 = shl nuw nsw i32 %0, 3
%12 = and i32 %11, 56
%13 = lshr i32 %1, 12
%14 = or i32 %12, %13
%15 = trunc i32 %14 to i8
%16 = or i8 %15, 64
%17 = getelementptr inbounds i8, i8* %2, i64 1
store i8 %16, i8* %17
%18 = lshr i32 %1, 6
%19 = trunc i32 %18 to i8
%20 = and i8 %19, 63
%21 = or i8 %20, -128
%22 = getelementptr inbounds i8, i8* %2, i64 2
store i8 %21, i8* %22
%23 = trunc i32 %1 to i8
%24 = or i8 %23, -64
%25 = getelementptr inbounds i8, i8* %2, i64 3
store i8 %24, i8* %25
br label %26
26: ; preds = %3, %8
%27 = phi i32 [ 0, %8 ], [ 1, %3 ]
ret i32 %27
}