#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_storage {
  int dummy;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  scalar_t__ sin_port;
  void *sin_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_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_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*/ inet_pton(void *, char *, TYPE_1__ *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
char *strerror(int /*<<< orphan*/);
int discover_ipv4(int s, struct sockaddr_storage *modem_address,
                  socklen_t *modem_address_length) {
  int ret = 0;
  struct sockaddr_in local_address = {0};
  local_address.sin_family = AF_INET;
  local_address.sin_port = 0;
  local_address.sin_addr.s_addr = INADDR_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_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) != 0) {
      printf("Failed to set multicast loopback option: %s\n", strerror(errno));
    } else {
      struct sockaddr_in discovery_address = {0};
      discovery_address.sin_family = AF_INET;
      discovery_address.sin_port = htons(854);
      inet_pton(AF_INET, "224.0.0.117", &discovery_address.sin_addr);
      if (get_peer_offer(s, (struct sockaddr *)&discovery_address,
                         sizeof(discovery_address), modem_address,
                         modem_address_length))
        ret = 1;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<discover_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     1209 <discover_ipv4+0x89>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe01(%rip),%rdi
call   1050 <printf@plt>
jmp    12da <discover_ipv4+0x15a>
movl   $0x1,-0x44(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1260 <discover_ipv4+0xe0>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdc5(%rip),%rdi
call   1050 <printf@plt>
jmp    12d5 <discover_ipv4+0x155>
lea    -0x68(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    $0x356,%edi
call   1040 <htons@plt>
mov    %rax,-0x60(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%rdi
lea    -0x68(%rbp),%rdx
lea    0xda7(%rip),%rsi
call   1070 <inet_pton@plt>
mov    -0x4(%rbp),%edi
lea    -0x68(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x20,%edx
call   1090 <get_peer_offer@plt>
cmp    $0x0,%rax
je     12d0 <discover_ipv4+0x150>
movl   $0x1,-0x1c(%rbp)
jmp    12d5 <discover_ipv4+0x155>
jmp    12da <discover_ipv4+0x15a>
mov    -0x1c(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<discover_ipv4>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
mov    0x2e48(%rip),%rbx
mov    (%rbx),%rax
mov    %rax,0x40(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x30(%rsp),%rsi
mov    $0x20,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     11d8 <discover_ipv4+0x68>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2a(%rip),%rdi
jmp    121e <discover_ipv4+0xae>
movl   $0x1,0xc(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1235 <discover_ipv4+0xc5>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdfd(%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    0xde5(%rip),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdx
call   1060 <inet_pton@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x20,%edx
mov    %r12,%rcx
mov    %r14,%r8
call   1080 <get_peer_offer@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
jmp    1228 <discover_ipv4+0xb8>

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

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i8*, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@INADDR_ANY = global i32 0
@.str = [27 x i8] c"Failed to bind socket: %s\0A\00"
@errno = global i32 0
@IPPROTO_IP = global i32 0
@IP_MULTICAST_LOOP = global i32 0
@.str.1 = [45 x i8] c"Failed to set multicast loopback option: %s\0A\00"
@.str.2 = [12 x i8] c"224.0.0.117\00"
define i32 @discover_ipv4(i32 %0, %struct.sockaddr_storage* %1, i32* %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = load i8*, i8** @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i8* %8, i8** %9
%10 = load i32, i32* @INADDR_ANY
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%13 = call i64 @bind(i32 %0, %struct.sockaddr* %12, i32 32)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %44
19: ; preds = %3
%20 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%21 = load i32, i32* @IPPROTO_IP
%22 = load i32, i32* @IP_MULTICAST_LOOP
%23 = call i64 @setsockopt(i32 %0, i32 %21, i32 %22, i32* %5, i32 4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %42
29: ; preds = %19
%30 = bitcast %struct.sockaddr_in* %6 to i8*
%31 = load i8*, i8** @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i8* %31, i8** %32
%33 = call i64 @htons(i32 854)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i64 %33, i64* %34
%35 = load i8*, i8** @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%37 = call i32 @inet_pton(i8* %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %struct.TYPE_2__* %36)
%38 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%39 = call i64 @get_peer_offer(i32 %0, %struct.sockaddr* %38, i32 32, %struct.sockaddr_storage* %1, i32* %2)
%40 = icmp ne i64 %39, 0
%41 = zext i1 %40 to i32
br label %42
42: ; preds = %29, %25
%43 = phi i32 [ 0, %25 ], [ %41, %29 ]
br label %44
44: ; preds = %42, %15
%45 = phi i32 [ 0, %15 ], [ %43, %42 ]
ret i32 %45
}