#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 */
typedef int u_short;
typedef int u_long;
struct sockaddr_in {
  void *sin_port;
  int /*<<< orphan*/ sin_addr;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ s;

/* Variables and functions */
void *AF_INET;
scalar_t__ EAGAIN;
scalar_t__ ECONNREFUSED;
scalar_t__ ECONNRESET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(void *, void *, int /*<<< orphan*/);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(void *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int getpid();
void *htons(int);
int /*<<< orphan*/ perror(char *);
int pmap_getport(struct sockaddr_in *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ sock_optimize(int, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_connect(char *host, int prog, int rdwr) {
  static struct hostent *h;
  static struct sockaddr_in s;
  static u_short save_port;
  static u_long save_prog;
  static char *save_host;
  int sock;
  static int tries = 0;
  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    perror("socket");
    exit(1);
  }
  if (rdwr & 0x0004) {
    static unsigned short port;
    struct sockaddr_in sin;
    if (!port) {
      port = (unsigned short)(getpid() << 4);
      if (port < 1024) {
        port += 1024;
      }
    }
    do {
      port++;
      bzero((void *)&sin, sizeof(sin));
      sin.sin_family = AF_INET;
      sin.sin_port = htons(port);
    } while (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1);
  }
  sock_optimize(sock, rdwr);
  if (!h || host != save_host || prog != save_prog) {
    save_host = host;
    save_prog = prog;
    if (!(h = gethostbyname(host))) {
      perror(host);
      exit(2);
    }
    bzero((void *)&s, sizeof(s));
    s.sin_family = AF_INET;
    bcopy((void *)h->h_addr, (void *)&s.sin_addr, h->h_length);
    if (prog > 0) {
      save_port = pmap_getport(&s, prog, (u_long)1, IPPROTO_TCP);
      if (!save_port) {
        perror("lib TCP: No port found");
        exit(3);
      }
      s.sin_port = htons(save_port);
    } else {
      s.sin_port = htons(-prog);
    }
  }
  if (connect(sock, (struct sockaddr *)&s, sizeof(s)) < 0) {
    if (errno == ECONNRESET || errno == ECONNREFUSED || errno == EAGAIN) {
      close(sock);
      if (++tries > 10)
        return (-1);
      return (tcp_connect(host, prog, rdwr));
    }
    perror("connect");
    exit(4);
  }
  tries = 0;
  return (sock);
}


x86 assembly (clang, -O0):
<tcp_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1235 <tcp_connect+0x55>
lea    0xdda(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     12ef <tcp_connect+0x10f>
cmpw   $0x0,0x2e94(%rip)
jne    128d <tcp_connect+0xad>
mov    $0x0,%al
call   1030 <getpid@plt>
shl    $0x4,%eax
mov    %ax,0x2e7d(%rip)
movzwl 0x2e76(%rip),%eax
cmp    $0x400,%eax
jge    1288 <tcp_connect+0xa8>
movzwl 0x2e64(%rip),%eax
add    $0x400,%eax
mov    %ax,0x2e58(%rip)
jmp    128d <tcp_connect+0xad>
jmp    1292 <tcp_connect+0xb2>
mov    0x2e47(%rip),%ax
add    $0x1,%ax
mov    %ax,0x2e3c(%rip)
lea    -0x38(%rbp),%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movzwl 0x2e19(%rip),%edi
call   1050 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1292 <tcp_connect+0xb2>
jmp    12ef <tcp_connect+0x10f>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10d0 <sock_optimize@plt>
cmpq   $0x0,0x2d96(%rip)
je     1328 <tcp_connect+0x148>
mov    -0x10(%rbp),%rax
cmp    0x2dad(%rip),%rax
jne    1328 <tcp_connect+0x148>
mov    -0x14(%rbp),%eax
cmp    0x2d9a(%rip),%eax
je     142c <tcp_connect+0x24c>
mov    -0x10(%rbp),%rax
mov    %rax,0x2d8d(%rip)
mov    -0x14(%rbp),%eax
mov    %eax,0x2d80(%rip)
mov    -0x10(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,0x2d4c(%rip)
cmp    $0x0,%rax
jne    1369 <tcp_connect+0x189>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    0x2d30(%rip),%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    0x2c67(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x2d25(%rip)
mov    0x2d06(%rip),%rax
mov    0x8(%rax),%rdi
mov    0x2cfb(%rip),%rax
mov    (%rax),%edx
lea    0x2cfa(%rip),%rsi
add    $0x8,%rsi
call   1090 <bcopy@plt>
cmpl   $0x0,-0x14(%rbp)
jle    1416 <tcp_connect+0x236>
mov    -0x14(%rbp),%esi
mov    0x2c15(%rip),%rax
mov    (%rax),%ecx
lea    0x2cd4(%rip),%rdi
mov    $0x1,%edx
call   1060 <pmap_getport@plt>
mov    %eax,0x2cdc(%rip)
cmpl   $0x0,0x2cd5(%rip)
jne    13ff <tcp_connect+0x21f>
lea    0xc17(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
mov    0x2cb3(%rip),%edi
call   1050 <htons@plt>
mov    %rax,0x2c8f(%rip)
jmp    1427 <tcp_connect+0x247>
xor    %edi,%edi
sub    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %rax,0x2c79(%rip)
jmp    142c <tcp_connect+0x24c>
mov    -0x1c(%rbp),%edi
lea    0x2c6a(%rip),%rsi
mov    $0x18,%edx
call   10f0 <connect@plt>
cmp    $0x0,%rax
jge    14f1 <tcp_connect+0x311>
mov    0x2b67(%rip),%rax
mov    (%rax),%rax
mov    0x2b6d(%rip),%rcx
cmp    (%rcx),%rax
je     1498 <tcp_connect+0x2b8>
mov    0x2b4d(%rip),%rax
mov    (%rax),%rax
mov    0x2b6b(%rip),%rcx
cmp    (%rcx),%rax
je     1498 <tcp_connect+0x2b8>
mov    0x2b33(%rip),%rax
mov    (%rax),%rax
mov    0x2b19(%rip),%rcx
cmp    (%rcx),%rax
jne    14db <tcp_connect+0x2fb>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    0x2c22(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2c19(%rip)
cmp    $0xa,%eax
jle    14c4 <tcp_connect+0x2e4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1501 <tcp_connect+0x321>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <tcp_connect@plt>
mov    %eax,-0x4(%rbp)
jmp    1501 <tcp_connect+0x321>
lea    0xb3c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10e0 <exit@plt>
movl   $0x0,0x2bcd(%rip)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%r14
mov    0x2dea(%rip),%r13
mov    0x0(%r13),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1427 <tcp_connect+0x247>
mov    %eax,%ebx
test   $0x4,%r15b
je     12c3 <tcp_connect+0xe3>
mov    %r14,0x8(%rsp)
movzwl 0x2ea6(%rip),%r14d
test   %r14w,%r14w
jne    125e <tcp_connect+0x7e>
xor    %eax,%eax
call   1030 <getpid@plt>
shl    $0x4,%eax
movzwl %ax,%ecx
lea    0x400(%rax),%r14d
cmp    $0x400,%ecx
cmovae %eax,%r14d
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r14d
mov    %r14w,0x2e64(%rip)
mov    %rbp,%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    0x0(%r13),%rax
mov    %rax,0x20(%rsp)
movzwl %r14w,%edi
call   1050 <htons@plt>
mov    %rax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12be <tcp_connect+0xde>
movzwl 0x2e24(%rip),%r14d
jmp    1270 <tcp_connect+0x90>
mov    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r15d,%esi
call   10d0 <sock_optimize@plt>
cmpq   $0x0,0x2dc3(%rip)
je     12ed <tcp_connect+0x10d>
cmp    %r14,0x2de2(%rip)
jne    12ed <tcp_connect+0x10d>
cmp    %r12d,0x2dd1(%rip)
je     1394 <tcp_connect+0x1b4>
mov    %r14,0x2dcc(%rip)
mov    %r12d,0x2dbd(%rip)
mov    %r14,%rdi
call   1080 <gethostbyname@plt>
mov    %rax,0x2d8e(%rip)
test   %rax,%rax
je     143d <tcp_connect+0x25d>
mov    %rax,%rbp
lea    0x2d83(%rip),%rdi
mov    $0x18,%esi
call   10c0 <bzero@plt>
mov    0x0(%r13),%rax
mov    %rax,0x2d7e(%rip)
mov    0x8(%rbp),%rsi
mov    0x0(%rbp),%edx
lea    0x2d68(%rip),%rdi
call   10a0 <memmove@plt>
test   %r12d,%r12d
jle    1383 <tcp_connect+0x1a3>
mov    0x2c87(%rip),%rax
mov    (%rax),%ecx
lea    0x2d46(%rip),%rdi
mov    %r12d,%esi
mov    $0x1,%edx
call   1060 <pmap_getport@plt>
mov    %eax,%edi
test   %eax,%eax
jne    1388 <tcp_connect+0x1a8>
lea    0xc93(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
mov    %r12d,%edi
neg    %edi
call   1050 <htons@plt>
mov    %rax,0x2d0c(%rip)
lea    0x2d05(%rip),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   10f0 <connect@plt>
test   %rax,%rax
js     13b8 <tcp_connect+0x1d8>
movl   $0x0,0x2d12(%rip)
jmp    1416 <tcp_connect+0x236>
mov    0x2bf9(%rip),%rax
mov    (%rax),%rax
mov    0x2bff(%rip),%rcx
cmp    (%rcx),%rax
je     13e6 <tcp_connect+0x206>
mov    0x2c0b(%rip),%rcx
cmp    (%rcx),%rax
je     13e6 <tcp_connect+0x206>
mov    0x2bc7(%rip),%rcx
cmp    (%rcx),%rax
jne    144f <tcp_connect+0x26f>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2cd5(%rip),%eax
lea    0x1(%rax),%ecx
mov    %ecx,0x2ccc(%rip)
mov    $0xffffffff,%ebx
cmp    $0x9,%eax
jg     1416 <tcp_connect+0x236>
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1040 <tcp_connect@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbd2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %r14,%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    0xbc8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i64 }
%struct.sockaddr_in = type { i8*, i32, i8* }
%struct.sockaddr = type { i32 }
@tcp_connect.h = internal global %struct.hostent* null
@tcp_connect.s = internal global %struct.sockaddr_in zeroinitializer
@tcp_connect.save_port = internal global i32 0
@tcp_connect.save_prog = internal global i32 0
@tcp_connect.save_host = internal global i8* null
@tcp_connect.tries = internal global i32 0
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@tcp_connect.port = internal global i16 0
@.str.1 = [23 x i8] c"lib TCP: No port found\00"
@errno = global i64 0
@ECONNRESET = global i64 0
@ECONNREFUSED = global i64 0
@EAGAIN = global i64 0
@.str.2 = [8 x i8] c"connect\00"
define i32 @tcp_connect(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = call i32 @socket(i8* %10, i32 %11, i32 %12)
store i32 %13, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i32, i32* %7
%20 = and i32 %19, 4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %56
22: ; preds = %18
%23 = load i16, i16* @tcp_connect.port
%24 = icmp ne i16 %23, 0
br i1 %24, label %38, label %25
25: ; preds = %22
%26 = call i32 (...) @getpid()
%27 = shl i32 %26, 4
%28 = trunc i32 %27 to i16
store i16 %28, i16* @tcp_connect.port
%29 = load i16, i16* @tcp_connect.port
%30 = zext i16 %29 to i32
%31 = icmp slt i32 %30, 1024
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = load i16, i16* @tcp_connect.port
%34 = zext i16 %33 to i32
%35 = add nsw i32 %34, 1024
%36 = trunc i32 %35 to i16
store i16 %36, i16* @tcp_connect.port
br label %37
37: ; preds = %32, %25
br label %38
38: ; preds = %37, %22
br label %39
39: ; preds = %50, %38
%40 = load i16, i16* @tcp_connect.port
%41 = add i16 %40, 1
store i16 %41, i16* @tcp_connect.port
%42 = bitcast %struct.sockaddr_in* %9 to i8*
%43 = call i32 @bzero(i8* %42, i32 24)
%44 = load i8*, i8** @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i8* %44, i8** %45
%46 = load i16, i16* @tcp_connect.port
%47 = zext i16 %46 to i32
%48 = call i8* @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i8* %48, i8** %49
br label %50
50: ; preds = %39
%51 = load i32, i32* %8
%52 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%53 = call i32 @bind(i32 %51, %struct.sockaddr* %52, i32 24)
%54 = icmp eq i32 %53, -1
br i1 %54, label %39, label %55
55: ; preds = %50
br label %56
56: ; preds = %55, %18
%57 = load i32, i32* %8
%58 = load i32, i32* %7
%59 = call i32 @sock_optimize(i32 %57, i32 %58)
%60 = load %struct.hostent*, %struct.hostent** @tcp_connect.h
%61 = icmp ne %struct.hostent* %60, null
br i1 %61, label %62, label %70
62: ; preds = %56
%63 = load i8*, i8** %5
%64 = load i8*, i8** @tcp_connect.save_host
%65 = icmp ne i8* %63, %64
br i1 %65, label %70, label %66
66: ; preds = %62
%67 = load i32, i32* %6
%68 = load i32, i32* @tcp_connect.save_prog
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %110
70: ; preds = %66, %62, %56
%71 = load i8*, i8** %5
store i8* %71, i8** @tcp_connect.save_host
%72 = load i32, i32* %6
store i32 %72, i32* @tcp_connect.save_prog
%73 = load i8*, i8** %5
%74 = call %struct.hostent* @gethostbyname(i8* %73)
store %struct.hostent* %74, %struct.hostent** @tcp_connect.h
%75 = icmp ne %struct.hostent* %74, null
br i1 %75, label %80, label %76
76: ; preds = %70
%77 = load i8*, i8** %5
%78 = call i32 @perror(i8* %77)
%79 = call i32 @exit(i32 2)
unreachable
80: ; preds = %70
%81 = call i32 @bzero(i8* bitcast (%struct.sockaddr_in* @tcp_connect.s to i8*), i32 24)
%82 = load i8*, i8** @AF_INET
store i8* %82, i8** getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @tcp_connect.s, i32 0, i32 2)
%83 = load %struct.hostent*, %struct.hostent** @tcp_connect.h
%84 = getelementptr inbounds %struct.hostent, %struct.hostent* %83, i32 0, i32 1
%85 = load i64, i64* %84
%86 = inttoptr i64 %85 to i8*
%87 = load %struct.hostent*, %struct.hostent** @tcp_connect.h
%88 = getelementptr inbounds %struct.hostent, %struct.hostent* %87, i32 0, i32 0
%89 = load i32, i32* %88
%90 = call i32 @bcopy(i8* %86, i8* bitcast (i32* getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @tcp_connect.s, i32 0, i32 1) to i8*), i32 %89)
%91 = load i32, i32* %6
%92 = icmp sgt i32 %91, 0
br i1 %92, label %93, label %105
93: ; preds = %80
%94 = load i32, i32* %6
%95 = load i32, i32* @IPPROTO_TCP
%96 = call i32 @pmap_getport(%struct.sockaddr_in* @tcp_connect.s, i32 %94, i32 1, i32 %95)
store i32 %96, i32* @tcp_connect.save_port
%97 = load i32, i32* @tcp_connect.save_port
%98 = icmp ne i32 %97, 0
br i1 %98, label %102, label %99
99: ; preds = %93
%100 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%101 = call i32 @exit(i32 3)
unreachable
102: ; preds = %93
%103 = load i32, i32* @tcp_connect.save_port
%104 = call i8* @htons(i32 %103)
store i8* %104, i8** getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @tcp_connect.s, i32 0, i32 0)
br label %109
105: ; preds = %80
%106 = load i32, i32* %6
%107 = sub nsw i32 0, %106
%108 = call i8* @htons(i32 %107)
store i8* %108, i8** getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @tcp_connect.s, i32 0, i32 0)
br label %109
109: ; preds = %105, %102
br label %110
110: ; preds = %109, %66
%111 = load i32, i32* %8
%112 = call i64 @connect(i32 %111, %struct.sockaddr* bitcast (%struct.sockaddr_in* @tcp_connect.s to %struct.sockaddr*), i32 24)
%113 = icmp slt i64 %112, 0
br i1 %113, label %114, label %141
114: ; preds = %110
%115 = load i64, i64* @errno
%116 = load i64, i64* @ECONNRESET
%117 = icmp eq i64 %115, %116
br i1 %117, label %126, label %118
118: ; preds = %114
%119 = load i64, i64* @errno
%120 = load i64, i64* @ECONNREFUSED
%121 = icmp eq i64 %119, %120
br i1 %121, label %126, label %122
122: ; preds = %118
%123 = load i64, i64* @errno
%124 = load i64, i64* @EAGAIN
%125 = icmp eq i64 %123, %124
br i1 %125, label %126, label %138
126: ; preds = %122, %118, %114
%127 = load i32, i32* %8
%128 = call i32 @close(i32 %127)
%129 = load i32, i32* @tcp_connect.tries
%130 = add nsw i32 %129, 1
store i32 %130, i32* @tcp_connect.tries
%131 = icmp sgt i32 %130, 10
br i1 %131, label %132, label %133
132: ; preds = %126
store i32 -1, i32* %4
br label %143
133: ; preds = %126
%134 = load i8*, i8** %5
%135 = load i32, i32* %6
%136 = load i32, i32* %7
%137 = call i32 @tcp_connect(i8* %134, i32 %135, i32 %136)
store i32 %137, i32* %4
br label %143
138: ; preds = %122
%139 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%140 = call i32 @exit(i32 4)
unreachable
141: ; preds = %110
store i32 0, i32* @tcp_connect.tries
%142 = load i32, i32* %8
store i32 %142, i32* %4
br label %143
143: ; preds = %141, %133, %132
%144 = load i32, i32* %4
ret i32 %144
}

LLVM IR (-O2):
%struct.hostent = type { i32, i64 }
%struct.sockaddr_in = type { i8*, i32, i8* }
%struct.sockaddr = type { i32 }
@tcp_connect.h = internal unnamed_addr global %struct.hostent* null
@tcp_connect.s = internal global %struct.sockaddr_in zeroinitializer
@tcp_connect.save_prog = internal unnamed_addr global i32 0
@tcp_connect.save_host = internal unnamed_addr global i8* null
@tcp_connect.tries = internal unnamed_addr global i32 0
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@tcp_connect.port = internal unnamed_addr global i16 0
@.str.1 = [23 x i8] c"lib TCP: No port found\00"
@errno = global i64 0
@ECONNRESET = global i64 0
@ECONNREFUSED = global i64 0
@EAGAIN = global i64 0
@.str.2 = [8 x i8] c"connect\00"
define i32 @tcp_connect(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = load i8*, i8** @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i8* %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %3
%14 = and i32 %2, 4
%15 = icmp eq i32 %14, 0
br i1 %15, label %44, label %16
16: ; preds = %13
%17 = bitcast %struct.sockaddr_in* %4 to i8*
%18 = load i16, i16* @tcp_connect.port
%19 = icmp eq i16 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = tail call i32 (...) @getpid()
%22 = trunc i32 %21 to i16
%23 = shl i16 %22, 4
%24 = icmp ult i16 %23, 1024
%25 = add nuw nsw i16 %23, 1024
%26 = select i1 %24, i16 %25, i16 %23
br label %27
27: ; preds = %20, %16
%28 = phi i16 [ %18, %16 ], [ %26, %20 ]
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %32
32: ; preds = %41, %27
%33 = phi i16 [ %42, %41 ], [ %28, %27 ]
%34 = add i16 %33, 1
store i16 %34, i16* @tcp_connect.port
%35 = call i32 @bzero(i8* %17, i32 24)
%36 = load i8*, i8** @AF_INET
store i8* %36, i8** %29
%37 = zext i16 %34 to i32
%38 = call i8* @htons(i32 %37)
store i8* %38, i8** %30
%39 = call i32 @bind(i32 %8, %struct.sockaddr* %31, i32 24)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = load i16, i16* @tcp_connect.port
br label %32
43: ; preds = %32
br label %44
44: ; preds = %43, %13
%45 = call i32 @sock_optimize(i32 %8, i32 %2)
%46 = load %struct.hostent*, %struct.hostent** @tcp_connect.h
%47 = icmp ne %struct.hostent* %46, null
%48 = load i8*, i8** @tcp_connect.save_host
%49 = icmp eq i8* %48, %0
%50 = select i1 %47, i1 %49, i1 false
%51 = load i32, i32* @tcp_connect.save_prog
%52 = icmp eq i32 %51, %1
%53 = select i1 %50, i1 %52, i1 false
br i1 %53, label %81, label %54
54: ; preds = %44
store i8* %0, i8** @tcp_connect.save_host
store i32 %1, i32* @tcp_connect.save_prog
%55 = call %struct.hostent* @gethostbyname(i8* %0)
store %struct.hostent* %55, %struct.hostent** @tcp_connect.h
%56 = icmp eq %struct.hostent* %55, null
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @perror(i8* %0)
%59 = call i32 @exit(i32 2)
unreachable
60: ; preds = %54
%61 = call i32 @bzero(i8* bitcast (%struct.sockaddr_in* @tcp_connect.s to i8*), i32 24)
%62 = load i8*, i8** @AF_INET
store i8* %62, i8** getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @tcp_connect.s, i64 0, i32 2)
%63 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i64 0, i32 1
%64 = load i64, i64* %63
%65 = inttoptr i64 %64 to i8*
%66 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i64 0, i32 0
%67 = load i32, i32* %66
%68 = icmp sgt i32 %1, 0
br i1 %68, label %69, label %76
69: ; preds = %60
%70 = load i32, i32* @IPPROTO_TCP
%71 = call i32 @pmap_getport(%struct.sockaddr_in* @tcp_connect.s, i32 %1, i32 1, i32 %70)
%72 = icmp eq i32 %71, 0
br i1 %72, label %73, label %78
73: ; preds = %69
%74 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%75 = call i32 @exit(i32 3)
unreachable
76: ; preds = %60
%77 = sub nsw i32 0, %1
br label %78
78: ; preds = %69, %76
%79 = phi i32 [ %77, %76 ], [ %71, %69 ]
%80 = call i8* @htons(i32 %79)
store i8* %80, i8** getelementptr inbounds (%struct.sockaddr_in, %struct.sockaddr_in* @tcp_connect.s, i64 0, i32 0)
br label %81
81: ; preds = %78, %44
%82 = call i64 @connect(i32 %8, %struct.sockaddr* bitcast (%struct.sockaddr_in* @tcp_connect.s to %struct.sockaddr*), i32 24)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %104
84: ; preds = %81
%85 = load i64, i64* @errno
%86 = load i64, i64* @ECONNRESET
%87 = icmp eq i64 %85, %86
%88 = load i64, i64* @ECONNREFUSED
%89 = icmp eq i64 %85, %88
%90 = select i1 %87, i1 true, i1 %89
%91 = load i64, i64* @EAGAIN
%92 = icmp eq i64 %85, %91
%93 = select i1 %90, i1 true, i1 %92
br i1 %93, label %94, label %101
94: ; preds = %84
%95 = call i32 @close(i32 %8)
%96 = load i32, i32* @tcp_connect.tries
%97 = add nsw i32 %96, 1
store i32 %97, i32* @tcp_connect.tries
%98 = icmp sgt i32 %96, 9
br i1 %98, label %105, label %99
99: ; preds = %94
%100 = call i32 @tcp_connect(i8* %0, i32 %1, i32 %2)
br label %105
101: ; preds = %84
%102 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%103 = call i32 @exit(i32 4)
unreachable
104: ; preds = %81
store i32 0, i32* @tcp_connect.tries
br label %105
105: ; preds = %94, %104, %99
%106 = phi i32 [ %100, %99 ], [ %8, %104 ], [ -1, %94 ]
ret i32 %106
}