#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 /*<<< orphan*/ tos;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ IP_TOS;
int /*<<< orphan*/ SOL_IP;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int sendto(int, void *, int, int /*<<< orphan*/, struct sockaddr *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int send_packet(int sck, void *buf, int bufsz, struct sockaddr_in addr,
                int tos) {
  int cc;
  static int prev_tos = -1;
  if (prev_tos != tos) {
    if (setsockopt(sck, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0) {
      perror("setsockopt (IP_TOS)");
      exit(1);
    }
    prev_tos = tos;
  }
  cc = sendto(sck, buf, bufsz, 0, (struct sockaddr *)&addr, sizeof(addr));
  return cc;
}


x86 assembly (clang, -O0):
<send_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %ecx,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    0x2ee1(%rip),%eax
cmp    -0x20(%rbp),%eax
je     11b5 <send_packet+0x75>
mov    -0xc(%rbp),%edi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11ac <send_packet+0x6c>
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,0x2e8b(%rip)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x8(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_packet>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %ecx,0x8(%rsp)
mov    %r8d,0x4(%rsp)
cmp    %r8d,0x2ee0(%rip)
je     1195 <send_packet+0x55>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11b8 <send_packet+0x78>
mov    0x4(%rsp),%eax
mov    %eax,0x2eab(%rip)
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1040 <sendto@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@send_packet.prev_tos = internal global i32 -1
@SOL_IP = global i32 0
@IP_TOS = global i32 0
@.str = [20 x i8] c"setsockopt (IP_TOS)\00"
define i32 @send_packet(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %3, i32* %12
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %4, i32* %10
%13 = load i32, i32* @send_packet.prev_tos
%14 = load i32, i32* %10
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %27
16: ; preds = %5
%17 = load i32, i32* %7
%18 = load i32, i32* @SOL_IP
%19 = load i32, i32* @IP_TOS
%20 = call i64 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %10, i32 4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* %10
store i32 %26, i32* @send_packet.prev_tos
br label %27
27: ; preds = %25, %5
%28 = load i32, i32* %7
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%32 = call i32 @sendto(i32 %28, i8* %29, i32 %30, i32 0, %struct.sockaddr* %31, i32 4)
store i32 %32, i32* %11
%33 = load i32, i32* %11
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@send_packet.prev_tos = internal unnamed_addr global i32 -1
@SOL_IP = global i32 0
@IP_TOS = global i32 0
@.str = [20 x i8] c"setsockopt (IP_TOS)\00"
define i32 @send_packet(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %3, i32* %8
store i32 %4, i32* %7
%9 = load i32, i32* @send_packet.prev_tos
%10 = icmp eq i32 %9, %4
br i1 %10, label %21, label %11
11: ; preds = %5
%12 = load i32, i32* @SOL_IP
%13 = load i32, i32* @IP_TOS
%14 = call i64 @setsockopt(i32 %0, i32 %12, i32 %13, i32* %7, i32 4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = load i32, i32* %7
store i32 %20, i32* @send_packet.prev_tos
br label %21
21: ; preds = %19, %5
%22 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%23 = call i32 @sendto(i32 %0, i8* %1, i32 %2, i32 0, %struct.sockaddr* %22, i32 4)
ret i32 %23
}