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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BINDTODEVICE;
int /*<<< orphan*/ close(int);
int discover_ipv4(int, struct sockaddr_storage *, int /*<<< orphan*/ *);
int discover_ipv6(int, char const *, struct sockaddr_storage *,
                  int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
scalar_t__ geteuid();
int /*<<< orphan*/ printf(char *, char const *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char const *,
                      scalar_t__);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int discover(int use_ipv6, const char *iface,
             struct sockaddr_storage *modem_address,
             socklen_t *modem_address_length) {
  int ret = 0;
  int s = socket(use_ipv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
  if (s == -1) {
    printf("Failed to create socket: %s\n", strerror(errno));
  } else {
    ret = 1;
    if (iface) {
      if (geteuid() != 0)
        printf("Not binding multicast discovery socket to interface %s as not "
               "root\n",
               iface);
      else if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface,
                          strlen(iface) + 1) != 0) {
        printf("Failed to bind socket to interface %s: %s\n", iface,
               strerror(errno));
        ret = 0;
      }
    }
    if (ret) {
      if (use_ipv6)
        ret = discover_ipv6(s, iface, modem_address, modem_address_length);
      else
        ret = discover_ipv4(s, modem_address, modem_address_length);
    }
    close(s);
  }
  return ret;
}


x86 assembly (clang, -O0):
<discover>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11c9 <discover+0x39>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11d5 <discover+0x45>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1219 <discover+0x89>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1331 <discover+0x1a1>
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12e1 <discover+0x151>
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%rax
je     1253 <discover+0xc3>
mov    -0x10(%rbp),%rsi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12dc <discover+0x14c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    %rax,%r8
add    $0x1,%r8
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12d7 <discover+0x147>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x24(%rbp)
jmp    12dc <discover+0x14c>
jmp    12e1 <discover+0x151>
cmpl   $0x0,-0x24(%rbp)
je     1329 <discover+0x199>
cmpl   $0x0,-0x4(%rbp)
je     1311 <discover+0x181>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1090 <discover_ipv6@plt>
mov    %eax,-0x24(%rbp)
jmp    1324 <discover+0x194>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <discover_ipv4@plt>
mov    %eax,-0x24(%rbp)
jmp    1329 <discover+0x199>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
mov    -0x24(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<discover>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r13d
test   %edi,%edi
jne    11b4 <discover+0x24>
mov    0x2e2e(%rip),%rax
jmp    11bb <discover+0x2b>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     120f <discover+0x7f>
mov    %eax,%ebx
test   %r14,%r14
je     11f8 <discover+0x68>
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %rax,%rax
je     1255 <discover+0xc5>
lea    0xe2f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
test   %r13d,%r13d
je     1230 <discover+0xa0>
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1090 <discover_ipv6@plt>
jmp    123b <discover+0xab>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xddc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1244 <discover+0xb4>
mov    %r12,%rsi
mov    %r15,%rdx
call   1080 <discover_ipv4@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%r8
mov    %ebx,%edi
mov    0x4(%rsp),%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11f8 <discover+0x68>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdbc(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123d <discover+0xad>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [29 x i8] c"Failed to create socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [68 x i8] c"Not binding multicast discovery socket to interface %s as not root\0A\00"
@SOL_SOCKET = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str.2 = [43 x i8] c"Failed to bind socket to interface %s: %s\0A\00"
define i32 @discover(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 i32
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
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = load i32, i32* @AF_INET6
br label %17
15: ; preds = %4
%16 = load i32, i32* @AF_INET
br label %17
17: ; preds = %15, %13
%18 = phi i32 [ %14, %13 ], [ %16, %15 ]
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %74
27: ; preds = %17
store i32 1, i32* %9
%28 = load i8*, i8** %6
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %53
30: ; preds = %27
%31 = call i64 (...) @geteuid()
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i8*, i8** %6
%35 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %52
36: ; preds = %30
%37 = load i32, i32* %10
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_BINDTODEVICE
%40 = load i8*, i8** %6
%41 = load i8*, i8** %6
%42 = call i64 @strlen(i8* %41)
%43 = add nsw i64 %42, 1
%44 = call i64 @setsockopt(i32 %37, i32 %38, i32 %39, i8* %40, i64 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %36
%47 = load i8*, i8** %6
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %47, i8* %49)
store i32 0, i32* %9
br label %51
51: ; preds = %46, %36
br label %52
52: ; preds = %51, %33
br label %53
53: ; preds = %52, %27
%54 = load i32, i32* %9
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %71
56: ; preds = %53
%57 = load i32, i32* %5
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %56
%60 = load i32, i32* %10
%61 = load i8*, i8** %6
%62 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%63 = load i32*, i32** %8
%64 = call i32 @discover_ipv6(i32 %60, i8* %61, %struct.sockaddr_storage* %62, i32* %63)
store i32 %64, i32* %9
br label %70
65: ; preds = %56
%66 = load i32, i32* %10
%67 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%68 = load i32*, i32** %8
%69 = call i32 @discover_ipv4(i32 %66, %struct.sockaddr_storage* %67, i32* %68)
store i32 %69, i32* %9
br label %70
70: ; preds = %65, %59
br label %71
71: ; preds = %70, %53
%72 = load i32, i32* %10
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %71, %23
%75 = load i32, i32* %9
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [29 x i8] c"Failed to create socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [68 x i8] c"Not binding multicast discovery socket to interface %s as not root\0A\00"
@SOL_SOCKET = global i32 0
@SO_BINDTODEVICE = global i32 0
@.str.2 = [43 x i8] c"Failed to bind socket to interface %s: %s\0A\00"
define i32 @discover(i32 %0, i8* %1, %struct.sockaddr_storage* %2, i32* %3) {
%5 = icmp eq i32 %0, 0
%6 = load i32, i32* @AF_INET6
%7 = load i32, i32* @AF_INET
%8 = select i1 %5, i32 %7, i32 %6
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %42
16: ; preds = %4
%17 = icmp eq i8* %1, null
br i1 %17, label %34, label %18
18: ; preds = %16
%19 = tail call i64 (...) @geteuid()
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %34
23: ; preds = %18
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_BINDTODEVICE
%26 = tail call i64 @strlen(i8* %1)
%27 = add nsw i64 %26, 1
%28 = tail call i64 @setsockopt(i32 %10, i32 %24, i32 %25, i8* %1, i64 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %23
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
%33 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %32)
br label %39
34: ; preds = %16, %23, %21
br i1 %5, label %37, label %35
35: ; preds = %34
%36 = tail call i32 @discover_ipv6(i32 %10, i8* %1, %struct.sockaddr_storage* %2, i32* %3)
br label %39
37: ; preds = %34
%38 = tail call i32 @discover_ipv4(i32 %10, %struct.sockaddr_storage* %2, i32* %3)
br label %39
39: ; preds = %30, %35, %37
%40 = phi i32 [ %36, %35 ], [ %38, %37 ], [ 0, %30 ]
%41 = tail call i32 @close(i32 %10)
br label %42
42: ; preds = %39, %12
%43 = phi i32 [ 0, %12 ], [ %40, %39 ]
ret i32 %43
}