#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 */
struct iphdr {
  int tos;
  int /*<<< orphan*/ check;
};
typedef int __u8;
typedef int __u32;

/* Variables and functions */
int /*<<< orphan*/ htons(int);
int ntohs(int /*<<< orphan*/);
void ipv4_change_dsfield(struct iphdr *iph, __u8 mask, __u8 value) {
  __u32 check = ntohs(iph->check);
  __u8 dsfield;
  dsfield = (iph->tos & mask) | value;
  check += iph->tos;
  if ((check + 1) >> 16)
    check = (check + 1) & 0xffff;
  check -= dsfield;
  check += check >> 16;
  iph->check = htons(check);
  iph->tos = dsfield;
}


x86 assembly (clang, -O0):
<ipv4_change_dsfield>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
and    -0xc(%rbp),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
sar    $0x10,%eax
cmp    $0x0,%eax
je     117c <ipv4_change_dsfield+0x5c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
and    $0xffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x10,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipv4_change_dsfield>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x4(%rdi),%edi
call   1040 <ntohs@plt>
mov    (%r14),%ecx
and    %ecx,%ebx
or     %ebp,%ebx
lea    (%rcx,%rax,1),%edx
add    %ecx,%eax
add    $0x1,%eax
cmp    $0x10000,%eax
movzwl %ax,%eax
cmovb  %edx,%eax
sub    %ebx,%eax
mov    %eax,%edi
sar    $0x10,%edi
add    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%r14)
mov    %ebx,(%r14)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32, i32 }
define void @ipv4_change_dsfield(%struct.iphdr* %0, i32 %1, i32 %2) {
%4 = alloca %struct.iphdr*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.iphdr* %0, %struct.iphdr** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load %struct.iphdr*, %struct.iphdr** %4
%10 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %9, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @ntohs(i32 %11)
store i32 %12, i32* %7
%13 = load %struct.iphdr*, %struct.iphdr** %4
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* %5
%17 = and i32 %15, %16
%18 = load i32, i32* %6
%19 = or i32 %17, %18
store i32 %19, i32* %8
%20 = load %struct.iphdr*, %struct.iphdr** %4
%21 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* %7
%24 = add nsw i32 %23, %22
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
%27 = ashr i32 %26, 16
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %3
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
%32 = and i32 %31, 65535
store i32 %32, i32* %7
br label %33
33: ; preds = %29, %3
%34 = load i32, i32* %8
%35 = load i32, i32* %7
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = ashr i32 %37, 16
%39 = load i32, i32* %7
%40 = add nsw i32 %39, %38
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = call i32 @htons(i32 %41)
%43 = load %struct.iphdr*, %struct.iphdr** %4
%44 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %43, i32 0, i32 1
store i32 %42, i32* %44
%45 = load i32, i32* %8
%46 = load %struct.iphdr*, %struct.iphdr** %4
%47 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %46, i32 0, i32 0
store i32 %45, i32* %47
ret void
}

LLVM IR (-O2):
%struct.iphdr = type { i32, i32 }
define void @ipv4_change_dsfield(%struct.iphdr* nocapture %0, i32 %1, i32 %2) {
%4 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 1
%5 = load i32, i32* %4
%6 = tail call i32 @ntohs(i32 %5)
%7 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = and i32 %8, %1
%10 = or i32 %9, %2
%11 = add nsw i32 %8, %6
%12 = add nsw i32 %11, 1
%13 = icmp ult i32 %12, 65536
%14 = and i32 %12, 65535
%15 = select i1 %13, i32 %11, i32 %14
%16 = sub nsw i32 %15, %10
%17 = ashr i32 %16, 16
%18 = add nsw i32 %17, %16
%19 = tail call i32 @htons(i32 %18)
store i32 %19, i32* %4
store i32 %10, i32* %7
ret void
}