#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
struct hostent *gethostbyname(char *);
scalar_t__ h_errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int socketConnect(char *ip, int portnumber) {
  struct sockaddr_in server_addr;
  struct hostent *host;
  int sockfd;
  if ((host = gethostbyname(ip)) == NULL) {
    fprintf(stderr, "Gethostname error:%s, the ip addr is: %s\n",
            strerror(h_errno), ip);
    return -1;
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "Socket Error:%s\a\n", strerror(errno));
    return -1;
  }
  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(portnumber);
  server_addr.sin_addr = *((struct in_addr *)host->h_addr);
  if (connect(sockfd, (struct sockaddr *)(&server_addr),
              sizeof(struct sockaddr)) == -1) {
    if (EINPROGRESS != errno) {
      printf("connect() failed:%s\n", strerror(errno));
      return -1;
    }
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<socketConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e5 <socketConnect+0x65>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <socketConnect+0x15f>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1245 <socketConnect+0xc5>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <socketConnect+0x15f>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12d9 <socketConnect+0x159>
mov    0x2d49(%rip),%rax
mov    (%rax),%rax
mov    0x2d17(%rip),%rcx
cmp    (%rcx),%rax
je     12d4 <socketConnect+0x154>
mov    0x2d07(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd79(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <socketConnect+0x15f>
jmp    12d9 <socketConnect+0x159>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketConnect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     123a <socketConnect+0xba>
mov    %rax,%rbp
mov    0x2e3e(%rip),%r12
mov    (%r12),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     126a <socketConnect+0xea>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    129a <socketConnect+0x11a>
mov    0x2dcb(%rip),%rax
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%rdi
cmp    %rdi,(%rax)
je     129a <socketConnect+0x11a>
call   1090 <strerror@plt>
lea    0xe0c(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    1295 <socketConnect+0x115>
mov    0x2d87(%rip),%rax
mov    (%rax),%ebp
mov    0x2d8e(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xda7(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1295 <socketConnect+0x115>
mov    0x2d57(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3e(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xda1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"Gethostname error:%s, the ip addr is: %s\0A\00"
@h_errno = global i64 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [18 x i8] c"Socket Error:%s\07\0A\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.2 = [21 x i8] c"connect() failed:%s\0A\00"
define i32 @socketConnect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %7
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i64, i64* @h_errno
%15 = call i8* @strerror(i64 %14)
%16 = load i8*, i8** %4
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
store i32 -1, i32* %3
br label %57
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 -1, i32* %3
br label %57
28: ; preds = %18
%29 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %5
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%36 = load %struct.hostent*, %struct.hostent** %7
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 0
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to %struct.in_addr*
%40 = bitcast %struct.in_addr* %35 to i8*
%41 = bitcast %struct.in_addr* %39 to i8*
%42 = load i32, i32* %8
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = call i32 @connect(i32 %42, %struct.sockaddr* %43, i32 4)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %55
46: ; preds = %28
%47 = load i64, i64* @EINPROGRESS
%48 = load i64, i64* @errno
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = call i8* @strerror(i64 %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %52)
store i32 -1, i32* %3
br label %57
54: ; preds = %46
br label %55
55: ; preds = %54, %28
%56 = load i32, i32* %8
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %50, %23, %12
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"Gethostname error:%s, the ip addr is: %s\0A\00"
@h_errno = global i64 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [18 x i8] c"Socket Error:%s\07\0A\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.2 = [21 x i8] c"connect() failed:%s\0A\00"
define i32 @socketConnect(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i64, i64* @h_errno
%10 = tail call i8* @strerror(i64 %9)
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %0)
br label %43
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i64, i64* @errno
%20 = tail call i8* @strerror(i64 %19)
%21 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %43
22: ; preds = %12
%23 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = tail call i32 @htons(i32 %1)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to i32*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%32 = load i32, i32* %30
store i32 %32, i32* %31
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i32 @connect(i32 %15, %struct.sockaddr* %33, i32 4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %43
36: ; preds = %22
%37 = load i64, i64* @EINPROGRESS
%38 = load i64, i64* @errno
%39 = icmp eq i64 %37, %38
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = call i8* @strerror(i64 %38)
%42 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %22, %36, %40, %17, %7
%44 = phi i32 [ -1, %7 ], [ -1, %17 ], [ -1, %40 ], [ %15, %36 ], [ %15, %22 ]
ret i32 %44
}