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

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ socket_connect(int, char *, int);
int /*<<< orphan*/ socket_join_multicast(int, char *);
int socket_open(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ socket_set_nonblock(int);
int /*<<< orphan*/ socket_set_ttl(int, int);
int output_init_udp(char *ip, int port, int ttl) {
  int sock;
  sock = socket_open(NULL, 0);
  if (sock < 0)
    return sock;
  socket_set_nonblock(sock);
  if (socket_connect(sock, ip, port)) {
    close(sock);
    return -1;
  }
  socket_join_multicast(sock, ip);
  if (ttl)
    socket_set_ttl(sock, ttl);
  return sock;
}


x86 assembly (clang, -O0):
<output_init_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <socket_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1195 <output_init_udp+0x35>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <output_init_udp+0x91>
mov    -0x1c(%rbp),%edi
call   1030 <socket_set_nonblock@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <socket_connect@plt>
cmp    $0x0,%rax
je     11ca <output_init_udp+0x6a>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <output_init_udp+0x91>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <socket_join_multicast@plt>
cmpl   $0x0,-0x18(%rbp)
je     11eb <output_init_udp+0x8b>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <socket_set_ttl@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<output_init_udp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
xor    %edi,%edi
xor    %esi,%esi
call   1060 <socket_open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11bd <output_init_udp+0x5d>
mov    %ebx,%edi
call   1030 <socket_set_nonblock@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1070 <socket_connect@plt>
test   %rax,%rax
je     11a4 <output_init_udp+0x44>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    11bd <output_init_udp+0x5d>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <socket_join_multicast@plt>
test   %r14d,%r14d
je     11bd <output_init_udp+0x5d>
mov    %ebx,%edi
mov    %r14d,%esi
call   1080 <socket_set_ttl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @output_init_udp(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = call i32 @socket_open(i32* null, i32 0)
store i32 %9, i32* %8
%10 = load i32, i32* %8
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i32, i32* %8
store i32 %13, i32* %4
br label %37
14: ; preds = %3
%15 = load i32, i32* %8
%16 = call i32 @socket_set_nonblock(i32 %15)
%17 = load i32, i32* %8
%18 = load i8*, i8** %5
%19 = load i32, i32* %6
%20 = call i64 @socket_connect(i32 %17, i8* %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* %8
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %4
br label %37
25: ; preds = %14
%26 = load i32, i32* %8
%27 = load i8*, i8** %5
%28 = call i32 @socket_join_multicast(i32 %26, i8* %27)
%29 = load i32, i32* %7
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = call i32 @socket_set_ttl(i32 %32, i32 %33)
br label %35
35: ; preds = %31, %25
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %37
37: ; preds = %35, %22, %12
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
define i32 @output_init_udp(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @socket_open(i32* null, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = tail call i32 @socket_set_nonblock(i32 %4)
%8 = tail call i64 @socket_connect(i32 %4, i8* %0, i32 %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @close(i32 %4)
br label %17
12: ; preds = %6
%13 = tail call i32 @socket_join_multicast(i32 %4, i8* %0)
%14 = icmp eq i32 %2, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @socket_set_ttl(i32 %4, i32 %2)
br label %17
17: ; preds = %12, %15, %3, %10
%18 = phi i32 [ -1, %10 ], [ %4, %3 ], [ %4, %15 ], [ %4, %12 ]
ret i32 %18
}