#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*/ servaddr;
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_cli(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ 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 sock[5];
  int i;
  for (i = 0; i < 5; i++) {
    if ((sock[i] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
      do {
        perror("socket");
        exit(EXIT_FAILURE);
      } while (0);
    }
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(5188);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if (connect(sock[i], (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
      do {
        perror("connect");
        exit(EXIT_FAILURE);
      } while (0);
    }
    struct sockaddr_in localaddr;
    socklen_t addrlen = sizeof(localaddr);
    if ((getsockname(sock[i], (struct sockaddr *)&localaddr, &addrlen)) < 0) {
      do {
        perror("getsockname");
        exit(EXIT_FAILURE);
      } while (0);
    }
    printf("ip: %s, port: %d\n", inet_ntoa(localaddr.sin_addr),
           ntohs(localaddr.sin_port));
  }
  echo_cli(sock[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    1320 <main+0x160>
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   1090 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1444,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
lea    0xda7(%rip),%rdi
call   1070 <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   1090 <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   10a0 <getsockname@plt>
cmp    $0x0,%rax
jge    12ec <main+0x12c>
jmp    12d2 <main+0x112>
lea    0xd40(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x3c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    0xd18(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d6 <main+0x16>
mov    -0x20(%rbp),%edi
call   10b0 <echo_cli@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    0xe3e(%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     12d1 <main+0x111>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x1444,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xdb7(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     12da <main+0x11a>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10a0 <getsockname@plt>
test   %rax,%rax
js     12e3 <main+0x123>
mov    0x24(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x20(%rsp),%edi
call   1080 <ntohs@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
add    $0x1,%r14
cmp    $0x5,%r14
jne    11f0 <main+0x30>
mov    0x30(%rsp),%edi
call   10b0 <echo_cli@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd28(%rip),%rdi
jmp    12ea <main+0x12a>
lea    0xd30(%rip),%rdi
jmp    12ea <main+0x12a>
lea    0xd2f(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cca(%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 = [12 x i8] c"getsockname\00"
@.str.4 = [18 x i8] c"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 = %70, %0
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 5
br i1 %9, label %10, label %73
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 5188)
%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 i64 @getsockname(i32 %51, %struct.sockaddr* %52, i32* %6)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %47
br label %56
56: ; preds = %55
%57 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; No predecessors!
br label %61
61: ; preds = %60, %47
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call i8* @inet_ntoa(i32 %64)
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i32 @ntohs(i32 %67)
%69 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %65, i32 %68)
br label %70
70: ; preds = %61
%71 = load i32, i32* %3
%72 = add nsw i32 %71, 1
store i32 %72, i32* %3
br label %7
73: ; preds = %7
%74 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 0
%75 = load i32, i32* %74
%76 = call i32 @echo_cli(i32 %75)
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 = [12 x i8] c"getsockname\00"
@.str.4 = [18 x i8] c"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, %46
%17 = phi i64 [ 0, %0 ], [ %52, %46 ]
%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 5188)
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 i64 @getsockname(i32 %21, %struct.sockaddr* %13, i32* %4)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %39
%47 = load i32, i32* %14
%48 = call i8* @inet_ntoa(i32 %47)
%49 = load i32, i32* %15
%50 = call i32 @ntohs(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %48, i32 %50)
%52 = add nuw nsw i64 %17, 1
%53 = icmp eq i64 %52, 5
br i1 %53, label %54, label %16
54: ; preds = %46
%55 = getelementptr inbounds [5 x i32], [5 x i32]* %1, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @echo_cli(i32 %56)
ret i32 0
}