#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_SCTP;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_SEQPACKET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int xyz_sock_connect(char *addr, int port, int type) {
  struct sockaddr_in sin;
  int sockfd;
  struct hostent *ht;
  if (addr == NULL) {
    return -1;
  }
  switch (type) {
  case 1:
    sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    break;
  case 2:
    sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    break;
  case 3:
    sockfd = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
    break;
  default:
    return -1;
  }
  if (sockfd == -1) {
    perror("socket() error");
    return -1;
  }
  ht = gethostbyname(addr);
  if (ht == NULL) {
    perror("gethostbyname() error");
    return -1;
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_addr.s_addr = *((in_addr_t *)(ht->h_addr));
  sin.sin_port = htons(port);
  sin.sin_family = AF_INET;
  if (connect(sockfd, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1) {
    perror("connect() error");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<xyz_sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1189 <xyz_sock_connect+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <xyz_sock_connect+0x1a0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x1,%eax
je     11bf <xyz_sock_connect+0x5f>
jmp    119d <xyz_sock_connect+0x3d>
mov    -0x3c(%rbp),%eax
sub    $0x2,%eax
je     11e7 <xyz_sock_connect+0x87>
jmp    11ae <xyz_sock_connect+0x4e>
mov    -0x3c(%rbp),%eax
sub    $0x3,%eax
je     120f <xyz_sock_connect+0xaf>
jmp    1237 <xyz_sock_connect+0xd7>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
jmp    1243 <xyz_sock_connect+0xe3>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
jmp    1243 <xyz_sock_connect+0xe3>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
jmp    1243 <xyz_sock_connect+0xe3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <xyz_sock_connect+0x1a0>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1265 <xyz_sock_connect+0x105>
lea    0xdac(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <xyz_sock_connect+0x1a0>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1295 <xyz_sock_connect+0x135>
lea    0xd8b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <xyz_sock_connect+0x1a0>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    12fa <xyz_sock_connect+0x19a>
lea    0xd3c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <xyz_sock_connect+0x1a0>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz_sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     1217 <xyz_sock_connect+0xb7>
add    $0xffffffff,%edx
cmp    $0x2,%edx
ja     1217 <xyz_sock_connect+0xb7>
mov    %esi,%r14d
mov    %rdi,%rbp
movslq %edx,%rax
lea    0x2c4c(%rip),%rcx
mov    (%rcx,%rax,8),%rcx
lea    0x2c59(%rip),%rdx
mov    (%rdx,%rax,8),%rax
mov    0x2e2e(%rip),%rdx
mov    (%rdx),%edi
mov    (%rcx),%esi
mov    (%rax),%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1224 <xyz_sock_connect+0xc4>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     122d <xyz_sock_connect+0xcd>
mov    %rax,%rbp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1236 <xyz_sock_connect+0xd6>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd5(%rip),%rdi
jmp    123d <xyz_sock_connect+0xdd>
lea    0xddb(%rip),%rdi
jmp    123d <xyz_sock_connect+0xdd>
lea    0xde8(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1217 <xyz_sock_connect+0xb7>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOCK_SEQPACKET = global i32 0
@IPPROTO_SCTP = global i32 0
@.str = [15 x i8] c"socket() error\00"
@.str.1 = [22 x i8] c"gethostbyname() error\00"
@AF_INET = global i32 0
@.str.2 = [16 x i8] c"connect() error\00"
define i32 @xyz_sock_connect(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca %struct.hostent*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %66
14: ; preds = %3
%15 = load i32, i32* %7
switch i32 %15, label %31 [
i32 1, label %16
i32 2, label %21
i32 3, label %26
]
16: ; preds = %14
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = load i32, i32* @IPPROTO_TCP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %9
br label %32
21: ; preds = %14
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_DGRAM
%24 = load i32, i32* @IPPROTO_UDP
%25 = call i32 @socket(i32 %22, i32 %23, i32 %24)
store i32 %25, i32* %9
br label %32
26: ; preds = %14
%27 = load i32, i32* @PF_INET
%28 = load i32, i32* @SOCK_SEQPACKET
%29 = load i32, i32* @IPPROTO_SCTP
%30 = call i32 @socket(i32 %27, i32 %28, i32 %29)
store i32 %30, i32* %9
br label %32
31: ; preds = %14
store i32 -1, i32* %4
br label %66
32: ; preds = %26, %21, %16
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %66
37: ; preds = %32
%38 = load i8*, i8** %5
%39 = call %struct.hostent* @gethostbyname(i8* %38)
store %struct.hostent* %39, %struct.hostent** %10
%40 = load %struct.hostent*, %struct.hostent** %10
%41 = icmp eq %struct.hostent* %40, null
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %66
44: ; preds = %37
%45 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%46 = load %struct.hostent*, %struct.hostent** %10
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to i32*
%50 = load i32, i32* %49
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i32 %50, i32* %52
%53 = load i32, i32* %6
%54 = call i32 @htons(i32 %53)
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %54, i32* %55
%56 = load i32, i32* @AF_INET
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %56, i32* %57
%58 = load i32, i32* %9
%59 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%60 = call i32 @connect(i32 %58, %struct.sockaddr* %59, i32 4)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %44
%63 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %66
64: ; preds = %44
%65 = load i32, i32* %9
store i32 %65, i32* %4
br label %66
66: ; preds = %64, %62, %42, %35, %31, %13
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOCK_SEQPACKET = global i32 0
@IPPROTO_SCTP = global i32 0
@.str = [15 x i8] c"socket() error\00"
@.str.1 = [22 x i8] c"gethostbyname() error\00"
@AF_INET = global i32 0
@.str.2 = [16 x i8] c"connect() error\00"
@switch.table.xyz_sock_connect = [3 x i32*] [i32* @SOCK_STREAM, i32* @SOCK_DGRAM, i32* @SOCK_SEQPACKET]
@switch.table.xyz_sock_connect.3 = [3 x i32*] [i32* @IPPROTO_TCP, i32* @IPPROTO_UDP, i32* @IPPROTO_SCTP]
define i32 @xyz_sock_connect(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = icmp eq i8* %0, null
br i1 %6, label %45, label %7
7: ; preds = %3
%8 = add i32 %2, -1
%9 = icmp ult i32 %8, 3
br i1 %9, label %10, label %45
10: ; preds = %7
%11 = sext i32 %8 to i64
%12 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.xyz_sock_connect, i64 0, i64 %11
%13 = load i32*, i32** %12
%14 = sext i32 %8 to i64
%15 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.xyz_sock_connect.3, i64 0, i64 %14
%16 = load i32*, i32** %15
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* %13
%19 = load i32, i32* %16
%20 = tail call i32 @socket(i32 %17, i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %45
24: ; preds = %10
%25 = tail call %struct.hostent* @gethostbyname(i8* %0)
%26 = icmp eq %struct.hostent* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %45
29: ; preds = %24
%30 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i64 0, i32 0
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to i32*
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
store i32 %34, i32* %35
%36 = call i32 @htons(i32 %1)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %36, i32* %37
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %38, i32* %39
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i32 @connect(i32 %20, %struct.sockaddr* %40, i32 4)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %29
%44 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %7, %29, %3, %43, %27, %22
%46 = phi i32 [ -1, %22 ], [ -1, %27 ], [ -1, %43 ], [ -1, %3 ], [ -1, %7 ], [ %20, %29 ]
ret i32 %46
}