#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct protoent {
  int /*<<< orphan*/ p_proto;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ endprotoent();
int /*<<< orphan*/ *fdopen(int, char *);
struct hostent *gethostbyname(char const *);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ htons(int);
void *inet_addr(char const *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
FILE *TcpOpen(const char *szHost, int nPort) {
  int s;
  struct sockaddr_in sa;
  struct hostent *he;
  struct protoent *pe;
  FILE *ps;
  pe = getprotobyname("TCP");
  s = socket(PF_INET, SOCK_STREAM, pe->p_proto);
  endprotoent();
  bzero((char *)&sa, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = inet_addr(szHost);
  sa.sin_port = htons(25);
  if ((he = gethostbyname(szHost)) != NULL)
    bcopy(he->h_addr, (char *)&sa.sin_addr, he->h_length);
  else if ((sa.sin_addr.s_addr = inet_addr(szHost)) < 0)
    perror("gethostbyname ()");
  if (connect(s, (struct sockaddr *)&sa, 16) == -1)
    perror("connect ()");
  else if ((ps = fdopen(s, "r+")) == NULL)
    perror("fdopen ()");
  else
    return ps;
  close(s);
  return NULL;
}


x86 assembly (clang, -O0):
<TcpOpen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe2a(%rip),%rdi
call   1080 <getprotobyname@plt>
mov    %rax,-0x38(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <endprotoent@plt>
lea    -0x28(%rbp),%rdi
mov    $0x10,%esi
call   10c0 <bzero@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
mov    $0x19,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     126c <TcpOpen+0xac>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
jmp    1294 <TcpOpen+0xd4>
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jae    128f <TcpOpen+0xcf>
lea    0xd7a(%rip),%rdi
call   10b0 <perror@plt>
jmp    1294 <TcpOpen+0xd4>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12bf <TcpOpen+0xff>
lea    0xd60(%rip),%rdi
call   10b0 <perror@plt>
jmp    12ff <TcpOpen+0x13f>
mov    -0x18(%rbp),%edi
lea    0xd57(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    12ed <TcpOpen+0x12d>
lea    0xd40(%rip),%rdi
call   10b0 <perror@plt>
jmp    12fa <TcpOpen+0x13a>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    130f <TcpOpen+0x14f>
jmp    12ff <TcpOpen+0x13f>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TcpOpen>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0xe30(%rip),%rdi
call   1080 <getprotobyname@plt>
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edi
mov    0x2df3(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <endprotoent@plt>
lea    0x8(%rsp),%rdi
mov    $0x10,%esi
call   10c0 <bzero@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %rax,0x8(%rsp)
mov    $0x19,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     124c <TcpOpen+0x8c>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x8(%rsp),%rsi
call   10a0 <bcopy@plt>
jmp    1259 <TcpOpen+0x99>
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     1289 <TcpOpen+0xc9>
lea    0xd99(%rip),%rsi
mov    %ebp,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1292 <TcpOpen+0xd2>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd74(%rip),%rdi
jmp    1299 <TcpOpen+0xd9>
lea    0xd79(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1282 <TcpOpen+0xc2>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.protoent = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"TCP\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str.2 = [11 x i8] c"connect ()\00"
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [10 x i8] c"fdopen ()\00"
define noalias i32* @TcpOpen(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = getelementptr inbounds %struct.protoent, %struct.protoent* %5, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @socket(i32 %6, i32 %7, i32 %9)
%11 = tail call i32 (...) @endprotoent()
%12 = call i32 @bzero(i8* %4, i32 16)
%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 = tail call i8* @inet_addr(i8* %0)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i8* %15, i8** %16
%17 = tail call i32 @htons(i32 25)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = tail call %struct.hostent* @gethostbyname(i8* %0)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %27, label %21
21: ; preds = %2
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @bcopy(i32 %23, i8* %4, i32 %25)
br label %29
27: ; preds = %2
%28 = tail call i8* @inet_addr(i8* %0)
store i8* %28, i8** %16
br label %29
29: ; preds = %27, %21
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i32 @connect(i32 %10, %struct.sockaddr* %30, i32 16)
%32 = icmp eq i32 %31, -1
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = call i32* @fdopen(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %40
36: ; preds = %33, %29
%37 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %29 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %33 ]
%38 = call i32 @perror(i8* %37)
%39 = call i32 @close(i32 %10)
br label %40
40: ; preds = %33, %36
%41 = phi i32* [ null, %36 ], [ %34, %33 ]
ret i32* %41
}