#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
struct forth_instance {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ iswhitespace(char);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int st_pop(struct forth_instance *);
int /*<<< orphan*/ st_push(struct forth_instance *, int);
void openconn(struct forth_instance *fi) {
  struct hostent *he;
  struct sockaddr_in their_addr;
  int sd, i = 0, port;
  char *toconn, back;
  toconn = (char *)st_pop(fi);
  port = st_pop(fi);
  while (toconn[i] && !iswhitespace(toconn[i]))
    i++;
  back = toconn[i];
  toconn[i] = '\0';
  if ((he = gethostbyname(toconn)) == NULL) {
    st_push(fi, (int)0);
    toconn[i] = back;
    return;
  }
  toconn[i] = back;
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    st_push(fi, (int)0);
    return;
  }
  their_addr.sin_family = AF_INET;
  their_addr.sin_port = htons(port);
  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  memset(&(their_addr.sin_zero), '\0', 8);
  if (connect(sd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) ==
      -1) {
    st_push(fi, (int)0);
    return;
  }
  st_push(fi, (int)sd);
  return;
}


x86 assembly (clang, -O0):
<openconn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <st_pop@plt>
cltq
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <st_pop@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x3a(%rbp)
je     11e4 <openconn+0x64>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <iswhitespace@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
test   $0x1,%al
jne    11f4 <openconn+0x74>
jmp    1202 <openconn+0x82>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ae <openconn+0x2e>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x39(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1251 <openconn+0xd1>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1080 <st_push@plt>
mov    -0x39(%rbp),%dl
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    12fd <openconn+0x17d>
mov    -0x39(%rbp),%dl
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1294 <openconn+0x114>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1080 <st_push@plt>
jmp    12fd <openconn+0x17d>
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f1 <openconn+0x171>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1080 <st_push@plt>
jmp    12fd <openconn+0x17d>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <st_push@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openconn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
call   1060 <st_pop@plt>
movslq %eax,%rbp
mov    %r15,%rdi
call   1060 <st_pop@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%al
test   %al,%al
je     11d2 <openconn+0x52>
xor    %ebx,%ebx
nopl   (%rax)
movsbl %al,%edi
call   1030 <iswhitespace@plt>
test   %eax,%eax
jne    11c9 <openconn+0x49>
movzbl 0x1(%rbp,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    11b0 <openconn+0x30>
mov    %ebx,%eax
mov    0x0(%rbp,%rax,1),%r12b
jmp    11d7 <openconn+0x57>
xor    %r12d,%r12d
xor    %ebx,%ebx
mov    %ebx,%ebx
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     125e <openconn+0xde>
mov    %rax,%r13
mov    %r12b,0x0(%rbp,%rbx,1)
mov    0x2dee(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     126f <openconn+0xef>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x0(%r13),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %r15,%rdi
cmp    $0xffffffff,%eax
je     1287 <openconn+0x107>
mov    %ebp,%esi
jmp    1289 <openconn+0x109>
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <st_push@plt>
mov    %r12b,0x0(%rbp,%rbx,1)
jmp    128e <openconn+0x10e>
mov    %r15,%rdi
xor    %esi,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <st_push@plt>
xor    %esi,%esi
call   1080 <st_push@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.forth_instance = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define void @openconn(%struct.forth_instance* %0) {
%2 = alloca %struct.forth_instance*
%3 = alloca %struct.hostent*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8
store %struct.forth_instance* %0, %struct.forth_instance** %2
store i32 0, i32* %6
%10 = load %struct.forth_instance*, %struct.forth_instance** %2
%11 = call i32 @st_pop(%struct.forth_instance* %10)
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %8
%14 = load %struct.forth_instance*, %struct.forth_instance** %2
%15 = call i32 @st_pop(%struct.forth_instance* %14)
store i32 %15, i32* %7
br label %16
16: ; preds = %35, %1
%17 = load i8*, i8** %8
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %16
%25 = load i8*, i8** %8
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = call i32 @iswhitespace(i8 signext %29)
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
br label %33
33: ; preds = %24, %16
%34 = phi i1 [ false, %16 ], [ %32, %24 ]
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %16
38: ; preds = %33
%39 = load i8*, i8** %8
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
store i8 %43, i8* %9
%44 = load i8*, i8** %8
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %8
%49 = call %struct.hostent* @gethostbyname(i8* %48)
store %struct.hostent* %49, %struct.hostent** %3
%50 = icmp eq %struct.hostent* %49, null
br i1 %50, label %51, label %59
51: ; preds = %38
%52 = load %struct.forth_instance*, %struct.forth_instance** %2
%53 = call i32 @st_push(%struct.forth_instance* %52, i32 0)
%54 = load i8, i8* %9
%55 = load i8*, i8** %8
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 %54, i8* %58
br label %98
59: ; preds = %38
%60 = load i8, i8* %9
%61 = load i8*, i8** %8
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 %60, i8* %64
%65 = load i32, i32* @AF_INET
%66 = load i32, i32* @SOCK_STREAM
%67 = call i32 @socket(i32 %65, i32 %66, i32 0)
store i32 %67, i32* %5
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %72
69: ; preds = %59
%70 = load %struct.forth_instance*, %struct.forth_instance** %2
%71 = call i32 @st_push(%struct.forth_instance* %70, i32 0)
br label %98
72: ; preds = %59
%73 = load i32, i32* @AF_INET
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 3
store i32 %73, i32* %74
%75 = load i32, i32* %7
%76 = call i32 @htons(i32 %75)
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %76, i32* %77
%78 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%79 = load %struct.hostent*, %struct.hostent** %3
%80 = getelementptr inbounds %struct.hostent, %struct.hostent* %79, i32 0, i32 0
%81 = load i64, i64* %80
%82 = inttoptr i64 %81 to %struct.in_addr*
%83 = bitcast %struct.in_addr* %78 to i8*
%84 = bitcast %struct.in_addr* %82 to i8*
%85 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%86 = call i32 @memset(i32* %85, i8 signext 0, i32 8)
%87 = load i32, i32* %5
%88 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%89 = call i32 @connect(i32 %87, %struct.sockaddr* %88, i32 4)
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %94
91: ; preds = %72
%92 = load %struct.forth_instance*, %struct.forth_instance** %2
%93 = call i32 @st_push(%struct.forth_instance* %92, i32 0)
br label %98
94: ; preds = %72
%95 = load %struct.forth_instance*, %struct.forth_instance** %2
%96 = load i32, i32* %5
%97 = call i32 @st_push(%struct.forth_instance* %95, i32 %96)
br label %98
98: ; preds = %94, %91, %69, %51
ret void
}

LLVM IR (-O2):
%struct.forth_instance = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define void @openconn(%struct.forth_instance* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = tail call i32 @st_pop(%struct.forth_instance* %0)
%5 = sext i32 %4 to i64
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @st_pop(%struct.forth_instance* %0)
%8 = load i8, i8* %6
%9 = icmp eq i8 %8, 0
br i1 %9, label %29, label %10
10: ; preds = %1, %16
%11 = phi i64 [ %17, %16 ], [ 0, %1 ]
%12 = phi i8 [ %20, %16 ], [ %8, %1 ]
%13 = phi i32 [ %18, %16 ], [ 0, %1 ]
%14 = tail call i32 @iswhitespace(i8 signext %12)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = add nuw i64 %11, 1
%18 = add nuw nsw i32 %13, 1
%19 = getelementptr inbounds i8, i8* %6, i64 %17
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %24, label %10
22: ; preds = %10
%23 = trunc i64 %11 to i32
br label %24
24: ; preds = %16, %22
%25 = phi i32 [ %23, %22 ], [ %18, %16 ]
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %6, i64 %26
%28 = load i8, i8* %27
br label %29
29: ; preds = %1, %24
%30 = phi i8 [ %28, %24 ], [ 0, %1 ]
%31 = phi i32 [ %25, %24 ], [ 0, %1 ]
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %6, i64 %32
store i8 0, i8* %33
%34 = tail call %struct.hostent* @gethostbyname(i8* %6)
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = tail call i32 @st_push(%struct.forth_instance* %0, i32 0)
store i8 %30, i8* %33
br label %64
38: ; preds = %29
store i8 %30, i8* %33
%39 = load i32, i32* @AF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = tail call i32 @socket(i32 %39, i32 %40, i32 0)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = tail call i32 @st_push(%struct.forth_instance* %0, i32 0)
br label %64
45: ; preds = %38
%46 = load i32, i32* @AF_INET
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %46, i32* %47
%48 = tail call i32 @htons(i32 %7)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %48, i32* %49
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i64 0, i32 0
%51 = load i64, i64* %50
%52 = inttoptr i64 %51 to i32*
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%54 = load i32, i32* %52
store i32 %54, i32* %53
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%56 = call i32 @memset(i32* %55, i8 signext 0, i32 8)
%57 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%58 = call i32 @connect(i32 %41, %struct.sockaddr* %57, i32 4)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %45
%61 = call i32 @st_push(%struct.forth_instance* %0, i32 0)
br label %64
62: ; preds = %45
%63 = call i32 @st_push(%struct.forth_instance* %0, i32 %41)
br label %64
64: ; preds = %62, %60, %43, %36
ret void
}