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

/* Variables and functions */
int AF_INET;
int AF_INET6;
int /*<<< orphan*/ IPV6_UNICAST_HOPS;
int /*<<< orphan*/ IP_TTL;
int /*<<< orphan*/ SOL_IP;
int /*<<< orphan*/ SOL_IPV6;
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int set_ttl(int af, int sk, int ttl) {
  if (af == AF_INET) {
    if (setsockopt(sk, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
      do {
        perror("setsockopt IP_TTL");
        return -1;
      } while (0);
  } else if (af == AF_INET6) {
    if (setsockopt(sk, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
      do {
        perror("setsockopt IPV6_UNICAST_HOPS");
        return -1;
      } while (0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_ttl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e9d(%rip),%rcx
cmp    (%rcx),%eax
jne    1193 <set_ttl+0x73>
mov    -0xc(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    118e <set_ttl+0x6e>
jmp    1176 <set_ttl+0x56>
lea    0xe83(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <set_ttl+0xe1>
jmp    11fa <set_ttl+0xda>
mov    -0x8(%rbp),%eax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%eax
jne    11f5 <set_ttl+0xd5>
mov    -0xc(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11f0 <set_ttl+0xd0>
jmp    11d8 <set_ttl+0xb8>
lea    0xe33(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <set_ttl+0xe1>
jmp    11f5 <set_ttl+0xd5>
jmp    11fa <set_ttl+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_ttl>:
push   %rbx
sub    $0x10,%rsp
mov    %edx,0xc(%rsp)
mov    0x2ea8(%rip),%rax
cmp    %edi,(%rax)
jne    116a <set_ttl+0x4a>
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%edx
lea    0xc(%rsp),%rcx
mov    %esi,%edi
mov    %eax,%esi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    11a2 <set_ttl+0x82>
lea    0xe98(%rip),%rdi
jmp    11b1 <set_ttl+0x91>
mov    0x2e5f(%rip),%rax
xor    %ebx,%ebx
cmp    %edi,(%rax)
jne    11a2 <set_ttl+0x82>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%edx
lea    0xc(%rsp),%rcx
mov    %esi,%edi
mov    %eax,%esi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11aa <set_ttl+0x8a>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe61(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11a2 <set_ttl+0x82>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOL_IP = global i32 0
@IP_TTL = global i32 0
@.str = [18 x i8] c"setsockopt IP_TTL\00"
@AF_INET6 = global i32 0
@SOL_IPV6 = global i32 0
@IPV6_UNICAST_HOPS = global i32 0
@.str.1 = [29 x i8] c"setsockopt IPV6_UNICAST_HOPS\00"
define i32 @set_ttl(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32, i32* %5
%9 = load i32, i32* @AF_INET
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %22
11: ; preds = %3
%12 = load i32, i32* %6
%13 = load i32, i32* @SOL_IP
%14 = load i32, i32* @IP_TTL
%15 = call i64 @setsockopt(i32 %12, i32 %13, i32 %14, i32* %7, i32 4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
br label %18
18: ; preds = %17
%19 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
20: ; No predecessors!
br label %21
21: ; preds = %20, %11
br label %38
22: ; preds = %3
%23 = load i32, i32* %5
%24 = load i32, i32* @AF_INET6
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %37
26: ; preds = %22
%27 = load i32, i32* %6
%28 = load i32, i32* @SOL_IPV6
%29 = load i32, i32* @IPV6_UNICAST_HOPS
%30 = call i64 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %7, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
35: ; No predecessors!
br label %36
36: ; preds = %35, %26
br label %37
37: ; preds = %36, %22
br label %38
38: ; preds = %37, %21
store i32 0, i32* %4
br label %39
39: ; preds = %38, %33, %18
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOL_IP = global i32 0
@IP_TTL = global i32 0
@.str = [18 x i8] c"setsockopt IP_TTL\00"
@AF_INET6 = global i32 0
@SOL_IPV6 = global i32 0
@IPV6_UNICAST_HOPS = global i32 0
@.str.1 = [29 x i8] c"setsockopt IPV6_UNICAST_HOPS\00"
define i32 @set_ttl(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
store i32 %2, i32* %4
%5 = load i32, i32* @AF_INET
%6 = icmp eq i32 %5, %0
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = load i32, i32* @SOL_IP
%9 = load i32, i32* @IP_TTL
%10 = call i64 @setsockopt(i32 %1, i32 %8, i32 %9, i32* %4, i32 4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %20, label %23
12: ; preds = %3
%13 = load i32, i32* @AF_INET6
%14 = icmp eq i32 %13, %0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i32, i32* @SOL_IPV6
%17 = load i32, i32* @IPV6_UNICAST_HOPS
%18 = call i64 @setsockopt(i32 %1, i32 %16, i32 %17, i32* %4, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15, %7
%21 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %7 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %15 ]
%22 = call i32 @perror(i8* %21)
br label %23
23: ; preds = %20, %7, %15, %12
%24 = phi i32 [ 0, %12 ], [ 0, %15 ], [ 0, %7 ], [ -1, %20 ]
ret i32 %24
}