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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_udp_connection(const char *ipaddress, int port) {
  struct sockaddr_in addr_ctl;
  int s_ctl = -1;
  int result = -1;
  if (!ipaddress)
    return -1;
  s_ctl = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (s_ctl <= 0) {
    perror("udp socket error");
    return -1;
  }
  addr_ctl.sin_family = AF_INET;
  addr_ctl.sin_addr.s_addr = inet_addr(ipaddress);
  addr_ctl.sin_port = htons(port);
  result = bind(s_ctl, (struct sockaddr *)&addr_ctl, sizeof(addr_ctl));
  if (result < 0) {
    perror("udp bind error");
    return -1;
  }
  return s_ctl;
}


x86 assembly (clang, -O0):
<create_udp_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <create_udp_connection+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <create_udp_connection+0xd8>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11c9 <create_udp_connection+0x79>
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <create_udp_connection+0xd8>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1222 <create_udp_connection+0xd2>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <create_udp_connection+0xd8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_udp_connection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     11c5 <create_udp_connection+0x75>
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
jle    11d0 <create_udp_connection+0x80>
mov    %eax,%ebx
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
call   1040 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %eax,%eax
js     11d9 <create_udp_connection+0x89>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe29(%rip),%rdi
jmp    11e0 <create_udp_connection+0x90>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11c5 <create_udp_connection+0x75>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [17 x i8] c"udp socket error\00"
@AF_INET = global i32 0
@.str.1 = [15 x i8] c"udp bind error\00"
define i32 @create_udp_connection(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
store i32 -1, i32* %7
store i32 -1, i32* %8
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 -1, i32* %3
br label %40
12: ; preds = %2
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_UDP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
21: ; preds = %12
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i8*, i8** %4
%25 = call i32 @inet_addr(i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %5
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %7
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %21
%37 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
38: ; preds = %21
%39 = load i32, i32* %7
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %36, %19, %11
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [17 x i8] c"udp socket error\00"
@AF_INET = global i32 0
@.str.1 = [15 x i8] c"udp bind error\00"
define i32 @create_udp_connection(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %26, label %6
6: ; preds = %2
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = tail call i32 @socket(i32 %7, i32 %8, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %26
14: ; preds = %6
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @inet_addr(i8* %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @bind(i32 %10, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %14, %2, %24, %12
%27 = phi i32 [ -1, %12 ], [ -1, %24 ], [ -1, %2 ], [ %10, %14 ]
ret i32 %27
}