#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 addrinfo {
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int usock(const char *target, const char *target_port,
          struct addrinfo **targetinfo) {
  struct addrinfo hints;
  int retfd;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  if (getaddrinfo(target, target_port, &hints, targetinfo) != 0) {
    return -1;
  }
  if ((retfd = socket((*targetinfo)->ai_family, (*targetinfo)->ai_socktype,
                      (*targetinfo)->ai_protocol)) == -1) {
    return -2;
  }
  return retfd;
}


x86 assembly (clang, -O0):
<usock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rdx
call   1040 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1197 <usock+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <usock+0xa7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x8(%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11d1 <usock+0xa1>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d7 <usock+0xa7>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usock>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1040 <getaddrinfo@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11ad <usock+0x7d>
mov    (%r14),%rax
mov    0x8(%rax),%edi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1050 <socket@plt>
mov    %eax,%ecx
cmp    $0xffffffff,%eax
mov    $0xfffffffe,%eax
cmovne %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @usock(i8* %0, i8* %1, %struct.addrinfo** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.addrinfo**
%8 = alloca %struct.addrinfo
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.addrinfo** %2, %struct.addrinfo*** %7
%10 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 12)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = load %struct.addrinfo**, %struct.addrinfo*** %7
%18 = call i64 @getaddrinfo(i8* %15, i8* %16, %struct.addrinfo* %8, %struct.addrinfo** %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %39
21: ; preds = %3
%22 = load %struct.addrinfo**, %struct.addrinfo*** %7
%23 = load %struct.addrinfo*, %struct.addrinfo** %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = load %struct.addrinfo**, %struct.addrinfo*** %7
%27 = load %struct.addrinfo*, %struct.addrinfo** %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load %struct.addrinfo**, %struct.addrinfo*** %7
%31 = load %struct.addrinfo*, %struct.addrinfo** %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @socket(i32 %25, i32 %29, i32 %33)
store i32 %34, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %21
store i32 -2, i32* %4
br label %39
37: ; preds = %21
%38 = load i32, i32* %9
store i32 %38, i32* %4
br label %39
39: ; preds = %37, %36, %20
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @usock(i8* %0, i8* %1, %struct.addrinfo** %2) {
%4 = alloca %struct.addrinfo
%5 = bitcast %struct.addrinfo* %4 to i8*
%6 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 12)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %9, i32* %10
%11 = call i64 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %3
%14 = load %struct.addrinfo*, %struct.addrinfo** %2
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 2
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %16, i32 %18, i32 %20)
%22 = icmp eq i32 %21, -1
%23 = select i1 %22, i32 -2, i32 %21
br label %24
24: ; preds = %13, %3
%25 = phi i32 [ -1, %3 ], [ %23, %13 ]
ret i32 %25
}