#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*/ dscp;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_TCLASS;
int /*<<< orphan*/ IP_TOS;
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int settos(int p, int type, int tos, int ecn) {
  int dscp = tos | ecn;
  int rc = 0;
  switch (type) {
  default:
  IPPROTO_IP:
    rc = setsockopt(p, IPPROTO_IP, IP_TOS, (char *)&dscp, sizeof(dscp));
    break;
  IPPROTO_IPV6:
    rc = setsockopt(p, IPPROTO_IPV6, IPV6_TCLASS, (int *)&dscp, sizeof(dscp));
    break;
  }
  return (rc);
}


x86 assembly (clang, -O0):
<settos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
jmp    113b <settos+0x2b>
mov    -0x4(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<settos>:
push   %rax
or     %ecx,%edx
mov    %edx,0x4(%rsp)
mov    0x2eca(%rip),%rax
mov    (%rax),%esi
mov    0x2eb9(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
xor    %eax,%eax
call   1030 <setsockopt@plt>
pop    %rcx
ret

LLVM IR (-O0):
@IPPROTO_IP = global i32 0
@IP_TOS = global i32 0
@IPPROTO_IPV6 = global i32 0
@IPV6_TCLASS = global i32 0
define i32 @settos(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %7
%12 = load i32, i32* %8
%13 = or i32 %11, %12
store i32 %13, i32* %9
store i32 0, i32* %10
%14 = load i32, i32* %6
switch i32 %14, label %15 [
]
15: ; preds = %4
br label %16
16: ; preds = %15
%17 = load i32, i32* %5
%18 = load i32, i32* @IPPROTO_IP
%19 = load i32, i32* @IP_TOS
%20 = bitcast i32* %9 to i8*
%21 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %17, i32 %18, i32 %19, i8* %20, i64 4)
store i32 %21, i32* %10
br label %27
22: ; No predecessors!
%23 = load i32, i32* %5
%24 = load i32, i32* @IPPROTO_IPV6
%25 = load i32, i32* @IPV6_TCLASS
%26 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %23, i32 %24, i32 %25, i32* %9, i64 4)
store i32 %26, i32* %10
br label %27
27: ; preds = %22, %16
%28 = load i32, i32* %10
ret i32 %28
}

LLVM IR (-O2):
@IPPROTO_IP = global i32 0
@IP_TOS = global i32 0
@IPPROTO_IPV6 = global i32 0
@IPV6_TCLASS = global i32 0
define i32 @settos(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = or i32 %3, %2
store i32 %7, i32* %5
%8 = load i32, i32* @IPPROTO_IP
%9 = load i32, i32* @IP_TOS
%10 = call i32 (i32, i32, i32, ...) @setsockopt(i32 %0, i32 %8, i32 %9, i32* %5, i64 4)
ret i32 %10
}