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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_with_server() {
  int newfd;
  struct sockaddr_in serv_addr;
  struct hostent *to;
  if ((to = gethostbyname("127.0.0.1")) == NULL) {
    fprintf(stderr, "gethostbyname() error...\n");
    return -1;
  }
  if ((newfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "socket() error...\n");
    return -1;
  }
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(7000);
  serv_addr.sin_addr = *((struct in_addr *)to->h_addr);
  memset(&(serv_addr.sin_zero), 0, 8);
  if (connect(newfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) ==
      -1) {
    fprintf(stderr, "connect() error...\n");
    return -1;
  } else {
    printf("Connected to server at %s:%d\n", "127.0.0.1", 7000);
    return newfd;
  }
}


x86 assembly (clang, -O0):
<connect_with_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b3 <connect_with_server+0x43>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <connect_with_server+0x117>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11f9 <connect_with_server+0x89>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <connect_with_server+0x117>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x1b58,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1269 <connect_with_server+0xf9>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <connect_with_server+0x117>
lea    0xddb(%rip),%rdi
lea    0xd89(%rip),%rsi
mov    $0x1b58,%edx
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_with_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe81(%rip),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     120f <connect_with_server+0x9f>
mov    %rax,%rbx
mov    0x2e51(%rip),%r14
mov    (%r14),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1221 <connect_with_server+0xb1>
mov    %eax,%ebp
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    $0x1b58,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1233 <connect_with_server+0xc3>
lea    0xe4f(%rip),%rdi
lea    0xdfd(%rip),%rsi
mov    $0x1b58,%edx
call   1040 <printf@plt>
jmp    124d <connect_with_server+0xdd>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
jmp    1243 <connect_with_server+0xd3>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
jmp    1243 <connect_with_server+0xd3>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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