#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;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  unsigned long **h_addr_list;
  int h_length;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ addr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
unsigned long INADDR_NONE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ *fdopen(int, char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
unsigned long inet_addr(char const *);
int /*<<< orphan*/ memcpy(char *, unsigned long *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
FILE *open_tcp_stream(const char *host, int port) {
  struct sockaddr_in server;
  struct hostent *host_info;
  unsigned long addr;
  int sock;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    return 0;
  memset(&server, 0, sizeof(server));
  if ((addr = inet_addr(host)) != INADDR_NONE) {
    memcpy((char *)&server.sin_addr, &addr, sizeof(addr));
  } else {
    host_info = gethostbyname(host);
    if (NULL == host_info)
      return 0;
    memcpy((char *)&server.sin_addr, host_info->h_addr_list[0],
           host_info->h_length);
  }
  server.sin_family = AF_INET;
  server.sin_port = htons(port);
  if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
    return 0;
  FILE *stream = fdopen(sock, "r");
  if (stream == NULL) {
    close(sock);
    return NULL;
  }
  return stream;
}


x86 assembly (clang, -O0):
<open_tcp_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11d2 <open_tcp_stream+0x42>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <open_tcp_stream+0x148>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %rax,-0x30(%rbp)
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
je     121a <open_tcp_stream+0x8a>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
jmp    125e <open_tcp_stream+0xce>
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1240 <open_tcp_stream+0xb0>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <open_tcp_stream+0x148>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <memcpy@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    129d <open_tcp_stream+0x10d>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <open_tcp_stream+0x148>
mov    -0x34(%rbp),%edi
lea    0xd59(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12d0 <open_tcp_stream+0x140>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <open_tcp_stream+0x148>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_tcp_stream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e3f(%rip),%r12
mov    (%r12),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1271 <open_tcp_stream+0xe1>
mov    %eax,%r15d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    %rbp,%rdi
call   1060 <inet_addr@plt>
mov    %rax,0x18(%rsp)
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <open_tcp_stream+0x81>
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     126f <open_tcp_stream+0xdf>
lea    0x10(%rsp),%rdi
mov    (%rax),%rcx
mov    (%rcx),%rsi
mov    0x8(%rax),%edx
jmp    1220 <open_tcp_stream+0x90>
lea    0x10(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     126f <open_tcp_stream+0xdf>
lea    0xda9(%rip),%rsi
mov    %r15d,%edi
call   1090 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1271 <open_tcp_stream+0xe1>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64**, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_NONE = global i64 0
@.str = [2 x i8] c"r\00"
define i32* @open_tcp_stream(i8* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%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* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32* null, i32** %3
br label %66
17: ; preds = %2
%18 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%19 = load i8*, i8** %4
%20 = call i64 @inet_addr(i8* %19)
store i64 %20, i64* %8
%21 = load i64, i64* @INADDR_NONE
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%25 = bitcast i32* %24 to i8*
%26 = call i32 @memcpy(i8* %25, i64* %8, i32 8)
br label %45
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %7
%30 = load %struct.hostent*, %struct.hostent** %7
%31 = icmp eq %struct.hostent* null, %30
br i1 %31, label %32, label %33
32: ; preds = %27
store i32* null, i32** %3
br label %66
33: ; preds = %27
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%35 = bitcast i32* %34 to i8*
%36 = load %struct.hostent*, %struct.hostent** %7
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 0
%38 = load i64**, i64*** %37
%39 = getelementptr inbounds i64*, i64** %38, i64 0
%40 = load i64*, i64** %39
%41 = load %struct.hostent*, %struct.hostent** %7
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 1
%43 = load i32, i32* %42
%44 = call i32 @memcpy(i8* %35, i64* %40, i32 %43)
br label %45
45: ; preds = %33, %23
%46 = load i32, i32* @AF_INET
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %46, i32* %47
%48 = load i32, i32* %5
%49 = call i32 @htons(i32 %48)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %49, i32* %50
%51 = load i32, i32* %9
%52 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%53 = call i64 @connect(i32 %51, %struct.sockaddr* %52, i32 12)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %45
store i32* null, i32** %3
br label %66
56: ; preds = %45
%57 = load i32, i32* %9
%58 = call i32* @fdopen(i32 %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %58, i32** %10
%59 = load i32*, i32** %10
%60 = icmp eq i32* %59, null
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
store i32* null, i32** %3
br label %66
64: ; preds = %56
%65 = load i32*, i32** %10
store i32* %65, i32** %3
br label %66
66: ; preds = %64, %61, %55, %32, %16
%67 = load i32*, i32** %3
ret i32* %67
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64**, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_NONE = global i64 0
@.str = [2 x i8] c"r\00"
define noalias i32* @open_tcp_stream(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %45, label %11
11: ; preds = %2
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%13 = call i64 @inet_addr(i8* %0)
store i64 %13, i64* %4
%14 = load i64, i64* @INADDR_NONE
%15 = icmp eq i64 %13, %14
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%18 = bitcast i32* %17 to i8*
%19 = call i32 @memcpy(i8* %18, i64* %4, i32 8)
br label %32
20: ; preds = %11
%21 = call %struct.hostent* @gethostbyname(i8* %0)
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %45, label %23
23: ; preds = %20
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%25 = bitcast i32* %24 to i8*
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 0, i32 0
%27 = load i64**, i64*** %26
%28 = load i64*, i64** %27
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 @memcpy(i8* %25, i64* %28, i32 %30)
br label %32
32: ; preds = %23, %16
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %33, i32* %34
%35 = call i32 @htons(i32 %1)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %35, i32* %36
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = call i64 @connect(i32 %9, %struct.sockaddr* %37, i32 12)
%39 = icmp slt i64 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %32
%41 = call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @close(i32 %9)
br label %45
45: ; preds = %43, %40, %32, %20, %2
%46 = phi i32* [ null, %2 ], [ null, %20 ], [ null, %32 ], [ null, %43 ], [ %41, %40 ]
ret i32* %46
}