#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int client_create(char *addr, int port) {
  struct sockaddr_in serv_addr;
  int fd;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Error : Could not create socket");
    return -1;
  }
  memset(&serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  if (inet_pton(AF_INET, addr, &serv_addr.sin_addr) <= 0) {
    perror("inet_pton error occured");
    return -1;
  }
  if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("Error : Connect Failed");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<client_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ac <client_create+0x4c>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <client_create+0xe7>
lea    -0x20(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%rax
jg     120e <client_create+0xae>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <client_create+0xe7>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1241 <client_create+0xe1>
lea    0xe08(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <client_create+0xe7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e70(%rip),%r12
mov    (%r12),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f1 <client_create+0x91>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <inet_pton@plt>
test   %rax,%rax
jle    11fa <client_create+0x9a>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     1203 <client_create+0xa3>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rdi
jmp    120a <client_create+0xaa>
lea    0xe1f(%rip),%rdi
jmp    120a <client_create+0xaa>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11e2 <client_create+0x82>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"Error : Could not create socket\00"
@.str.1 = [24 x i8] c"inet_pton error occured\00"
@.str.2 = [23 x i8] c"Error : Connect Failed\00"
define i32 @client_create(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_in* %6, i32 48, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* %5
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i8*, i8** %4
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%24 = call i64 @inet_pton(i32 %21, i8* %22, i32* %23)
%25 = icmp sle i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
28: ; preds = %14
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
35: ; preds = %28
%36 = load i32, i32* %7
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %33, %26, %12
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"Error : Could not create socket\00"
@.str.1 = [24 x i8] c"inet_pton error occured\00"
@.str.2 = [23 x i8] c"Error : Connect Failed\00"
define i32 @client_create(i8* %0, i32 %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 ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %29
11: ; preds = %2
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 48, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%19 = call i64 @inet_pton(i32 %17, i8* %0, i32* %18)
%20 = icmp slt i64 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %29
23: ; preds = %11
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @connect(i32 %7, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %23, %27, %21, %9
%30 = phi i32 [ -1, %9 ], [ -1, %21 ], [ -1, %27 ], [ %7, %23 ]
ret i32 %30
}