#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 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int open_socket(struct sockaddr *addr) {
  int sockfd = 0;
  sockfd = socket(PF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    fprintf(stderr, "Open sockfd(TCP) error!\n");
    exit(-1);
  }
  if (connect(sockfd, addr, sizeof(struct sockaddr)) < 0) {
    fprintf(stderr, "Connect sockfd(TCP) error!\n");
    exit(-1);
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1198 <open_socket+0x58>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    11d2 <open_socket+0x92>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     1182 <open_socket+0x42>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <connect@plt>
test   %rax,%rax
js     1194 <open_socket+0x54>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
jmp    11a4 <open_socket+0x64>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Open sockfd(TCP) error!\0A\00"
@.str.1 = [28 x i8] c"Connect sockfd(TCP) error!\0A\00"
define i32 @open_socket(%struct.sockaddr* %0) {
%2 = alloca %struct.sockaddr*
%3 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %2
store i32 0, i32* %3
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %3
%15 = load %struct.sockaddr*, %struct.sockaddr** %2
%16 = call i64 @connect(i32 %14, %struct.sockaddr* %15, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %13
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Open sockfd(TCP) error!\0A\00"
@.str.1 = [28 x i8] c"Connect sockfd(TCP) error!\0A\00"
define i32 @open_socket(%struct.sockaddr* %0) {
%2 = load i32, i32* @PF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %1
%11 = tail call i64 @connect(i32 %4, %struct.sockaddr* %0, i32 4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %10
ret i32 %4
}