#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 ai_protocol;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int getSocket() {
  const char *addr = "127.0.0.1";
  int sfd, rv;
  char _port[6];
  struct addrinfo hints, *servinfo, *p;
  snprintf(_port, 6, "%d", 6379);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(addr, _port, &hints, &servinfo)) != 0) {
    hints.ai_family = AF_INET6;
    if ((rv = getaddrinfo(addr, _port, &hints, &servinfo)) != 0) {
      printf("error address\n");
      return -1;
    }
  }
  p = servinfo;
  printf("%d\n", p->ai_protocol);
  if ((sfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
    printf("error create socket\n");
    return -1;
  }
  if (connect(sfd, p->ai_addr, p->ai_addrlen) == -1) {
    printf("connect error\n");
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<getSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xe91(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x1e(%rbp),%rdi
mov    $0x6,%esi
lea    0xe87(%rip),%rdx
mov    $0x18eb,%ecx
call   1040 <snprintf@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1222 <getSocket+0xc2>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     121d <getSocket+0xbd>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <getSocket+0x15c>
jmp    1222 <getSocket+0xc2>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    127d <getSocket+0x11d>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <getSocket+0x15c>
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b6 <getSocket+0x156>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <getSocket+0x15c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0xe8b(%rip),%rdx
lea    0x12(%rsp),%r14
mov    %r14,%rdi
mov    $0x6,%esi
mov    $0x18eb,%ecx
call   1050 <snprintf@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0xe35(%rip),%rdi
lea    0x8(%rsp),%rcx
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1214 <getSocket+0xa4>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0xe0d(%rip),%rdi
lea    0x12(%rsp),%rsi
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     1214 <getSocket+0xa4>
lea    0xe21(%rip),%rdi
jmp    125f <getSocket+0xef>
mov    0x8(%rsp),%rbx
mov    (%rbx),%esi
lea    0xdeb(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%edi
mov    (%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1258 <getSocket+0xe8>
mov    %eax,%ebp
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1269 <getSocket+0xf9>
lea    0xdbb(%rip),%rdi
jmp    125f <getSocket+0xef>
lea    0xdc0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [3 x i8] c"%d\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET6 = global i32 0
@.str.2 = [15 x i8] c"error address\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [21 x i8] c"error create socket\0A\00"
@.str.5 = [15 x i8] c"connect error\0A\00"
define i32 @getSocket() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [6 x i8]
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %2
%9 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 6379)
%11 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 20)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %14, i32* %15
%16 = load i8*, i8** %2
%17 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%18 = call i32 @getaddrinfo(i8* %16, i8* %17, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %18, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %0
%21 = load i32, i32* @AF_INET6
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %21, i32* %22
%23 = load i8*, i8** %2
%24 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%25 = call i32 @getaddrinfo(i8* %23, i8* %24, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %25, i32* %4
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %63
29: ; preds = %20
br label %30
30: ; preds = %29, %0
%31 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %31, %struct.addrinfo** %8
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = load %struct.addrinfo*, %struct.addrinfo** %8
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 4
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %8
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 3
%41 = load i32, i32* %40
%42 = load %struct.addrinfo*, %struct.addrinfo** %8
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @socket(i32 %38, i32 %41, i32 %44)
store i32 %45, i32* %3
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %30
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %63
49: ; preds = %30
%50 = load i32, i32* %3
%51 = load %struct.addrinfo*, %struct.addrinfo** %8
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 2
%53 = load i32, i32* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %8
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = call i32 @connect(i32 %50, i32 %53, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %49
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %63
61: ; preds = %49
%62 = load i32, i32* %3
store i32 %62, i32* %1
br label %63
63: ; preds = %61, %59, %47, %27
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [3 x i8] c"%d\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET6 = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@str = [14 x i8] c"connect error\00"
@str.6 = [20 x i8] c"error create socket\00"
@str.7 = [14 x i8] c"error address\00"
define i32 @getSocket() {
%1 = alloca [6 x i8]
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = call i32 @snprintf(i8* %4, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 6379)
%8 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 20)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %4, %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %0
%16 = load i32, i32* @AF_INET6
store i32 %16, i32* %10
%17 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %4, %struct.addrinfo* %2, %struct.addrinfo** %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %15, %0
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 3
%27 = load i32, i32* %26
%28 = load i32, i32* %21
%29 = call i32 @socket(i32 %25, i32 %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %38, label %31
31: ; preds = %19
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 2
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 1
%35 = load i32, i32* %34
%36 = call i32 @connect(i32 %29, i32 %33, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %31, %19, %15
%39 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0), %15 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0), %19 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0), %31 ]
%40 = call i32 @puts(i8* dereferenceable(1) %39)
br label %41
41: ; preds = %38, %31
%42 = phi i32 [ %29, %31 ], [ -1, %38 ]
ret i32 %42
}