#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;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int chat_client_init(int server_port, const char *server_name) {
  int client_fd;
  struct sockaddr_in server_addr;
  struct hostent *server_host;
  client_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (client_fd < 0) {
    perror("chat_client_init, socket");
    return -1;
  }
  if ((server_host = gethostbyname(server_name)) == NULL) {
    perror("chat_client_init, gethostbyname");
    return -2;
  }
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(server_port);
  server_addr.sin_addr = *((struct in_addr *)server_host->h_addr);
  if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
      0) {
    perror("chat_client_init, connect");
    return -3;
  }
  return client_fd;
}


x86 assembly (clang, -O0):
<chat_client_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119d <chat_client_init+0x4d>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <chat_client_init+0xd8>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11cc <chat_client_init+0x7c>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1228 <chat_client_init+0xd8>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%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    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1222 <chat_client_init+0xd2>
lea    0xe28(%rip),%rdi
call   1050 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1228 <chat_client_init+0xd8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chat_client_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
mov    0x2e81(%rip),%r15
mov    (%r15),%edi
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11d0 <chat_client_init+0x80>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11e3 <chat_client_init+0x93>
mov    %rax,%rbp
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11f6 <chat_client_init+0xa6>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe29(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11c3 <chat_client_init+0x73>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffe,%ebx
jmp    11c3 <chat_client_init+0x73>
lea    0xe3c(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffd,%ebx
jmp    11c3 <chat_client_init+0x73>

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

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