#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 */
unsigned int read_wordL(int);
int /*<<< orphan*/ write_word(int, int);
void tx_packet(unsigned char *tx_buffer, unsigned int tx_buffer_len) {
  unsigned int nReturn = 0;
  unsigned int i = 0;
  unsigned char *tx_send = (unsigned char *)tx_buffer;
  write_word(0x0004, 0x00C0);
  write_word(0x0006, tx_buffer_len);
  while (!(nReturn & 0x0100)) {
    write_word(0x000A, 0x0138);
    nReturn = read_wordL(0x000C);
  }
  tx_buffer_len = (tx_buffer_len + 1) >> 1;
  for (i = 0; i < tx_buffer_len; i++) {
    write_word(0x0000, *(tx_send++));
    tx_send += 2;
  }
}


x86 assembly (clang, -O0):
<tx_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x4,%edi
mov    $0xc0,%esi
call   1040 <write_word@plt>
mov    -0xc(%rbp),%esi
mov    $0x6,%edi
call   1040 <write_word@plt>
mov    -0x10(%rbp),%eax
and    $0x100,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    117e <tx_packet+0x5e>
jmp    119f <tx_packet+0x7f>
mov    $0xa,%edi
mov    $0x138,%esi
call   1040 <write_word@plt>
mov    $0xc,%edi
call   1030 <read_wordL@plt>
mov    %eax,-0x10(%rbp)
jmp    1161 <tx_packet+0x41>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
shr    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    11f1 <tx_packet+0xd1>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movzbl (%rax),%esi
xor    %edi,%edi
call   1040 <write_word@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b2 <tx_packet+0x92>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tx_packet>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x4,%edi
mov    $0xc0,%esi
call   1040 <write_word@plt>
mov    $0x6,%edi
mov    %ebp,%esi
call   1040 <write_word@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xa,%edi
mov    $0x138,%esi
call   1040 <write_word@plt>
mov    $0xc,%edi
call   1030 <read_wordL@plt>
test   $0x100,%eax
je     1150 <tx_packet+0x30>
add    $0x1,%ebp
cmp    $0x2,%ebp
jb     1193 <tx_packet+0x73>
shr    %ebp
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%esi
xor    %edi,%edi
call   1040 <write_word@plt>
add    $0x3,%rbx
add    $0xffffffff,%ebp
jne    1180 <tx_packet+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @tx_packet(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%8 = load i8*, i8** %3
store i8* %8, i8** %7
%9 = call i32 @write_word(i32 4, i32 192)
%10 = load i32, i32* %4
%11 = call i32 @write_word(i32 6, i32 %10)
br label %12
12: ; preds = %17, %2
%13 = load i32, i32* %5
%14 = and i32 %13, 256
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @write_word(i32 10, i32 312)
%19 = call i32 @read_wordL(i32 12)
store i32 %19, i32* %5
br label %12
20: ; preds = %12
%21 = load i32, i32* %4
%22 = add i32 %21, 1
%23 = lshr i32 %22, 1
store i32 %23, i32* %4
store i32 0, i32* %6
br label %24
24: ; preds = %36, %20
%25 = load i32, i32* %6
%26 = load i32, i32* %4
%27 = icmp ult i32 %25, %26
br i1 %27, label %28, label %39
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %7
%31 = load i8, i8* %29
%32 = zext i8 %31 to i32
%33 = call i32 @write_word(i32 0, i32 %32)
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i64 2
store i8* %35, i8** %7
br label %36
36: ; preds = %28
%37 = load i32, i32* %6
%38 = add i32 %37, 1
store i32 %38, i32* %6
br label %24
39: ; preds = %24
ret void
}

LLVM IR (-O2):
define void @tx_packet(i8* nocapture %0, i32 %1) {
%3 = tail call i32 @write_word(i32 4, i32 192)
%4 = tail call i32 @write_word(i32 6, i32 %1)
br label %5
5: ; preds = %2, %5
%6 = tail call i32 @write_word(i32 10, i32 312)
%7 = tail call i32 @read_wordL(i32 12)
%8 = and i32 %7, 256
%9 = icmp eq i32 %8, 0
br i1 %9, label %5, label %10
10: ; preds = %5
%11 = add i32 %1, 1
%12 = icmp ult i32 %11, 2
br i1 %12, label %24, label %13
13: ; preds = %10
%14 = lshr i32 %11, 1
br label %15
15: ; preds = %13, %15
%16 = phi i8* [ %21, %15 ], [ %0, %13 ]
%17 = phi i32 [ %22, %15 ], [ 0, %13 ]
%18 = load i8, i8* %16
%19 = zext i8 %18 to i32
%20 = tail call i32 @write_word(i32 0, i32 %19)
%21 = getelementptr inbounds i8, i8* %16, i64 3
%22 = add nuw nsw i32 %17, 1
%23 = icmp eq i32 %22, %14
br i1 %23, label %24, label %15
24: ; preds = %15, %10
ret void
}