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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read_socket(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_MailServer() {
  struct sockaddr_in server;
  struct hostent *hp = NULL;
  int sockfd = -1;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == sockfd) {
    perror("socket");
    return sockfd;
  }
  hp = gethostbyname("smtp.163.com");
  if (NULL == hp) {
    perror("gethostbyname");
    return -1;
  }
  memset(&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
  server.sin_port = htons(25);
  if (connect(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("connect");
    return -1;
  }
  read_socket(sockfd);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_MailServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x1c(%rbp),%eax
jne    11d8 <connect_MailServer+0x58>
lea    0xe38(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1291 <connect_MailServer+0x111>
lea    0xe28(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    120c <connect_MailServer+0x8c>
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <connect_MailServer+0x111>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
add    $0x4,%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    $0x19,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1283 <connect_MailServer+0x103>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <connect_MailServer+0x111>
mov    -0x1c(%rbp),%edi
call   1040 <read_socket@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_MailServer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e57(%rip),%r15
mov    (%r15),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1220 <connect_MailServer+0xa0>
mov    %eax,%ebp
lea    0xe55(%rip),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1229 <connect_MailServer+0xa9>
mov    %rax,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
lea    0xc(%rsp),%rdi
mov    0x8(%rbx),%rsi
mov    (%rbx),%edx
call   1070 <memcpy@plt>
mov    $0x19,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1232 <connect_MailServer+0xb2>
mov    %ebp,%edi
call   1040 <read_socket@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
jmp    1239 <connect_MailServer+0xb9>
lea    0xde4(%rip),%rdi
jmp    1239 <connect_MailServer+0xb9>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1213 <connect_MailServer+0x93>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [13 x i8] c"smtp.163.com\00"
@.str.2 = [14 x i8] c"gethostbyname\00"
@.str.3 = [8 x i8] c"connect\00"
define i32 @connect_MailServer() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %35
9: ; preds = %0
%10 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %35
14: ; preds = %9
%15 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%19 = bitcast i32* %18 to i8*
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i8*
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @memcpy(i8* %19, i8* %22, i32 %24)
%26 = call i32 @htons(i32 25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%29 = call i64 @connect(i32 %5, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %14
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %35
33: ; preds = %14
%34 = call i32 @read_socket(i32 %5)
br label %35
35: ; preds = %33, %31, %12, %7
%36 = phi i32 [ -1, %7 ], [ -1, %12 ], [ -1, %31 ], [ %5, %33 ]
ret i32 %36
}