#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*/ piaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int 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 a2open(char *ipaddr) {
  struct sockaddr_in piaddr;
  int res;
  int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd < 0) {
    perror("Cannot create socket");
    return -1;
  }
  memset(&piaddr, 0, sizeof(piaddr));
  piaddr.sin_family = AF_INET;
  piaddr.sin_port = htons(6551);
  res = inet_pton(AF_INET, ipaddr, &piaddr.sin_addr);
  if (res < 0) {
    perror("First parameter is not a valid address family");
    close(fd);
    return -1;
  } else if (res == 0) {
    perror("Char string (second parameter does not contain valid ipaddress)");
    close(fd);
    return -1;
  }
  if (connect(fd, (struct sockaddr *)&piaddr, sizeof(piaddr)) < 0) {
    perror("Connect failed");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<a2open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11c1 <a2open+0x51>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <a2open+0x12d>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1997,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <inet_pton@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    122d <a2open+0xbd>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <a2open+0x12d>
cmpl   $0x0,-0x24(%rbp)
jne    1257 <a2open+0xe7>
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <a2open+0x12d>
jmp    125c <a2open+0xec>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1297 <a2open+0x127>
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <a2open+0x12d>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a2open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1204 <a2open+0x94>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e2a(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x1997,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <inet_pton@plt>
test   %eax,%eax
js     1212 <a2open+0xa2>
je     121b <a2open+0xab>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    1233 <a2open+0xc3>
lea    0xe81(%rip),%rdi
jmp    1222 <a2open+0xb2>
lea    0xdf5(%rip),%rdi
call   1070 <perror@plt>
jmp    122e <a2open+0xbe>
lea    0xdfc(%rip),%rdi
jmp    1222 <a2open+0xb2>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@AF_INET = global i32 0
@.str.1 = [46 x i8] c"First parameter is not a valid address family\00"
@.str.2 = [64 x i8] c"Char string (second parameter does not contain valid ipaddress)\00"
@.str.3 = [15 x i8] c"Connect failed\00"
define i32 @a2open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %50
15: ; preds = %1
%16 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 6551)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i8*, i8** %3
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%24 = call i32 @inet_pton(i32 %21, i8* %22, i32* %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %2
br label %50
31: ; preds = %15
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %2
br label %50
38: ; preds = %31
br label %39
39: ; preds = %38
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%42 = call i64 @connect(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %2
br label %50
48: ; preds = %39
%49 = load i32, i32* %6
store i32 %49, i32* %2
br label %50
50: ; preds = %48, %44, %34, %27, %13
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@AF_INET = global i32 0
@.str.1 = [46 x i8] c"First parameter is not a valid address family\00"
@.str.2 = [64 x i8] c"Char string (second parameter does not contain valid ipaddress)\00"
@.str.3 = [15 x i8] c"Connect failed\00"
define i32 @a2open(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %36
11: ; preds = %1
%12 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 6551)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%19 = call i32 @inet_pton(i32 %17, i8* %0, i32* %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @close(i32 %7)
br label %36
24: ; preds = %11
%25 = icmp eq i32 %19, 0
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @close(i32 %7)
br label %36
29: ; preds = %24
%30 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%31 = call i64 @connect(i32 %7, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @close(i32 %7)
br label %36
36: ; preds = %29, %33, %26, %21, %9
%37 = phi i32 [ -1, %9 ], [ -1, %21 ], [ -1, %26 ], [ -1, %33 ], [ %7, %29 ]
ret i32 %37
}