#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;
};
typedef int /*<<< orphan*/ addrport;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_client_socket(int *socket_id, int client_port, char *host) {
  struct sockaddr_in addrport;
  *socket_id = socket(AF_INET, SOCK_STREAM, 0);
  if (*socket_id == -1) {
    printf("Socket creation failed\n");
    return -1;
  }
  addrport.sin_family = AF_INET;
  addrport.sin_port = htons(client_port);
  addrport.sin_addr.s_addr = inet_addr(host);
  if (connect(*socket_id, (struct sockaddr *)&addrport, sizeof(addrport)) ==
      -1) {
    printf("Connect failed\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_client_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    11a9 <setup_client_socket+0x59>
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <setup_client_socket+0xb8>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    1201 <setup_client_socket+0xb1>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <setup_client_socket+0xb8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_client_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e7c(%rip),%r12
mov    (%r12),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     11c5 <setup_client_socket+0x75>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    %rsp,%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11d6 <setup_client_socket+0x86>
lea    0xe3d(%rip),%rdi
jmp    11cc <setup_client_socket+0x7c>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Socket creation failed\0A\00"
@.str.1 = [16 x i8] c"Connect failed\0A\00"
define i32 @setup_client_socket(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = load i32*, i32** %5
store i32 %11, i32* %12
%13 = load i32*, i32** %5
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %36
18: ; preds = %3
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* %6
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i8*, i8** %7
%25 = call i32 @inet_addr(i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
%30 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %18
%34 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %36
35: ; preds = %18
store i32 0, i32* %4
br label %36
36: ; preds = %35, %33, %16
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [15 x i8] c"Connect failed\00"
@str.2 = [23 x i8] c"Socket creation failed\00"
define i32 @setup_client_socket(i32* nocapture %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %0
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.2, i64 0, i64 0))
br label %25
12: ; preds = %3
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = tail call i32 @inet_addr(i8* %2)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %0
%20 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%21 = call i32 @connect(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %12, %23, %10
%26 = phi i32 [ -1, %10 ], [ -1, %23 ], [ 0, %12 ]
ret i32 %26
}