#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 /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ src;
typedef int /*<<< orphan*/ pkt;
typedef int /*<<< orphan*/ dst;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int build_tcp_packet(int *, int, int, int /*<<< orphan*/, int /*<<< orphan*/,
                     int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                     int /*<<< orphan*/, int *, size_t);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int ntohl(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int sendto(int, int *, int, int /*<<< orphan*/, struct sockaddr *, int);
char *strerror(int /*<<< orphan*/);
char inject_tcp_packet(uint8_t tcph_flags, uint32_t iph_saddr,
                       uint32_t iph_daddr, uint16_t tcph_sport,
                       uint16_t tcph_dport, uint32_t tcph_seq,
                       uint32_t tcph_ack, uint8_t *payload, size_t payload_len,
                       int sock, struct sockaddr *addr, int addrlen) {
  uint8_t pkt[65535];
  int len = build_tcp_packet(pkt, sizeof(pkt), tcph_flags, iph_saddr, iph_daddr,
                             tcph_sport, tcph_dport, tcph_seq, tcph_ack,
                             payload, payload_len);
  if (len > 0) {
    static char src[32];
    static char dst[32];
    inet_ntop(AF_INET, &(iph_saddr), src, sizeof(src));
    inet_ntop(AF_INET, &(iph_daddr), dst, sizeof(dst));
    printf("inject_tcp_packet: 0x%02x %s -> %s %u - %u\n", tcph_flags, src, dst,
           ntohl(tcph_seq), ntohl(tcph_ack));
    if (sendto(sock, pkt, len, 0, (struct sockaddr *)&addr, addrlen) != len) {
      printf("sendto(): %s\n", strerror(errno));
    }
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<inject_tcp_packet>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x40060,%rsp
mov    0x38(%rbp),%eax
mov    0x30(%rbp),%rax
mov    0x28(%rbp),%eax
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%eax
mov    %edi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %edx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
lea    -0x40030(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
mov    -0x28(%rbp),%r14d
mov    -0x2c(%rbp),%ebx
mov    0x10(%rbp),%r11d
mov    0x18(%rbp),%r10
mov    0x20(%rbp),%rax
mov    $0x3fffc,%esi
mov    %r14d,(%rsp)
mov    %ebx,0x8(%rsp)
mov    %r11d,0x10(%rsp)
mov    %r10,0x18(%rsp)
mov    %rax,0x20(%rsp)
call   1060 <build_tcp_packet@plt>
mov    %eax,-0x40034(%rbp)
cmpl   $0x0,-0x40034(%rbp)
jle    12cd <inject_tcp_packet+0x16d>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
lea    0x2e59(%rip),%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
lea    0x2e5b(%rip),%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4003c(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <ntohl@plt>
mov    %eax,-0x40038(%rbp)
mov    0x10(%rbp),%edi
call   1070 <ntohl@plt>
mov    -0x4003c(%rbp),%esi
mov    -0x40038(%rbp),%r8d
mov    %eax,%r9d
lea    0xd9b(%rip),%rdi
lea    0x2df4(%rip),%rdx
lea    0x2e0d(%rip),%rcx
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x28(%rbp),%edi
lea    -0x40030(%rbp),%rsi
mov    -0x40034(%rbp),%edx
lea    0x30(%rbp),%r8
mov    0x38(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
cmp    -0x40034(%rbp),%eax
je     12c4 <inject_tcp_packet+0x164>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movb   $0x1,-0x11(%rbp)
jmp    12d1 <inject_tcp_packet+0x171>
movb   $0x0,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
add    $0x40060,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<inject_tcp_packet>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40038,%rsp
mov    %r9d,%ebx
mov    %ecx,%r9d
mov    %edx,%eax
mov    %esi,%ecx
mov    %edi,%r14d
mov    0x40070(%rsp),%r12d
movups 0x40078(%rsp),%xmm0
mov    %esi,0x2c(%rsp)
mov    %edx,0x28(%rsp)
movups %xmm0,0x18(%rsp)
mov    %r12d,0x10(%rsp)
mov    %ebx,0x8(%rsp)
mov    %r8d,(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x3fffc,%esi
mov    %r14d,%edx
mov    %eax,%r8d
call   1060 <build_tcp_packet@plt>
test   %eax,%eax
jle    1282 <inject_tcp_packet+0x122>
mov    %eax,%ebp
mov    0x2e1a(%rip),%r13
mov    0x0(%r13),%edi
lea    0x2e87(%rip),%r15
lea    0x2c(%rsp),%rsi
mov    %r15,%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
mov    0x0(%r13),%edi
lea    0x2e8a(%rip),%r13
lea    0x28(%rsp),%rsi
mov    %r13,%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
mov    %ebx,%edi
call   1070 <ntohl@plt>
mov    %eax,%ebx
mov    %r12d,%edi
call   1070 <ntohl@plt>
lea    0xde0(%rip),%rdi
mov    %r14d,%esi
mov    %r15,%rdx
mov    %r13,%rcx
mov    %ebx,%r8d
mov    %eax,%r9d
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x30(%rsp),%rsi
lea    0x40090(%rsp),%r8
mov    0x40088(%rsp),%edi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    0x40098(%rsp),%r9d
call   1050 <sendto@plt>
mov    $0x1,%bl
cmp    %ebp,%eax
je     1284 <inject_tcp_packet+0x124>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdb6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1284 <inject_tcp_packet+0x124>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x40038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@inject_tcp_packet.src = internal global [32 x i8] zeroinitializer
@inject_tcp_packet.dst = internal global [32 x i8] zeroinitializer
@AF_INET = global i32 0
@.str = [44 x i8] c"inject_tcp_packet: 0x%02x %s -> %s %u - %u\0A\00"
@.str.1 = [14 x i8] c"sendto(): %s\0A\00"
@errno = global i32 0
define signext i8 @inject_tcp_packet(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32* %7, i64 %8, i32 %9, %struct.sockaddr* %10, i32 %11) {
%13 = alloca i8
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32*
%22 = alloca i64
%23 = alloca i32
%24 = alloca %struct.sockaddr*
%25 = alloca i32
%26 = alloca [65535 x i32]
%27 = alloca i32
store i32 %0, i32* %14
store i32 %1, i32* %15
store i32 %2, i32* %16
store i32 %3, i32* %17
store i32 %4, i32* %18
store i32 %5, i32* %19
store i32 %6, i32* %20
store i32* %7, i32** %21
store i64 %8, i64* %22
store i32 %9, i32* %23
store %struct.sockaddr* %10, %struct.sockaddr** %24
store i32 %11, i32* %25
%28 = getelementptr inbounds [65535 x i32], [65535 x i32]* %26, i64 0, i64 0
%29 = load i32, i32* %14
%30 = load i32, i32* %15
%31 = load i32, i32* %16
%32 = load i32, i32* %17
%33 = load i32, i32* %18
%34 = load i32, i32* %19
%35 = load i32, i32* %20
%36 = load i32*, i32** %21
%37 = load i64, i64* %22
%38 = call i32 @build_tcp_packet(i32* %28, i32 262140, i32 %29, i32 %30, i32 %31, i32 %32, i32 %33, i32 %34, i32 %35, i32* %36, i64 %37)
store i32 %38, i32* %27
%39 = load i32, i32* %27
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %65
41: ; preds = %12
%42 = load i32, i32* @AF_INET
%43 = call i32 @inet_ntop(i32 %42, i32* %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.src, i64 0, i64 0), i32 32)
%44 = load i32, i32* @AF_INET
%45 = call i32 @inet_ntop(i32 %44, i32* %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.dst, i64 0, i64 0), i32 32)
%46 = load i32, i32* %14
%47 = load i32, i32* %19
%48 = call i32 @ntohl(i32 %47)
%49 = load i32, i32* %20
%50 = call i32 @ntohl(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %46, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.src, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.dst, i64 0, i64 0), i32 %48, i32 %50)
%52 = load i32, i32* %23
%53 = getelementptr inbounds [65535 x i32], [65535 x i32]* %26, i64 0, i64 0
%54 = load i32, i32* %27
%55 = bitcast %struct.sockaddr** %24 to %struct.sockaddr*
%56 = load i32, i32* %25
%57 = call i32 @sendto(i32 %52, i32* %53, i32 %54, i32 0, %struct.sockaddr* %55, i32 %56)
%58 = load i32, i32* %27
%59 = icmp ne i32 %57, %58
br i1 %59, label %60, label %64
60: ; preds = %41
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %60, %41
store i8 1, i8* %13
br label %66
65: ; preds = %12
store i8 0, i8* %13
br label %66
66: ; preds = %65, %64
%67 = load i8, i8* %13
ret i8 %67
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@inject_tcp_packet.src = internal global [32 x i8] zeroinitializer
@inject_tcp_packet.dst = internal global [32 x i8] zeroinitializer
@AF_INET = global i32 0
@.str = [44 x i8] c"inject_tcp_packet: 0x%02x %s -> %s %u - %u\0A\00"
@.str.1 = [14 x i8] c"sendto(): %s\0A\00"
@errno = global i32 0
define signext i8 @inject_tcp_packet(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32* %7, i64 %8, i32 %9, %struct.sockaddr* %10, i32 %11) {
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.sockaddr*
%16 = alloca [65535 x i32]
store i32 %1, i32* %13
store i32 %2, i32* %14
store %struct.sockaddr* %10, %struct.sockaddr** %15
%17 = bitcast [65535 x i32]* %16 to i8*
%18 = getelementptr inbounds [65535 x i32], [65535 x i32]* %16, i64 0, i64 0
%19 = call i32 @build_tcp_packet(i32* %18, i32 262140, i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32* %7, i64 %8)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %36
21: ; preds = %12
%22 = load i32, i32* @AF_INET
%23 = call i32 @inet_ntop(i32 %22, i32* %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.src, i64 0, i64 0), i32 32)
%24 = load i32, i32* @AF_INET
%25 = call i32 @inet_ntop(i32 %24, i32* %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.dst, i64 0, i64 0), i32 32)
%26 = call i32 @ntohl(i32 %5)
%27 = call i32 @ntohl(i32 %6)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.src, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @inject_tcp_packet.dst, i64 0, i64 0), i32 %26, i32 %27)
%29 = bitcast %struct.sockaddr** %15 to %struct.sockaddr*
%30 = call i32 @sendto(i32 %9, i32* %18, i32 %19, i32 0, %struct.sockaddr* %29, i32 %11)
%31 = icmp eq i32 %30, %19
br i1 %31, label %36, label %32
32: ; preds = %21
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %12, %21, %32
%37 = phi i8 [ 1, %32 ], [ 1, %21 ], [ 0, %12 ]
ret i8 %37
}