#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 char uint16_t;
typedef int /*<<< orphan*/ n;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
char htons(char);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char *, char);
int write_pkt(int fd, char *buf, uint16_t len) {
  uint16_t n;
  n = htons(len);
  if ((write(fd, &n, sizeof(n))) < 0) {
    perror("write_pkt(size):");
    exit(1);
  }
  if ((write(fd, buf, len)) < 0) {
    perror("write_pkt(size):");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_pkt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%edi
call   1040 <htons@plt>
mov    %al,-0x12(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1191 <write_pkt+0x51>
lea    0xe7e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movsbl -0x11(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11c1 <write_pkt+0x81>
lea    0xe4e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pkt>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ebx
mov    %edx,%edi
call   1040 <htons@plt>
mov    %al,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
js     118e <write_pkt+0x4e>
movsbl %bpl,%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
test   %rax,%rax
js     118e <write_pkt+0x4e>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"write_pkt(size):\00"
define i32 @write_pkt(i32 %0, i8* %1, i8 signext %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8 %2, i8* %6
%8 = load i8, i8* %6
%9 = call signext i8 @htons(i8 signext %8)
store i8 %9, i8* %7
%10 = load i32, i32* %4
%11 = call i64 @write(i32 %10, i8* %7, i8 signext 1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = load i32, i32* %4
%18 = load i8*, i8** %5
%19 = load i8, i8* %6
%20 = call i64 @write(i32 %17, i8* %18, i8 signext %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"write_pkt(size):\00"
define i32 @write_pkt(i32 %0, i8* nocapture %1, i8 signext %2) {
%4 = alloca i8
%5 = tail call signext i8 @htons(i8 signext %2)
store i8 %5, i8* %4
%6 = call i64 @write(i32 %0, i8* %4, i8 signext 1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i64 @write(i32 %0, i8* %1, i8 signext %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11
ret i32 0
}