#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 TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ remote;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ IPPROTO_UDP;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_DGRAM;
int connect(int, struct sockaddr *, int);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_open(const char *ip, int port) {
  struct sockaddr_in remote;
  int fd;
  if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    return 0;
  memset((char *)&remote, 0, sizeof(remote));
  remote.sin_family = AF_INET;
  remote.sin_port = htons(port);
  remote.sin_addr.s_addr = inet_addr(ip);
  int flags = fcntl(fd, F_GETFL);
  if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
    return 0;
  if (connect(fd, (struct sockaddr *)&remote, sizeof(remote)) == -1)
    return 0;
  return fd;
}


x86 assembly (clang, -O0):
<udp_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <udp_open+0x47>
movl   $0x0,-0x4(%rbp)
jmp    1250 <udp_open+0xf0>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2dda(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    1224 <udp_open+0xc4>
movl   $0x0,-0x4(%rbp)
jmp    1250 <udp_open+0xf0>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    124a <udp_open+0xea>
movl   $0x0,-0x4(%rbp)
jmp    1250 <udp_open+0xf0>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e6f(%rip),%r12
mov    (%r12),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     121d <udp_open+0xbd>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1060 <inet_addr@plt>
mov    %eax,(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2de0(%rip),%rcx
mov    (%rcx),%esi
mov    0x2def(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     121f <udp_open+0xbf>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
cmove  %ecx,%ebx
mov    %ebx,%ebp
jmp    121f <udp_open+0xbf>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @udp_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %46
15: ; preds = %2
%16 = bitcast %struct.sockaddr_in* %6 to i8*
%17 = call i32 @memset(i8* %16, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* %5
%21 = call i32 @htons(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i8*, i8** %4
%24 = call i32 @inet_addr(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %7
%28 = load i32, i32* @F_GETFL
%29 = call i32 (i32, i32, ...) @fcntl(i32 %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %7
%31 = load i32, i32* @F_SETFL
%32 = load i32, i32* %8
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %32, %33
%35 = call i32 (i32, i32, ...) @fcntl(i32 %30, i32 %31, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %15
store i32 0, i32* %3
br label %46
38: ; preds = %15
%39 = load i32, i32* %7
%40 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%41 = call i32 @connect(i32 %39, %struct.sockaddr* %40, i32 12)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 0, i32* %3
br label %46
44: ; preds = %38
%45 = load i32, i32* %7
store i32 %45, i32* %3
br label %46
46: ; preds = %44, %43, %37, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @udp_open(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @IPPROTO_UDP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %30, label %10
10: ; preds = %2
%11 = call i32 @memset(i8* %4, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @inet_addr(i8* %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @F_GETFL
%19 = call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %18)
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %21, %19
%23 = call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %20, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %10
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @connect(i32 %8, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
%29 = select i1 %28, i32 0, i32 %8
br label %30
30: ; preds = %10, %25, %2
%31 = phi i32 [ 0, %2 ], [ 0, %10 ], [ %29, %25 ]
ret i32 %31
}