#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*/ toscheck;
typedef int /*<<< orphan*/ tos;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_TOS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int *);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ stderr;
int set_socket_tos(int sockfd, int tos) {
  int toscheck = 0;
  socklen_t toslen;
  if (tos == 0)
    return 0;
  if (setsockopt(sockfd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) != 0) {
    perror("setting IP_TOS failed");
    return 1;
  }
  toslen = sizeof(toscheck);
  getsockopt(sockfd, IPPROTO_IP, IP_TOS, &toscheck, &toslen);
  if (toscheck != tos) {
    fprintf(stderr, "TOS %d != %d\n", toscheck, tos);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_socket_tos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    116b <set_socket_tos+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    1214 <set_socket_tos+0xd4>
mov    -0x8(%rbp),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11b1 <set_socket_tos+0x71>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1214 <set_socket_tos+0xd4>
movl   $0x4,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%r8
call   1040 <getsockopt@plt>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     120d <set_socket_tos+0xcd>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1214 <set_socket_tos+0xd4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_socket_tos>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,0x10(%rsp)
movl   $0x0,0xc(%rsp)
xor    %ebx,%ebx
test   %esi,%esi
je     11cc <set_socket_tos+0x8c>
mov    %edi,%ebp
mov    0x2e83(%rip),%r14
mov    (%r14),%esi
mov    0x2e71(%rip),%r15
mov    (%r15),%edx
lea    0x10(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    11d9 <set_socket_tos+0x99>
movl   $0x4,0x14(%rsp)
mov    (%r14),%esi
mov    (%r15),%edx
lea    0xc(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %ebp,%edi
call   1040 <getsockopt@plt>
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
cmp    %ecx,%edx
je     11cc <set_socket_tos+0x8c>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
jmp    11c7 <set_socket_tos+0x87>

LLVM IR (-O0):
@IPPROTO_IP = global i32 0
@IP_TOS = global i32 0
@.str = [22 x i8] c"setting IP_TOS failed\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"TOS %d != %d\0A\00"
define i32 @set_socket_tos(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %6
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %3
br label %33
11: ; preds = %2
%12 = load i32, i32* %4
%13 = load i32, i32* @IPPROTO_IP
%14 = load i32, i32* @IP_TOS
%15 = call i64 @setsockopt(i32 %12, i32 %13, i32 %14, i32* %5, i32 4)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %33
19: ; preds = %11
store i32 4, i32* %7
%20 = load i32, i32* %4
%21 = load i32, i32* @IPPROTO_IP
%22 = load i32, i32* @IP_TOS
%23 = call i32 @getsockopt(i32 %20, i32 %21, i32 %22, i32* %6, i32* %7)
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i32, i32* %6
%30 = load i32, i32* %5
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30)
store i32 1, i32* %3
br label %33
32: ; preds = %19
store i32 0, i32* %3
br label %33
33: ; preds = %32, %27, %17, %10
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@IPPROTO_IP = global i32 0
@IP_TOS = global i32 0
@.str = [22 x i8] c"setting IP_TOS failed\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"TOS %d != %d\0A\00"
define i32 @set_socket_tos(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %1, i32* %3
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = bitcast i32* %5 to i8*
%8 = icmp eq i32 %1, 0
br i1 %8, label %26, label %9
9: ; preds = %2
%10 = load i32, i32* @IPPROTO_IP
%11 = load i32, i32* @IP_TOS
%12 = call i64 @setsockopt(i32 %0, i32 %10, i32 %11, i32* %3, i32 4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %26
16: ; preds = %9
store i32 4, i32* %5
%17 = load i32, i32* @IPPROTO_IP
%18 = load i32, i32* @IP_TOS
%19 = call i32 @getsockopt(i32 %0, i32 %17, i32 %18, i32* %4, i32* %5)
%20 = load i32, i32* %4
%21 = load i32, i32* %3
%22 = icmp eq i32 %20, %21
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
br label %26
26: ; preds = %16, %2, %23, %14
%27 = phi i32 [ 1, %14 ], [ 1, %23 ], [ 0, %2 ], [ 0, %16 ]
ret i32 %27
}