#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__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ seraddr;
typedef int /*<<< orphan*/ localaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ echo_client(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int clientsocket[5];
  int i;
  for (i = 0; i < 5; ++i) {
    if ((clientsocket[i] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
      do {
        perror("socket");
        exit(EXIT_FAILURE);
      } while (0);
    }
    struct sockaddr_in seraddr;
    memset(&seraddr, 0, sizeof(seraddr));
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(3000);
    seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if (connect(clientsocket[i], (struct sockaddr *)&seraddr, sizeof(seraddr)) <
        0) {
      do {
        perror("connect");
        exit(EXIT_FAILURE);
      } while (0);
    }
    struct sockaddr_in localaddr;
    socklen_t addrlen = sizeof(localaddr);
    getsockname(clientsocket[i], (struct sockaddr *)&localaddr, &addrlen);
    printf("localhost ip=%s,port=%d\n", inet_ntoa(localaddr.sin_addr),
           ntohs(localaddr.sin_port));
  }
  echo_client(clientsocket[0]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x5,-0x24(%rbp)
jge    12f7 <main+0x137>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
movslq -0x24(%rbp),%rcx
mov    %eax,-0x20(%rbp,%rcx,4)
cmp    $0x0,%eax
jge    1230 <main+0x70>
jmp    1216 <main+0x56>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0xbb8,%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
lea    0xda7(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
movslq -0x24(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    12a7 <main+0xe7>
jmp    128d <main+0xcd>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movl   $0xc,-0x44(%rbp)
movslq -0x24(%rbp),%rax
mov    -0x20(%rbp,%rax,4),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   10b0 <getsockname@plt>
mov    -0x3c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    0xd35(%rip),%rdi
call   1060 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d6 <main+0x16>
mov    -0x20(%rbp),%edi
call   1040 <echo_client@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
xor    %r14d,%r14d
lea    0x10(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xe32(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,0x30(%rsp,%r14,4)
test   %eax,%eax
js     12cc <main+0x10c>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0xbb8,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xdb7(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     12d5 <main+0x115>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10b0 <getsockname@plt>
mov    0x24(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x20(%rsp),%edi
call   1090 <ntohs@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <printf@plt>
add    $0x1,%r14
cmp    $0x5,%r14
jne    11f0 <main+0x30>
mov    0x30(%rsp),%edi
call   1040 <echo_client@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2d(%rip),%rdi
jmp    12dc <main+0x11c>
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [25 x i8] c"localhost ip=%s,port=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i32]
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %7
7: ; preds = %62, %0
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 5
br i1 %9, label %10, label %65
10: ; preds = %7
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* @IPPROTO_TCP
%14 = call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 %16
store i32 %14, i32* %17
%18 = icmp slt i32 %14, 0
br i1 %18, label %19, label %25
19: ; preds = %10
br label %20
20: ; preds = %19
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; No predecessors!
br label %25
25: ; preds = %24, %10
%26 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 3000)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %29, i32* %30
%31 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %25
br label %42
42: ; preds = %41
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; No predecessors!
br label %47
47: ; preds = %46, %25
store i32 12, i32* %6
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 %49
%51 = load i32, i32* %50
%52 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%53 = call i32 @getsockname(i32 %51, %struct.sockaddr* %52, i32* %6)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i8* @inet_ntoa(i32 %56)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @ntohs(i32 %59)
%61 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %57, i32 %60)
br label %62
62: ; preds = %47
%63 = load i32, i32* %3
%64 = add nsw i32 %63, 1
store i32 %64, i32* %3
br label %7
65: ; preds = %7
%66 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 0
%67 = load i32, i32* %66
%68 = call i32 @echo_client(i32 %67)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [25 x i8] c"localhost ip=%s,port=%d\0A\00"
define i32 @main() {
%1 = alloca [5 x i32]
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast [5 x i32]* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%10 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = bitcast i32* %4 to i8*
%13 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %16
16: ; preds = %0, %39
%17 = phi i64 [ 0, %0 ], [ %46, %39 ]
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = load i32, i32* @IPPROTO_TCP
%21 = call i32 @socket(i32 %18, i32 %19, i32 %20)
%22 = getelementptr inbounds [5 x i32], [5 x i32]* %1, i64 0, i64 %17
store i32 %21, i32* %22
%23 = icmp slt i32 %21, 0
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %16
%29 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%30 = load i32, i32* @AF_INET
store i32 %30, i32* %7
%31 = call i32 @htons(i32 3000)
store i32 %31, i32* %8
%32 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 %32, i32* %9
%33 = call i64 @connect(i32 %21, %struct.sockaddr* %10, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %28
store i32 12, i32* %4
%40 = call i32 @getsockname(i32 %21, %struct.sockaddr* %13, i32* %4)
%41 = load i32, i32* %14
%42 = call i8* @inet_ntoa(i32 %41)
%43 = load i32, i32* %15
%44 = call i32 @ntohs(i32 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %42, i32 %44)
%46 = add nuw nsw i64 %17, 1
%47 = icmp eq i64 %46, 5
br i1 %47, label %48, label %16
48: ; preds = %39
%49 = getelementptr inbounds [5 x i32], [5 x i32]* %1, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i32 @echo_client(i32 %50)
ret i32 0
}