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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connectToSocket(int SSocket, char *port, char *serverhost) {
  struct addrinfo serv_addr;
  struct addrinfo *serverinfo, *p;
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.ai_family = AF_UNSPEC;
  serv_addr.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(serverhost, port, &serv_addr, &serverinfo) != 0) {
    perror("Address not found");
    return 0;
  }
  for (p = serverinfo; p != NULL; p = p->ai_next) {
    SSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (SSocket == -1) {
      perror("Socket");
      return 0;
    }
    if (connect(SSocket, p->ai_addr, p->ai_addrlen) == -1) {
      perror("connect");
      return 0;
    }
    break;
  }
  if (p == NULL) {
    fprintf(stderr, "failed to connect\n");
    exit(2);
  }
  return SSocket;
}


x86 assembly (clang, -O0):
<connectToSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11e2 <connectToSocket+0x72>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    129f <connectToSocket+0x12f>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     126f <connectToSocket+0xff>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1234 <connectToSocket+0xc4>
lea    0xdef(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    129f <connectToSocket+0x12f>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    126a <connectToSocket+0xfa>
lea    0xdc0(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    129f <connectToSocket+0x12f>
jmp    126f <connectToSocket+0xff>
cmpq   $0x0,-0x48(%rbp)
jne    1299 <connectToSocket+0x129>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectToSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1030 <memset@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
jne    1200 <connectToSocket+0x90>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     1222 <connectToSocket+0xb2>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1209 <connectToSocket+0x99>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1212 <connectToSocket+0xa2>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf9(%rip),%rdi
jmp    1219 <connectToSocket+0xa9>
lea    0xe02(%rip),%rdi
jmp    1219 <connectToSocket+0xa9>
lea    0xe00(%rip),%rdi
call   1050 <perror@plt>
xor    %ebp,%ebp
jmp    11f3 <connectToSocket+0x83>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [18 x i8] c"Address not found\00"
@.str.1 = [7 x i8] c"Socket\00"
@.str.2 = [8 x i8] c"connect\00"
@stderr = global i32 0
@.str.3 = [19 x i8] c"failed to connect\0A\00"
define i32 @connectToSocket(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 32)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %14, i32* %15
%16 = load i8*, i8** %7
%17 = load i8*, i8** %6
%18 = call i64 @getaddrinfo(i8* %16, i8* %17, %struct.addrinfo* %8, %struct.addrinfo** %9)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %68
22: ; preds = %3
%23 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %23, %struct.addrinfo** %10
br label %24
24: ; preds = %55, %22
%25 = load %struct.addrinfo*, %struct.addrinfo** %10
%26 = icmp ne %struct.addrinfo* %25, null
br i1 %26, label %27, label %59
27: ; preds = %24
%28 = load %struct.addrinfo*, %struct.addrinfo** %10
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 4
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %10
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 3
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %10
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %30, i32 %33, i32 %36)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %27
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %68
42: ; preds = %27
%43 = load i32, i32* %5
%44 = load %struct.addrinfo*, %struct.addrinfo** %10
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 1
%46 = load i32, i32* %45
%47 = load %struct.addrinfo*, %struct.addrinfo** %10
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @connect(i32 %43, i32 %46, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %42
%53 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %68
54: ; preds = %42
br label %59
55: ; No predecessors!
%56 = load %struct.addrinfo*, %struct.addrinfo** %10
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 5
%58 = load %struct.addrinfo*, %struct.addrinfo** %57
store %struct.addrinfo* %58, %struct.addrinfo** %10
br label %24
59: ; preds = %54, %24
%60 = load %struct.addrinfo*, %struct.addrinfo** %10
%61 = icmp eq %struct.addrinfo* %60, null
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%65 = call i32 @exit(i32 2)
unreachable
66: ; preds = %59
%67 = load i32, i32* %5
store i32 %67, i32* %4
br label %68
68: ; preds = %66, %52, %40, %20
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [18 x i8] c"Address not found\00"
@.str.1 = [7 x i8] c"Socket\00"
@.str.2 = [8 x i8] c"connect\00"
@stderr = global i32 0
@.str.3 = [19 x i8] c"failed to connect\0A\00"
define i32 @connectToSocket(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %11, i32* %12
%13 = call i64 @getaddrinfo(i8* %2, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %38
15: ; preds = %3
%16 = load %struct.addrinfo*, %struct.addrinfo** %5
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %34, label %18
18: ; preds = %15
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 4
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 3
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 2
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %20, i32 %22, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %38, label %27
27: ; preds = %18
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @connect(i32 %25, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %38, label %41
34: ; preds = %15
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 2)
unreachable
38: ; preds = %27, %18, %3
%39 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %18 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %27 ]
%40 = call i32 @perror(i8* %39)
br label %41
41: ; preds = %38, %27
%42 = phi i32 [ %25, %27 ], [ 0, %38 ]
ret i32 %42
}