#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 {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
struct sockaddr *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct sockaddr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int udp_client(const char *hostname, const char *service,
               struct sockaddr **saptr, socklen_t *lenp) {
  int sockfd, n;
  struct addrinfo hints, *res, *rp;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  if ((n = getaddrinfo(hostname, service, &hints, &res)) != 0) {
    fprintf(stderr, "udp_client - getaddrinfo() failed: %s\n", gai_strerror(n));
    return -1;
  }
  for (rp = res; rp != NULL; rp = rp->ai_next) {
    sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sockfd >= 0)
      break;
  }
  if (rp == NULL) {
    fprintf(stderr, "udp_client failed\n");
    freeaddrinfo(res);
    return -1;
  }
  *saptr = malloc(res->ai_addrlen);
  if (*saptr == NULL) {
    fprintf(stderr, "udp_client - malloc() failed\n");
    freeaddrinfo(res);
    return -1;
  }
  memcpy(*saptr, res->ai_addr, res->ai_addrlen);
  *lenp = res->ai_addrlen;
  freeaddrinfo(res);
  return sockfd;
}


x86 assembly (clang, -O0):
<udp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     1215 <udp_client+0x95>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x30(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <udp_client+0x1a4>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     126a <udp_client+0xea>
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1254 <udp_client+0xd4>
jmp    126a <udp_client+0xea>
jmp    1259 <udp_client+0xd9>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    121d <udp_client+0x9d>
cmpq   $0x0,-0x60(%rbp)
jne    12a1 <udp_client+0x121>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x58(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <udp_client+0x1a4>
mov    -0x58(%rbp),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12f0 <udp_client+0x170>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd66(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x58(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <udp_client+0x1a4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x58(%rbp),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     120a <udp_client+0x8a>
mov    0x2dec(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    129b <udp_client+0x11b>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     123b <udp_client+0xbb>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
test   %eax,%eax
jns    1263 <udp_client+0xe3>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1220 <udp_client+0xa0>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    $0xffffffff,%ebp
jmp    129b <udp_client+0x11b>
mov    %eax,%ebp
mov    0x8(%rsp),%rbx
mov    (%rbx),%r15d
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     12aa <udp_client+0x12a>
mov    0x4(%rbx),%esi
mov    %rax,%rdi
mov    %r15d,%edx
call   1060 <memcpy@plt>
mov    0x8(%rsp),%rdi
mov    (%rdi),%eax
mov    %eax,(%r14)
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
jmp    124b <udp_client+0xcb>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"udp_client - getaddrinfo() failed: %s\0A\00"
@.str.1 = [19 x i8] c"udp_client failed\0A\00"
@.str.2 = [30 x i8] c"udp_client - malloc() failed\0A\00"
define i32 @udp_client(i8* %0, i8* %1, %struct.sockaddr** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.sockaddr**
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.addrinfo
%13 = alloca %struct.addrinfo*
%14 = alloca %struct.addrinfo*
store i8* %0, i8** %6
store i8* %1, i8** %7
store %struct.sockaddr** %2, %struct.sockaddr*** %8
store i32* %3, i32** %9
%15 = call i32 @memset(%struct.addrinfo* %12, i32 0, i32 32)
%16 = load i32, i32* @AF_UNSPEC
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_DGRAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 3
store i32 %18, i32* %19
%20 = load i8*, i8** %6
%21 = load i8*, i8** %7
%22 = call i32 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %12, %struct.addrinfo** %13)
store i32 %22, i32* %11
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %4
%25 = load i32, i32* @stderr
%26 = load i32, i32* %11
%27 = call i8* @gai_strerror(i32 %26)
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 -1, i32* %5
br label %92
29: ; preds = %4
%30 = load %struct.addrinfo*, %struct.addrinfo** %13
store %struct.addrinfo* %30, %struct.addrinfo** %14
br label %31
31: ; preds = %49, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %14
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %53
34: ; preds = %31
%35 = load %struct.addrinfo*, %struct.addrinfo** %14
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 4
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %14
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 3
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %14
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 2
%43 = load i32, i32* %42
%44 = call i32 @socket(i32 %37, i32 %40, i32 %43)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp sge i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %34
br label %53
48: ; preds = %34
br label %49
49: ; preds = %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %14
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 5
%52 = load %struct.addrinfo*, %struct.addrinfo** %51
store %struct.addrinfo* %52, %struct.addrinfo** %14
br label %31
53: ; preds = %47, %31
%54 = load %struct.addrinfo*, %struct.addrinfo** %14
%55 = icmp eq %struct.addrinfo* %54, null
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%59 = load %struct.addrinfo*, %struct.addrinfo** %13
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
store i32 -1, i32* %5
br label %92
61: ; preds = %53
%62 = load %struct.addrinfo*, %struct.addrinfo** %13
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call %struct.sockaddr* @malloc(i32 %64)
%66 = load %struct.sockaddr**, %struct.sockaddr*** %8
store %struct.sockaddr* %65, %struct.sockaddr** %66
%67 = load %struct.sockaddr**, %struct.sockaddr*** %8
%68 = load %struct.sockaddr*, %struct.sockaddr** %67
%69 = icmp eq %struct.sockaddr* %68, null
br i1 %69, label %70, label %75
70: ; preds = %61
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%73 = load %struct.addrinfo*, %struct.addrinfo** %13
%74 = call i32 @freeaddrinfo(%struct.addrinfo* %73)
store i32 -1, i32* %5
br label %92
75: ; preds = %61
%76 = load %struct.sockaddr**, %struct.sockaddr*** %8
%77 = load %struct.sockaddr*, %struct.sockaddr** %76
%78 = load %struct.addrinfo*, %struct.addrinfo** %13
%79 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %78, i32 0, i32 1
%80 = load i32, i32* %79
%81 = load %struct.addrinfo*, %struct.addrinfo** %13
%82 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = call i32 @memcpy(%struct.sockaddr* %77, i32 %80, i32 %83)
%85 = load %struct.addrinfo*, %struct.addrinfo** %13
%86 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %85, i32 0, i32 0
%87 = load i32, i32* %86
%88 = load i32*, i32** %9
store i32 %87, i32* %88
%89 = load %struct.addrinfo*, %struct.addrinfo** %13
%90 = call i32 @freeaddrinfo(%struct.addrinfo* %89)
%91 = load i32, i32* %10
store i32 %91, i32* %5
br label %92
92: ; preds = %75, %70, %56, %24
%93 = load i32, i32* %5
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"udp_client - getaddrinfo() failed: %s\0A\00"
@.str.1 = [19 x i8] c"udp_client failed\0A\00"
@.str.2 = [30 x i8] c"udp_client - malloc() failed\0A\00"
define i32 @udp_client(i8* %0, i8* %1, %struct.sockaddr** nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo* %5 to i8*
%8 = bitcast %struct.addrinfo** %6 to i8*
%9 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 32)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_DGRAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %5, %struct.addrinfo** %6)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load %struct.addrinfo*, %struct.addrinfo** %6
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %37, label %27
19: ; preds = %4
%20 = load i32, i32* @stderr
%21 = call i8* @gai_strerror(i32 %14)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %61
23: ; preds = %27
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%25 = load %struct.addrinfo*, %struct.addrinfo** %24
%26 = icmp eq %struct.addrinfo* %25, null
br i1 %26, label %37, label %27
27: ; preds = %16, %23
%28 = phi %struct.addrinfo* [ %25, %23 ], [ %17, %16 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %30, i32 %32, i32 %34)
%36 = icmp sgt i32 %35, -1
br i1 %36, label %42, label %23
37: ; preds = %23, %16
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%40 = load %struct.addrinfo*, %struct.addrinfo** %6
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
br label %61
42: ; preds = %27
%43 = load %struct.addrinfo*, %struct.addrinfo** %6
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 0
%45 = load i32, i32* %44
%46 = call %struct.sockaddr* @malloc(i32 %45)
store %struct.sockaddr* %46, %struct.sockaddr** %2
%47 = icmp eq %struct.sockaddr* %46, null
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%51 = load %struct.addrinfo*, %struct.addrinfo** %6
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %61
53: ; preds = %42
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i64 0, i32 1
%55 = load i32, i32* %54
%56 = call i32 @memcpy(%struct.sockaddr* %46, i32 %55, i32 %45)
%57 = load %struct.addrinfo*, %struct.addrinfo** %6
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i64 0, i32 0
%59 = load i32, i32* %58
store i32 %59, i32* %3
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
br label %61
61: ; preds = %53, %48, %37, %19
%62 = phi i32 [ -1, %19 ], [ -1, %37 ], [ -1, %48 ], [ %35, %53 ]
ret i32 %62
}