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

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
char *displayAddr(struct sockaddr *);
int /*<<< orphan*/ free(struct sockaddr *);
struct sockaddr *makeAddr(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *, ...);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int main(int argc, char **argv) {
  if (argc != 3 && argc != 5) {
    printf("sect <remote ip> <remote port> [<local ip> <local port>]\n");
    return 0;
  }
  struct sockaddr *it = makeAddr(argv[1], argv[2]);
  if (!it) {
    perror("creating remote address fails");
    return -1;
  }
  struct sockaddr *me = 0;
  if (argc == 5) {
    me = makeAddr(argv[3], argv[4]);
    if (!me) {
      perror("creating local address fails");
      return -1;
    }
  }
  const char *title = "TCP echo client";
  char buf[128];
  int buf_len = sizeof(buf) - 1;
  int connect_count = 0;
  while (1) {
    int s = socket(PF_INET, SOCK_STREAM, 0);
    if (s < 0) {
      perror("creating socket fails");
      break;
    }
    if (me) {
      int optval = 1;
      int opt =
          setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
      int bound = bind(s, me, sizeof(struct sockaddr_in));
      if (bound < 0) {
        perror("bind fails");
        break;
      }
      printf("bound on %s\n", displayAddr(me));
    }
    int connected = connect(s, it, sizeof(struct sockaddr_in));
    sprintf(buf, "connection on %s ", displayAddr(it));
    if (connected < 0) {
      strncat(buf, "fails", sizeof(buf));
      perror(buf);
      break;
    }
    strncat(buf, "succeeds", sizeof(buf));
    printf("%s\n", buf);
    ++connect_count;
    int message_count = 0;
    while (1) {
      sprintf(buf, "%s: connection #%d, message #%d", title, connect_count,
              ++message_count);
      printf("sending '%s'\n", buf);
      int sent = send(s, buf, strlen(buf), 0);
      if (sent < 0) {
        perror("send fails");
        break;
      }
      printf("sent '%s'\n", buf);
      int received = recv(s, buf, buf_len, 0);
      if (received < 0) {
        perror("receive fails");
        break;
      }
      if (received == 0) {
        printf("peer closed\n");
        break;
      }
      buf[received] = 0;
      printf("received '%s'\n", buf);
      sleep(1);
    }
    close(s);
  }
  free(it);
  if (me)
    free(me);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1247 <main+0x47>
cmpl   $0x5,-0x8(%rbp)
je     1247 <main+0x47>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    160e <main+0x40e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   10c0 <makeAddr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1283 <main+0x83>
lea    0xdc8(%rip),%rdi
call   10e0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    160e <main+0x40e>
movq   $0x0,-0x20(%rbp)
cmpl   $0x5,-0x8(%rbp)
jne    12d6 <main+0xd6>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
call   10c0 <makeAddr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d1 <main+0xd1>
lea    0xd98(%rip),%rdi
call   10e0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    160e <main+0x40e>
jmp    12d6 <main+0xd6>
lea    0xd98(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x7f,-0xb4(%rbp)
movl   $0x0,-0xb8(%rbp)
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0xbc(%rbp)
cmpl   $0x0,-0xbc(%rbp)
jge    1332 <main+0x132>
lea    0xd5d(%rip),%rdi
call   10e0 <perror@plt>
jmp    15ea <main+0x3ea>
cmpq   $0x0,-0x20(%rbp)
je     13c9 <main+0x1c9>
movl   $0x1,-0xc0(%rbp)
mov    -0xbc(%rbp),%edi
mov    0x2c94(%rip),%rax
mov    (%rax),%esi
mov    0x2c6b(%rip),%rax
mov    (%rax),%edx
lea    -0xc0(%rbp),%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    %eax,-0xc4(%rbp)
mov    -0xbc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   10d0 <bind@plt>
mov    %eax,-0xc8(%rbp)
cmpl   $0x0,-0xc8(%rbp)
jge    13af <main+0x1af>
lea    0xcf6(%rip),%rdi
call   10e0 <perror@plt>
jmp    15ea <main+0x3ea>
mov    -0x20(%rbp),%rdi
call   10b0 <displayAddr@plt>
mov    %rax,%rsi
lea    0xce4(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xbc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1100 <connect@plt>
mov    %eax,-0xcc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0x18(%rbp),%rdi
call   10b0 <displayAddr@plt>
mov    -0xe0(%rbp),%rdi
mov    %rax,%rdx
lea    0xca8(%rip),%rsi
mov    $0x0,%al
call   10f0 <sprintf@plt>
cmpl   $0x0,-0xcc(%rbp)
jge    1448 <main+0x248>
lea    -0xb0(%rbp),%rdi
lea    0xcd4(%rip),%rsi
mov    $0x80,%edx
call   1090 <strncat@plt>
lea    -0xb0(%rbp),%rdi
call   10e0 <perror@plt>
jmp    15ea <main+0x3ea>
lea    -0xb0(%rbp),%rdi
lea    0xc6f(%rip),%rsi
mov    $0x80,%edx
call   1090 <strncat@plt>
lea    -0xb0(%rbp),%rsi
lea    0xc41(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xb8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb8(%rbp)
movl   $0x0,-0xd0(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0xb8(%rbp),%ecx
mov    -0xd0(%rbp),%r8d
add    $0x1,%r8d
mov    %r8d,-0xd0(%rbp)
lea    0xc16(%rip),%rsi
mov    $0x0,%al
call   10f0 <sprintf@plt>
lea    -0xb0(%rbp),%rsi
lea    0xc21(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xbc(%rbp),%eax
mov    %eax,-0xec(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xe8(%rbp)
lea    -0xb0(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xec(%rbp),%edi
mov    -0xe8(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %eax,-0xd4(%rbp)
cmpl   $0x0,-0xd4(%rbp)
jge    1534 <main+0x334>
lea    0xbd2(%rip),%rdi
call   10e0 <perror@plt>
jmp    15da <main+0x3da>
lea    -0xb0(%rbp),%rsi
lea    0xbc5(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xbc(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    -0xb4(%rbp),%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0xd8(%rbp)
cmpl   $0x0,-0xd8(%rbp)
jge    1587 <main+0x387>
lea    0xb95(%rip),%rdi
call   10e0 <perror@plt>
jmp    15da <main+0x3da>
cmpl   $0x0,-0xd8(%rbp)
jne    15a7 <main+0x3a7>
lea    0xb85(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    15da <main+0x3da>
movslq -0xd8(%rbp),%rax
movb   $0x0,-0xb0(%rbp,%rax,1)
lea    -0xb0(%rbp),%rsi
lea    0xb69(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   1110 <sleep@plt>
jmp    148e <main+0x28e>
mov    -0xbc(%rbp),%edi
call   10a0 <close@plt>
jmp    12f5 <main+0xf5>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
je     1607 <main+0x407>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x3,%edi
je     1250 <main+0x40>
cmp    $0x5,%ebp
je     1250 <main+0x40>
lea    0xeca(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
call   10d0 <makeAddr@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
jne    1270 <main+0x60>
lea    0xd92(%rip),%rdi
jmp    1291 <main+0x81>
cmp    $0x5,%ebp
jne    129d <main+0x8d>
mov    0x18(%rbx),%rdi
mov    0x20(%rbx),%rsi
call   10d0 <makeAddr@plt>
mov    %rax,%r14
test   %rax,%rax
jne    12a0 <main+0x90>
lea    0xd8d(%rip),%rdi
call   10f0 <perror@plt>
mov    $0xffffffff,%eax
jmp    123e <main+0x2e>
xor    %r14d,%r14d
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
test   %eax,%eax
jns    12ce <main+0xbe>
lea    0xd87(%rip),%rdi
call   10f0 <perror@plt>
jmp    1523 <main+0x313>
mov    %eax,%ebp
lea    0x20(%rsp),%rbx
lea    0xdf1(%rip),%r12
xor    %r15d,%r15d
mov    %r14,0x8(%rsp)
jmp    1327 <main+0x117>
cs nopw 0x0(%rax,%rax,1)
lea    0xdfe(%rip),%rdi
call   1050 <puts@plt>
mov    0x8(%rsp),%r14
mov    %ebp,%edi
call   10b0 <close@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
mov    0x2cc0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12bd <main+0xad>
test   %r14,%r14
je     1388 <main+0x178>
movl   $0x1,0x1c(%rsp)
mov    0x2cad(%rip),%rax
mov    (%rax),%esi
mov    0x2c84(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
lea    0x1c(%rsp),%rcx
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10e0 <bind@plt>
test   %eax,%eax
js     1543 <main+0x333>
mov    %r14,%rdi
call   10c0 <displayAddr@plt>
lea    0xcee(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %ebp,%edi
mov    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0x4,%edx
call   1110 <connect@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   10c0 <displayAddr@plt>
mov    %rbx,%rdi
lea    0xcc8(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1100 <sprintf@plt>
test   %r13d,%r13d
js     14fd <main+0x2ed>
mov    %rbx,%rdi
lea    0xcbd(%rip),%rsi
mov    $0x80,%edx
call   10a0 <strncat@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
add    $0x1,%r15d
mov    %rbx,%rdi
lea    0xca6(%rip),%r14
mov    %r14,%rsi
lea    0xc43(%rip),%rdx
mov    %r15d,%ecx
mov    $0x1,%r8d
xor    %eax,%eax
call   1100 <sprintf@plt>
lea    0xca5(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
test   %eax,%eax
js     14db <main+0x2cb>
mov    $0x2,%r13d
nopl   (%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7f,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
js     14ec <main+0x2dc>
je     12f0 <main+0xe0>
mov    %eax,%eax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xc6c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x1,%edi
call   1120 <sleep@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
lea    0xba0(%rip),%rdx
mov    %r15d,%ecx
mov    %r13d,%r8d
xor    %eax,%eax
call   1100 <sprintf@plt>
lea    0xc05(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
add    $0x1,%r13d
test   %eax,%eax
jns    1440 <main+0x230>
lea    0xbe0(%rip),%rdi
call   10f0 <perror@plt>
jmp    12fc <main+0xec>
lea    0xbe5(%rip),%rdi
call   10f0 <perror@plt>
jmp    12fc <main+0xec>
lea    0xbc3(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%edx
call   10a0 <strncat@plt>
mov    %rbx,%rdi
call   10f0 <perror@plt>
mov    0x8(%rsp),%r14
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
test   %r14,%r14
je     123c <main+0x2c>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    123c <main+0x2c>
lea    0xb17(%rip),%rdi
call   10f0 <perror@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
jmp    1536 <main+0x326>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [58 x i8] c"sect <remote ip> <remote port> [<local ip> <local port>]\0A\00"
@.str.1 = [30 x i8] c"creating remote address fails\00"
@.str.2 = [29 x i8] c"creating local address fails\00"
@.str.3 = [16 x i8] c"TCP echo client\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [22 x i8] c"creating socket fails\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.5 = [11 x i8] c"bind fails\00"
@.str.6 = [13 x i8] c"bound on %s\0A\00"
@.str.7 = [18 x i8] c"connection on %s \00"
@.str.8 = [6 x i8] c"fails\00"
@.str.9 = [9 x i8] c"succeeds\00"
@.str.10 = [4 x i8] c"%s\0A\00"
@.str.11 = [32 x i8] c"%s: connection #%d, message #%d\00"
@.str.12 = [14 x i8] c"sending '%s'\0A\00"
@.str.13 = [11 x i8] c"send fails\00"
@.str.14 = [11 x i8] c"sent '%s'\0A\00"
@.str.15 = [14 x i8] c"receive fails\00"
@.str.16 = [13 x i8] c"peer closed\0A\00"
@.str.17 = [15 x i8] c"received '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr*
%7 = alloca %struct.sockaddr*
%8 = alloca i8*
%9 = alloca [128 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 3
br i1 %21, label %22, label %27
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 5
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %158
27: ; preds = %22, %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call %struct.sockaddr* @makeAddr(i8* %30, i8* %33)
store %struct.sockaddr* %34, %struct.sockaddr** %6
%35 = load %struct.sockaddr*, %struct.sockaddr** %6
%36 = icmp ne %struct.sockaddr* %35, null
br i1 %36, label %39, label %37
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %158
39: ; preds = %27
store %struct.sockaddr* null, %struct.sockaddr** %7
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, 5
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 3
%45 = load i8*, i8** %44
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 4
%48 = load i8*, i8** %47
%49 = call %struct.sockaddr* @makeAddr(i8* %45, i8* %48)
store %struct.sockaddr* %49, %struct.sockaddr** %7
%50 = load %struct.sockaddr*, %struct.sockaddr** %7
%51 = icmp ne %struct.sockaddr* %50, null
br i1 %51, label %54, label %52
52: ; preds = %42
%53 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %158
54: ; preds = %42
br label %55
55: ; preds = %54, %39
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8** %8
store i32 127, i32* %10
store i32 0, i32* %11
br label %56
56: ; preds = %55, %146
%57 = load i32, i32* @PF_INET
%58 = load i32, i32* @SOCK_STREAM
%59 = call i32 @socket(i32 %57, i32 %58, i32 0)
store i32 %59, i32* %12
%60 = load i32, i32* %12
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %149
64: ; preds = %56
%65 = load %struct.sockaddr*, %struct.sockaddr** %7
%66 = icmp ne %struct.sockaddr* %65, null
br i1 %66, label %67, label %83
67: ; preds = %64
store i32 1, i32* %13
%68 = load i32, i32* %12
%69 = load i32, i32* @SOL_SOCKET
%70 = load i32, i32* @SO_REUSEADDR
%71 = call i32 @setsockopt(i32 %68, i32 %69, i32 %70, i32* %13, i32 4)
store i32 %71, i32* %14
%72 = load i32, i32* %12
%73 = load %struct.sockaddr*, %struct.sockaddr** %7
%74 = call i32 @bind(i32 %72, %struct.sockaddr* %73, i32 4)
store i32 %74, i32* %15
%75 = load i32, i32* %15
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %67
%78 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %149
79: ; preds = %67
%80 = load %struct.sockaddr*, %struct.sockaddr** %7
%81 = call i8* @displayAddr(%struct.sockaddr* %80)
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %81)
br label %83
83: ; preds = %79, %64
%84 = load i32, i32* %12
%85 = load %struct.sockaddr*, %struct.sockaddr** %6
%86 = call i32 @connect(i32 %84, %struct.sockaddr* %85, i32 4)
store i32 %86, i32* %16
%87 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%88 = load %struct.sockaddr*, %struct.sockaddr** %6
%89 = call i8* @displayAddr(%struct.sockaddr* %88)
%90 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %87, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %89)
%91 = load i32, i32* %16
%92 = icmp slt i32 %91, 0
br i1 %92, label %93, label %98
93: ; preds = %83
%94 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%95 = call i32 @strncat(i8* %94, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 128)
%96 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%97 = call i32 @perror(i8* %96)
br label %149
98: ; preds = %83
%99 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%100 = call i32 @strncat(i8* %99, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i32 128)
%101 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i8* %101)
%103 = load i32, i32* %11
%104 = add nsw i32 %103, 1
store i32 %104, i32* %11
store i32 0, i32* %17
br label %105
105: ; preds = %98, %139
%106 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%107 = load i8*, i8** %8
%108 = load i32, i32* %11
%109 = load i32, i32* %17
%110 = add nsw i32 %109, 1
store i32 %110, i32* %17
%111 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %106, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i8* %107, i32 %108, i32 %110)
%112 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i8* %112)
%114 = load i32, i32* %12
%115 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%116 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%117 = call i32 @strlen(i8* %116)
%118 = call i32 @send(i32 %114, i8* %115, i32 %117, i32 0)
store i32 %118, i32* %18
%119 = load i32, i32* %18
%120 = icmp slt i32 %119, 0
br i1 %120, label %121, label %123
121: ; preds = %105
%122 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.13, i64 0, i64 0))
br label %146
123: ; preds = %105
%124 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%125 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0), i8* %124)
%126 = load i32, i32* %12
%127 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%128 = load i32, i32* %10
%129 = call i32 @recv(i32 %126, i8* %127, i32 %128, i32 0)
store i32 %129, i32* %19
%130 = load i32, i32* %19
%131 = icmp slt i32 %130, 0
br i1 %131, label %132, label %134
132: ; preds = %123
%133 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.15, i64 0, i64 0))
br label %146
134: ; preds = %123
%135 = load i32, i32* %19
%136 = icmp eq i32 %135, 0
br i1 %136, label %137, label %139
137: ; preds = %134
%138 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.16, i64 0, i64 0))
br label %146
139: ; preds = %134
%140 = load i32, i32* %19
%141 = sext i32 %140 to i64
%142 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 %141
store i8 0, i8* %142
%143 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%144 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.17, i64 0, i64 0), i8* %143)
%145 = call i32 @sleep(i32 1)
br label %105
146: ; preds = %137, %132, %121
%147 = load i32, i32* %12
%148 = call i32 @close(i32 %147)
br label %56
149: ; preds = %93, %77, %62
%150 = load %struct.sockaddr*, %struct.sockaddr** %6
%151 = call i32 @free(%struct.sockaddr* %150)
%152 = load %struct.sockaddr*, %struct.sockaddr** %7
%153 = icmp ne %struct.sockaddr* %152, null
br i1 %153, label %154, label %157
154: ; preds = %149
%155 = load %struct.sockaddr*, %struct.sockaddr** %7
%156 = call i32 @free(%struct.sockaddr* %155)
br label %157
157: ; preds = %154, %149
store i32 0, i32* %3
br label %158
158: ; preds = %157, %52, %37, %25
%159 = load i32, i32* %3
ret i32 %159
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str.1 = [30 x i8] c"creating remote address fails\00"
@.str.2 = [29 x i8] c"creating local address fails\00"
@.str.3 = [16 x i8] c"TCP echo client\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [22 x i8] c"creating socket fails\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.5 = [11 x i8] c"bind fails\00"
@.str.6 = [13 x i8] c"bound on %s\0A\00"
@.str.7 = [18 x i8] c"connection on %s \00"
@.str.8 = [6 x i8] c"fails\00"
@.str.9 = [9 x i8] c"succeeds\00"
@.str.11 = [32 x i8] c"%s: connection #%d, message #%d\00"
@.str.12 = [14 x i8] c"sending '%s'\0A\00"
@.str.13 = [11 x i8] c"send fails\00"
@.str.14 = [11 x i8] c"sent '%s'\0A\00"
@.str.15 = [14 x i8] c"receive fails\00"
@.str.17 = [15 x i8] c"received '%s'\0A\00"
@str = [12 x i8] c"peer closed\00"
@str.18 = [57 x i8] c"sect <remote ip> <remote port> [<local ip> <local port>]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = alloca i32
switch i32 %0, label %5 [
i32 5, label %7
i32 3, label %7
]
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.18, i64 0, i64 0))
br label %107
7: ; preds = %2, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call %struct.sockaddr* @makeAddr(i8* %9, i8* %11)
%13 = icmp eq %struct.sockaddr* %12, null
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %107
16: ; preds = %7
%17 = icmp eq i32 %0, 5
br i1 %17, label %18, label %27
18: ; preds = %16
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8*, i8** %1, i64 4
%22 = load i8*, i8** %21
%23 = tail call %struct.sockaddr* @makeAddr(i8* %20, i8* %22)
%24 = icmp eq %struct.sockaddr* %23, null
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %107
27: ; preds = %18, %16
%28 = phi %struct.sockaddr* [ %23, %18 ], [ null, %16 ]
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%30 = load i32, i32* @PF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %27
%35 = icmp eq %struct.sockaddr* %28, null
%36 = bitcast i32* %4 to i8*
br label %39
37: ; preds = %95, %27
%38 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %101
39: ; preds = %34, %95
%40 = phi i32 [ %32, %34 ], [ %99, %95 ]
%41 = phi i32 [ 0, %34 ], [ %65, %95 ]
br i1 %35, label %54, label %42
42: ; preds = %39
store i32 1, i32* %4
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* @SO_REUSEADDR
%45 = call i32 @setsockopt(i32 %40, i32 %43, i32 %44, i32* %4, i32 4)
%46 = call i32 @bind(i32 %40, %struct.sockaddr* %28, i32 4)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%50 = call i32 @free(%struct.sockaddr* %12)
br label %104
51: ; preds = %42
%52 = call i8* @displayAddr(%struct.sockaddr* %28)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %51, %39
%55 = call i32 @connect(i32 %40, %struct.sockaddr* %12, i32 4)
%56 = call i8* @displayAddr(%struct.sockaddr* %12)
%57 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %29, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %56)
%58 = icmp slt i32 %55, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = call i32 @strncat(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 128)
%61 = call i32 @perror(i8* %29)
br label %101
62: ; preds = %54
%63 = call i32 @strncat(i8* %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i32 128)
%64 = call i32 @puts(i8* %29)
%65 = add nuw nsw i32 %41, 1
%66 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %29, i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %65, i32 1)
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i8* %29)
%68 = call i32 @strlen(i8* %29)
%69 = call i32 @send(i32 %40, i8* %29, i32 %68, i32 0)
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %84, %62
%72 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.13, i64 0, i64 0))
br label %95
73: ; preds = %62, %84
%74 = phi i32 [ %89, %84 ], [ 1, %62 ]
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0), i8* %29)
%76 = call i32 @recv(i32 %40, i8* %29, i32 127, i32 0)
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %73
%79 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.15, i64 0, i64 0))
br label %95
80: ; preds = %73
%81 = icmp eq i32 %76, 0
br i1 %81, label %82, label %84
82: ; preds = %80
%83 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %95
84: ; preds = %80
%85 = zext i32 %76 to i64
%86 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %85
store i8 0, i8* %86
%87 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.17, i64 0, i64 0), i8* %29)
%88 = call i32 @sleep(i32 1)
%89 = add nuw nsw i32 %74, 1
%90 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %29, i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %65, i32 %89)
%91 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i8* %29)
%92 = call i32 @strlen(i8* %29)
%93 = call i32 @send(i32 %40, i8* %29, i32 %92, i32 0)
%94 = icmp slt i32 %93, 0
br i1 %94, label %71, label %73
95: ; preds = %82, %78, %71
%96 = call i32 @close(i32 %40)
%97 = load i32, i32* @PF_INET
%98 = load i32, i32* @SOCK_STREAM
%99 = call i32 @socket(i32 %97, i32 %98, i32 0)
%100 = icmp slt i32 %99, 0
br i1 %100, label %37, label %39
101: ; preds = %37, %59
%102 = call i32 @free(%struct.sockaddr* %12)
%103 = icmp eq %struct.sockaddr* %28, null
br i1 %103, label %106, label %104
104: ; preds = %48, %101
%105 = call i32 @free(%struct.sockaddr* %28)
br label %106
106: ; preds = %104, %101
br label %107
107: ; preds = %14, %106, %25, %5
%108 = phi i32 [ 0, %5 ], [ -1, %14 ], [ 0, %106 ], [ -1, %25 ]
ret i32 %108
}