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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int start_client(char *hostname) {
  struct hostent *server_resolve;
  struct in_addr *server_address;
  struct sockaddr_in server;
  size_t address_length;
  int server_socket;
  server_resolve = gethostbyname(hostname);
  if (server_resolve == NULL) {
    fprintf(stderr, "Address not found for %s\n", hostname);
    return -1;
  }
  server_address = (struct in_addr *)server_resolve->h_addr_list[0];
  server.sin_family = AF_INET;
  server.sin_port = htons(2342);
  server.sin_addr.s_addr = inet_addr(inet_ntoa(*server_address));
  server_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (server_socket == -1) {
    perror("Error creating socket");
    return -1;
  }
  address_length = sizeof(struct sockaddr_in);
  if (connect(server_socket, (struct sockaddr *)&server, address_length) ==
      -1) {
    perror("Error connecting to server");
    return -1;
  }
  return server_socket;
}


x86 assembly (clang, -O0):
<start_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <start_client+0x49>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe48(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <start_client+0x102>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x926,%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%edi
call   1050 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1243 <start_client+0xc3>
lea    0xde8(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <start_client+0x102>
movq   $0xc,-0x38(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    127c <start_client+0xfc>
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1282 <start_client+0x102>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_client>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1201 <start_client+0x81>
mov    (%rax),%rax
mov    (%rax),%rbx
mov    0x2e47(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    $0x926,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%edi
call   1050 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1228 <start_client+0xa8>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
mov    %eax,%edi
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    121e <start_client+0x9e>
lea    0xe31(%rip),%rdi
jmp    122f <start_client+0xaf>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
jmp    1219 <start_client+0x99>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Address not found for %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Error creating socket\00"
@.str.2 = [27 x i8] c"Error connecting to server\00"
define i32 @start_client(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.hostent*
%5 = alloca %struct.in_addr*
%6 = alloca %struct.sockaddr_in
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %4
%11 = load %struct.hostent*, %struct.hostent** %4
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %2
br label %52
17: ; preds = %1
%18 = load %struct.hostent*, %struct.hostent** %4
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i32 0, i32 0
%20 = load i64*, i64** %19
%21 = getelementptr inbounds i64, i64* %20, i64 0
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to %struct.in_addr*
store %struct.in_addr* %23, %struct.in_addr** %5
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 2342)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %26, i32* %27
%28 = load %struct.in_addr*, %struct.in_addr** %5
%29 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @inet_ntoa(i32 %30)
%32 = call i32 @inet_addr(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* @AF_INET
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %17
%41 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
42: ; preds = %17
store i64 12, i64* %7
%43 = load i32, i32* %8
%44 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%45 = load i64, i64* %7
%46 = call i32 @connect(i32 %43, %struct.sockaddr* %44, i64 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %52
50: ; preds = %42
%51 = load i32, i32* %8
store i32 %51, i32* %2
br label %52
52: ; preds = %50, %48, %40, %13
%53 = load i32, i32* %2
ret i32 %53
}

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