#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 sockaddr_storage {
  int dummy;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_scope_id;
  int /*<<< orphan*/ sin6_addr;
  scalar_t__ sin6_port;
  void *sin6_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ local_address;
typedef int /*<<< orphan*/ discovery_address;

/* Variables and functions */
void *AF_INET6;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_MULTICAST_LOOP;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
scalar_t__ get_peer_offer(int, struct sockaddr *, int,
                          struct sockaddr_storage *, int /*<<< orphan*/ *);
scalar_t__ htons(int);
int /*<<< orphan*/ if_nametoindex(char const *);
int /*<<< orphan*/ in6addr_any;
int /*<<< orphan*/ inet_pton(void *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
char *strerror(int /*<<< orphan*/);
int discover_ipv6(int s, const char *iface,
                  struct sockaddr_storage *modem_address,
                  socklen_t *modem_address_length) {
  int ret = 0;
  struct sockaddr_in6 local_address = {0};
  local_address.sin6_family = AF_INET6;
  local_address.sin6_port = 0;
  local_address.sin6_addr = in6addr_any;
  if (bind(s, (struct sockaddr *)&local_address, sizeof(local_address)) != 0) {
    printf("Failed to bind socket: %s\n", strerror(errno));
  } else {
    int on = 1;
    if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) !=
        0) {
      printf("Failed to set multicast loopback option: %s\n", strerror(errno));
    } else {
      struct sockaddr_in6 discovery_address = {0};
      discovery_address.sin6_family = AF_INET6;
      discovery_address.sin6_port = htons(854);
      inet_pton(AF_INET6, "FF02::1:7", &discovery_address.sin6_addr);
      if (iface)
        discovery_address.sin6_scope_id = if_nametoindex(iface);
      if (get_peer_offer(s, (struct sockaddr *)&discovery_address,
                         sizeof(discovery_address), modem_address,
                         modem_address_length)) {
        ((struct sockaddr_in6 *)modem_address)->sin6_scope_id =
            discovery_address.sin6_scope_id;
        ret = 1;
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<discover_ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x20,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     121d <discover_ipv6+0x8d>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
jmp    1312 <discover_ipv6+0x182>
movl   $0x1,-0x4c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1274 <discover_ipv6+0xe4>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb1(%rip),%rdi
call   1050 <printf@plt>
jmp    130d <discover_ipv6+0x17d>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x60(%rbp)
mov    $0x356,%edi
call   1040 <htons@plt>
mov    %rax,-0x68(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%rdi
lea    -0x70(%rbp),%rdx
add    $0x4,%rdx
lea    0xd8f(%rip),%rsi
call   1080 <inet_pton@plt>
cmpq   $0x0,-0x10(%rbp)
je     12d5 <discover_ipv6+0x145>
mov    -0x10(%rbp),%rdi
call   1070 <if_nametoindex@plt>
mov    %eax,-0x70(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0x20,%edx
call   10a0 <get_peer_offer@plt>
cmp    $0x0,%rax
je     1308 <discover_ipv6+0x178>
mov    -0x70(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x24(%rbp)
jmp    130d <discover_ipv6+0x17d>
jmp    1312 <discover_ipv6+0x182>
mov    -0x24(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<discover_ipv6>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
mov    0x2e2d(%rip),%rbx
mov    (%rbx),%rax
mov    %rax,0x40(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
lea    0x30(%rsp),%rsi
mov    $0x20,%edx
call   1080 <bind@plt>
test   %rax,%rax
je     11eb <discover_ipv6+0x6b>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe17(%rip),%rdi
jmp    1231 <discover_ipv6+0xb1>
movl   $0x1,0xc(%rsp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1248 <discover_ipv6+0xc8>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdea(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    (%rbx),%rax
mov    %rax,0x20(%rsp)
mov    $0x356,%edi
call   1040 <htons@plt>
mov    %rax,0x18(%rsp)
mov    (%rbx),%rdi
lea    0x14(%rsp),%rdx
lea    0xdcd(%rip),%rsi
call   1070 <inet_pton@plt>
test   %r12,%r12
je     1291 <discover_ipv6+0x111>
mov    %r12,%rdi
call   1060 <if_nametoindex@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x20,%edx
mov    %r15,%rcx
mov    %r14,%r8
call   1090 <get_peer_offer@plt>
test   %rax,%rax
je     1239 <discover_ipv6+0xb9>
mov    0x10(%rsp),%eax
mov    %eax,(%r15)
mov    $0x1,%eax
jmp    123b <discover_ipv6+0xbb>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in6 = type { i32, i32, i64, i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i8* null
@in6addr_any = global i32 0
@.str = [27 x i8] c"Failed to bind socket: %s\0A\00"
@errno = global i32 0
@IPPROTO_IPV6 = global i32 0
@IPV6_MULTICAST_LOOP = global i32 0
@.str.1 = [45 x i8] c"Failed to set multicast loopback option: %s\0A\00"
@.str.2 = [10 x i8] c"FF02::1:7\00"
define i32 @discover_ipv6(i32 %0, i8* %1, %struct.sockaddr_storage* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.sockaddr_storage*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in6
%11 = alloca i32
%12 = alloca %struct.sockaddr_in6
store i32 %0, i32* %5
store i8* %1, i8** %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
store i32* %3, i32** %8
store i32 0, i32* %9
%13 = bitcast %struct.sockaddr_in6* %10 to i8*
%14 = load i8*, i8** @AF_INET6
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 3
store i8* %14, i8** %15
%16 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 2
store i64 0, i64* %16
%17 = load i32, i32* @in6addr_any
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* %5
%20 = bitcast %struct.sockaddr_in6* %10 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 32)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %4
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %67
27: ; preds = %4
store i32 1, i32* %11
%28 = load i32, i32* %5
%29 = load i32, i32* @IPPROTO_IPV6
%30 = load i32, i32* @IPV6_MULTICAST_LOOP
%31 = call i64 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %11, i32 4)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %66
37: ; preds = %27
%38 = bitcast %struct.sockaddr_in6* %12 to i8*
%39 = load i8*, i8** @AF_INET6
%40 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i32 0, i32 3
store i8* %39, i8** %40
%41 = call i64 @htons(i32 854)
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i32 0, i32 2
store i64 %41, i64* %42
%43 = load i8*, i8** @AF_INET6
%44 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i32 0, i32 1
%45 = call i32 @inet_pton(i8* %43, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32* %44)
%46 = load i8*, i8** %6
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %52
48: ; preds = %37
%49 = load i8*, i8** %6
%50 = call i32 @if_nametoindex(i8* %49)
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i32 0, i32 0
store i32 %50, i32* %51
br label %52
52: ; preds = %48, %37
%53 = load i32, i32* %5
%54 = bitcast %struct.sockaddr_in6* %12 to %struct.sockaddr*
%55 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%56 = load i32*, i32** %8
%57 = call i64 @get_peer_offer(i32 %53, %struct.sockaddr* %54, i32 32, %struct.sockaddr_storage* %55, i32* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %52
%60 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %12, i32 0, i32 0
%61 = load i32, i32* %60
%62 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%63 = bitcast %struct.sockaddr_storage* %62 to %struct.sockaddr_in6*
%64 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %63, i32 0, i32 0
store i32 %61, i32* %64
store i32 1, i32* %9
br label %65
65: ; preds = %59, %52
br label %66
66: ; preds = %65, %33
br label %67
67: ; preds = %66, %23
%68 = load i32, i32* %9
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in6 = type { i32, i32, i64, i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i8* null
@in6addr_any = global i32 0
@.str = [27 x i8] c"Failed to bind socket: %s\0A\00"
@errno = global i32 0
@IPPROTO_IPV6 = global i32 0
@IPV6_MULTICAST_LOOP = global i32 0
@.str.1 = [45 x i8] c"Failed to set multicast loopback option: %s\0A\00"
@.str.2 = [10 x i8] c"FF02::1:7\00"
define i32 @discover_ipv6(i32 %0, i8* %1, %struct.sockaddr_storage* %2, i32* %3) {
%5 = alloca %struct.sockaddr_in6
%6 = alloca i32
%7 = alloca %struct.sockaddr_in6
%8 = bitcast %struct.sockaddr_in6* %5 to i8*
%9 = load i8*, i8** @AF_INET6
%10 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 3
store i8* %9, i8** %10
%11 = load i32, i32* @in6addr_any
%12 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 1
store i32 %11, i32* %12
%13 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%14 = call i64 @bind(i32 %0, %struct.sockaddr* %13, i32 32)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %4
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %55
20: ; preds = %4
%21 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%22 = load i32, i32* @IPPROTO_IPV6
%23 = load i32, i32* @IPV6_MULTICAST_LOOP
%24 = call i64 @setsockopt(i32 %0, i32 %22, i32 %23, i32* %6, i32 4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %53
30: ; preds = %20
%31 = bitcast %struct.sockaddr_in6* %7 to i8*
%32 = load i8*, i8** @AF_INET6
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 3
store i8* %32, i8** %33
%34 = call i64 @htons(i32 854)
%35 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 2
store i64 %34, i64* %35
%36 = load i8*, i8** @AF_INET6
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 1
%38 = call i32 @inet_pton(i8* %36, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32* %37)
%39 = icmp eq i8* %1, null
br i1 %39, label %43, label %40
40: ; preds = %30
%41 = call i32 @if_nametoindex(i8* %1)
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 0
store i32 %41, i32* %42
br label %43
43: ; preds = %40, %30
%44 = bitcast %struct.sockaddr_in6* %7 to %struct.sockaddr*
%45 = call i64 @get_peer_offer(i32 %0, %struct.sockaddr* %44, i32 32, %struct.sockaddr_storage* %2, i32* %3)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %43
%48 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i64 0, i32 0
%49 = load i32, i32* %48
%50 = getelementptr %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
store i32 %49, i32* %50
br label %51
51: ; preds = %47, %43
%52 = phi i32 [ 1, %47 ], [ 0, %43 ]
br label %53
53: ; preds = %51, %26
%54 = phi i32 [ 0, %26 ], [ %52, %51 ]
br label %55
55: ; preds = %53, %16
%56 = phi i32 [ 0, %16 ], [ %54, %53 ]
ret i32 %56
}