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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct sockaddr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, char const *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int udp_client(const char *host, const char *serv, struct sockaddr **p_sa,
               socklen_t *p_len) {
  int sockfd, n;
  struct addrinfo hints, *res, *p_res;
  bzero(&hints, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_DGRAM;
  if (0 != (n = getaddrinfo(host, serv, &hints, &res))) {
    printf("udp_client error for %s, %s : %s", host, serv, gai_strerror(n));
    exit(EXIT_FAILURE);
  }
  p_res = res;
  while (NULL != res) {
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sockfd >= 0) {
      break;
    }
    res = res->ai_next;
  }
  if (NULL == res) {
    printf("udp_client error for %s, %s.\n", host, serv);
    exit(EXIT_FAILURE);
  }
  *p_sa = (struct sockaddr *)malloc(res->ai_addrlen);
  memcpy(*p_sa, res->ai_addr, res->ai_addrlen);
  *p_len = res->ai_addrlen;
  freeaddrinfo(p_res);
  return sockfd;
}


x86 assembly (clang, -O0):
<udp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x48(%rbp),%rdi
mov    $0x20,%esi
call   1070 <bzero@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
je     1231 <udp_client+0xa1>
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
xor    %eax,%eax
cmp    -0x50(%rbp),%rax
je     1282 <udp_client+0xf2>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1271 <udp_client+0xe1>
jmp    1282 <udp_client+0xf2>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1239 <udp_client+0xa9>
xor    %eax,%eax
cmp    -0x50(%rbp),%rax
jne    12b2 <udp_client+0x122>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1070 <bzero@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    128a <udp_client+0xfa>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1225 <udp_client+0x95>
mov    %rbx,%rax
nopl   0x0(%rax,%rax,1)
mov    0x18(%rax),%edi
mov    0x10(%rax),%edx
mov    0x14(%rax),%esi
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    (%rsp),%rax
test   %ebp,%ebp
jns    1247 <udp_client+0xb7>
mov    0x8(%rax),%rax
mov    %rax,(%rsp)
test   %rax,%rax
jne    1200 <udp_client+0x70>
lea    0xdf5(%rip),%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
test   %rax,%rax
je     1225 <udp_client+0x95>
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,0x0(%r13)
mov    (%rsp),%rcx
mov    (%rcx),%edx
mov    0x4(%rcx),%esi
mov    %rax,%rdi
call   1050 <memcpy@plt>
mov    (%rsp),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    %rbx,%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd68(%rip),%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1239 <udp_client+0xa9>

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, %struct.addrinfo*, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [33 x i8] c"udp_client error for %s, %s : %s\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [30 x i8] c"udp_client error for %s, %s.\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 @bzero(%struct.addrinfo* %5, i32 32)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 5
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_DGRAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 4
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 %21, label %16
16: ; preds = %4
%17 = call i8* @gai_strerror(i32 %14)
%18 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %4
%22 = load %struct.addrinfo*, %struct.addrinfo** %6
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %41, label %24
24: ; preds = %21, %35
%25 = phi %struct.addrinfo* [ %37, %35 ], [ %22, %21 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp sgt i32 %32, -1
%34 = load %struct.addrinfo*, %struct.addrinfo** %6
br i1 %33, label %39, label %35
35: ; preds = %24
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 2
%37 = load %struct.addrinfo*, %struct.addrinfo** %36
store %struct.addrinfo* %37, %struct.addrinfo** %6
%38 = icmp eq %struct.addrinfo* %37, null
br i1 %38, label %41, label %24
39: ; preds = %24
%40 = icmp eq %struct.addrinfo* %34, null
br i1 %40, label %41, label %45
41: ; preds = %35, %21, %39
%42 = call i32 (i8*, i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %39
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 0
%47 = load i32, i32* %46
%48 = call i64 @malloc(i32 %47)
%49 = inttoptr i64 %48 to %struct.sockaddr*
store %struct.sockaddr* %49, %struct.sockaddr** %2
%50 = load %struct.addrinfo*, %struct.addrinfo** %6
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i64 0, i32 1
%52 = load i32, i32* %51
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i64 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @memcpy(%struct.sockaddr* %49, i32 %52, i32 %54)
%56 = load %struct.addrinfo*, %struct.addrinfo** %6
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i64 0, i32 0
%58 = load i32, i32* %57
store i32 %58, i32* %3
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %22)
ret i32 %32
}