#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
int ax25_compute_crc(int *, int);
int /*<<< orphan*/ memcpy(int *, int *, int);
uint32_t ax25_create_ui_packet(uint8_t *src_in, uint8_t *dst_in,
                               uint8_t *payload_in, uint16_t payload_length,
                               uint8_t *packet_out) {
  uint16_t crc = 0;
  uint16_t len = 0;
  uint16_t ax25_len = 0;
  if (payload_length > (256) || packet_out == NULL)
    return 0;
  memcpy(packet_out + 0, dst_in, 7);
  len += 7;
  memcpy(packet_out + (0 + 7), src_in, 7);
  len += 7;
  packet_out[((0 + 7) + 7)] = 0x03;
  len += 1;
  packet_out[(((0 + 7) + 7) + 1)] = 0xF0;
  len += 1;
  memcpy(packet_out + ((((0 + 7) + 7) + 1) + 1), payload_in, payload_length);
  len += payload_length;
  crc = ax25_compute_crc(packet_out, len);
  packet_out[len] = crc >> 8 & 0xFF;
  packet_out[len + 1] = crc & 0xFF;
  len += 2;
  return len;
}


x86 assembly (clang, -O0):
<ax25_create_ui_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x100,-0x24(%rbp)
jg     1168 <ax25_create_ui_packet+0x48>
cmpq   $0x0,-0x30(%rbp)
jne    1174 <ax25_create_ui_packet+0x54>
movl   $0x0,-0x4(%rbp)
jmp    1241 <ax25_create_ui_packet+0x121>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x7,%edx
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%eax
add    $0x7,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1c,%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%eax
add    $0x7,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movl   $0x3,0x38(%rax)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movl   $0xf0,0x3c(%rax)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x40,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x24(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1040 <ax25_compute_crc@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edx
sar    $0x8,%edx
and    $0xff,%edx
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%edx
and    $0xff,%edx
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ax25_create_ui_packet>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
cmp    $0x100,%ecx
jg     11a2 <ax25_create_ui_packet+0x82>
mov    %r8,%rbx
test   %r8,%r8
je     11a2 <ax25_create_ui_packet+0x82>
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rdi,%r12
mov    %rbx,%rdi
mov    $0x7,%edx
call   1030 <memcpy@plt>
lea    0x1c(%rbx),%rdi
mov    %r12,%rsi
mov    $0x7,%edx
call   1030 <memcpy@plt>
movabs $0xf000000003,%rax
mov    %rax,0x38(%rbx)
lea    0x40(%rbx),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
lea    0x10(%r14),%esi
mov    %rbx,%rdi
call   1040 <ax25_compute_crc@plt>
movzbl %ah,%ecx
movslq %r14d,%rdx
mov    %ecx,0x40(%rbx,%rdx,4)
movzbl %al,%eax
mov    %eax,0x44(%rbx,%rdx,4)
add    $0x12,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @ax25_create_ui_packet(i32* %0, i32* %1, i32* %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%15 = load i32, i32* %10
%16 = icmp sgt i32 %15, 256
br i1 %16, label %20, label %17
17: ; preds = %5
%18 = load i32*, i32** %11
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17, %5
store i32 0, i32* %6
br label %70
21: ; preds = %17
%22 = load i32*, i32** %11
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32*, i32** %8
%25 = call i32 @memcpy(i32* %23, i32* %24, i32 7)
%26 = load i32, i32* %13
%27 = add nsw i32 %26, 7
store i32 %27, i32* %13
%28 = load i32*, i32** %11
%29 = getelementptr inbounds i32, i32* %28, i64 7
%30 = load i32*, i32** %7
%31 = call i32 @memcpy(i32* %29, i32* %30, i32 7)
%32 = load i32, i32* %13
%33 = add nsw i32 %32, 7
store i32 %33, i32* %13
%34 = load i32*, i32** %11
%35 = getelementptr inbounds i32, i32* %34, i64 14
store i32 3, i32* %35
%36 = load i32, i32* %13
%37 = add nsw i32 %36, 1
store i32 %37, i32* %13
%38 = load i32*, i32** %11
%39 = getelementptr inbounds i32, i32* %38, i64 15
store i32 240, i32* %39
%40 = load i32, i32* %13
%41 = add nsw i32 %40, 1
store i32 %41, i32* %13
%42 = load i32*, i32** %11
%43 = getelementptr inbounds i32, i32* %42, i64 16
%44 = load i32*, i32** %9
%45 = load i32, i32* %10
%46 = call i32 @memcpy(i32* %43, i32* %44, i32 %45)
%47 = load i32, i32* %10
%48 = load i32, i32* %13
%49 = add nsw i32 %48, %47
store i32 %49, i32* %13
%50 = load i32*, i32** %11
%51 = load i32, i32* %13
%52 = call i32 @ax25_compute_crc(i32* %50, i32 %51)
store i32 %52, i32* %12
%53 = load i32, i32* %12
%54 = ashr i32 %53, 8
%55 = and i32 %54, 255
%56 = load i32*, i32** %11
%57 = load i32, i32* %13
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %56, i64 %58
store i32 %55, i32* %59
%60 = load i32, i32* %12
%61 = and i32 %60, 255
%62 = load i32*, i32** %11
%63 = load i32, i32* %13
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %62, i64 %65
store i32 %61, i32* %66
%67 = load i32, i32* %13
%68 = add nsw i32 %67, 2
store i32 %68, i32* %13
%69 = load i32, i32* %13
store i32 %69, i32* %6
br label %70
70: ; preds = %21, %20
%71 = load i32, i32* %6
ret i32 %71
}

LLVM IR (-O2):
define i32 @ax25_create_ui_packet(i32* %0, i32* %1, i32* %2, i32 %3, i32* %4) {
%6 = icmp sgt i32 %3, 256
%7 = icmp eq i32* %4, null
%8 = or i1 %6, %7
br i1 %8, label %28, label %9
9: ; preds = %5
%10 = tail call i32 @memcpy(i32* %4, i32* %1, i32 7)
%11 = getelementptr inbounds i32, i32* %4, i64 7
%12 = tail call i32 @memcpy(i32* %11, i32* %0, i32 7)
%13 = getelementptr inbounds i32, i32* %4, i64 14
store i32 3, i32* %13
%14 = getelementptr inbounds i32, i32* %4, i64 15
store i32 240, i32* %14
%15 = getelementptr inbounds i32, i32* %4, i64 16
%16 = tail call i32 @memcpy(i32* %15, i32* %2, i32 %3)
%17 = add nsw i32 %3, 16
%18 = tail call i32 @ax25_compute_crc(i32* %4, i32 %17)
%19 = lshr i32 %18, 8
%20 = and i32 %19, 255
%21 = sext i32 %17 to i64
%22 = getelementptr inbounds i32, i32* %4, i64 %21
store i32 %20, i32* %22
%23 = and i32 %18, 255
%24 = add nsw i32 %3, 17
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %4, i64 %25
store i32 %23, i32* %26
%27 = add nsw i32 %3, 18
br label %28
28: ; preds = %5, %9
%29 = phi i32 [ %27, %9 ], [ 0, %5 ]
ret i32 %29
}