#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_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror_msg_and_die(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
FILE *open_socket(struct sockaddr_in *s_in, int port) {
  int fd;
  FILE *fp;
  s_in->sin_port = htons(port);
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    perror_msg_and_die("socket()");
  if (connect(fd, (struct sockaddr *)s_in, sizeof(*s_in)) < 0)
    perror_msg_and_die("connect()");
  if ((fp = fdopen(fd, "r+")) == NULL)
    perror_msg_and_die("fdopen()");
  return fp;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11a0 <open_socket+0x50>
lea    0xe65(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11c7 <open_socket+0x77>
lea    0xe47(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
mov    -0x10(%rbp),%edi
lea    0xe42(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f0 <open_socket+0xa0>
lea    0xe2b(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,(%rbx)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    118a <open_socket+0x3a>
lea    0xe7b(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11aa <open_socket+0x5a>
lea    0xe64(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
lea    0xe62(%rip),%rsi
mov    %ebp,%edi
call   1050 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11cc <open_socket+0x7c>
lea    0xe4f(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [10 x i8] c"connect()\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [9 x i8] c"fdopen()\00"
define noalias i32* @open_socket(%struct.sockaddr_in* %0, i32 %1) {
%3 = tail call i32 @htons(i32 %1)
%4 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
store i32 %3, i32* %4
%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_msg_and_die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = bitcast %struct.sockaddr_in* %0 to %struct.sockaddr*
%13 = tail call i64 @connect(i32 %7, %struct.sockaddr* %12, i32 4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @perror_msg_and_die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = tail call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @perror_msg_and_die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
ret i32* %18
}