#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 */
typedef int /*<<< orphan*/ u_short;
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 /*<<< orphan*/ s_addr;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char *);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(double);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("%s\n", "Usage: ./client <port>");
    exit(1);
  };
  struct sockaddr_in s_addr;
  char buf[1024];
  int sfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sfd == -1) {
    printf("%s\n", "socket() error");
    exit(1);
  }
  memset((char *)&s_addr, 0, sizeof(s_addr));
  s_addr.sin_family = AF_INET;
  s_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  s_addr.sin_port = htons((u_short)atoi(argv[1]));
  if ((connect(sfd, (struct sockaddr *)&s_addr, sizeof(s_addr))) < 0) {
    printf("%s\n", "connect() error");
    exit(1);
  };
  fd_set rfds;
  while (1) {
    FD_ZERO(&rfds);
    FD_SET(sfd, &rfds);
    if ((select(sfd + 1, &rfds, NULL, NULL, NULL)) < 0) {
      sleep(0.2);
      continue;
    }
    if (FD_ISSET(sfd, &rfds)) {
      read(sfd, buf, 1024);
      printf("%s\n", buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1220 <main+0x40>
lea    0xdfe(%rip),%rdi
lea    0xdfb(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    1269 <main+0x89>
lea    0xdb5(%rip),%rdi
lea    0xdc9(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xda6(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    12e6 <main+0x106>
lea    0xd38(%rip),%rdi
lea    0xd65(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12eb <main+0x10b>
lea    -0x428(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x424(%rbp),%edi
lea    -0x428(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x424(%rbp),%edi
add    $0x1,%edi
lea    -0x428(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
cmp    $0x0,%rax
jge    1345 <main+0x165>
movsd  0xcc5(%rip),%xmm0
call   10f0 <sleep@plt>
jmp    12eb <main+0x10b>
mov    -0x424(%rbp),%edi
lea    -0x428(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     138b <main+0x1ab>
mov    -0x424(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    -0x420(%rbp),%rsi
lea    0xc82(%rip),%rdi
call   1050 <printf@plt>
jmp    12eb <main+0x10b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
cmp    $0x1,%edi
jle    12f1 <main+0x111>
mov    %rsi,%rbp
mov    0x2de6(%rip),%r15
mov    (%r15),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
je     1301 <main+0x121>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
lea    0xdf2(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     1311 <main+0x131>
mov    %ebx,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%rbp
lea    0x10(%rsp),%r15
lea    0xd7c(%rip),%r12
jmp    129d <main+0xbd>
xchg   %ax,%ax
movsd  0xd68(%rip),%xmm0
call   10f0 <sleep@plt>
mov    %rbp,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10e0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %rax,%rax
js     1290 <main+0xb0>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     129d <main+0xbd>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
jmp    129d <main+0xbd>
lea    0xd10(%rip),%rdi
lea    0xd0d(%rip),%rsi
jmp    131f <main+0x13f>
lea    0xd00(%rip),%rdi
lea    0xd14(%rip),%rsi
jmp    131f <main+0x13f>
lea    0xcf0(%rip),%rdi
lea    0xd1d(%rip),%rsi
call   1050 <printf@plt>
mov    $0x1,%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 }
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [23 x i8] c"Usage: ./client <port>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [15 x i8] c"socket() error\00"
@.str.3 = [10 x i8] c"127.0.0.1\00"
@.str.4 = [16 x i8] c"connect() error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = bitcast %struct.sockaddr_in* %6 to i8*
%26 = call i32 @memset(i8* %25, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i64 @atoi(i8* %34)
%36 = trunc i64 %35 to i32
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %8
%40 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%41 = call i64 @connect(i32 %39, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %24
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %24
br label %47
47: ; preds = %46, %55, %67
%48 = call i32 @FD_ZERO(i32* %9)
%49 = load i32, i32* %8
%50 = call i32 @FD_SET(i32 %49, i32* %9)
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
%53 = call i64 @select(i32 %52, i32* %9, i32* null, i32* null, i32* null)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %47
%56 = call i32 @sleep(double 2.000000e-01)
br label %47
57: ; preds = %47
%58 = load i32, i32* %8
%59 = call i64 @FD_ISSET(i32 %58, i32* %9)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %57
%62 = load i32, i32* %8
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%64 = call i32 @read(i32 %62, i8* %63, i32 1024)
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%66 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %61, %57
br label %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [23 x i8] c"Usage: ./client <port>\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [15 x i8] c"socket() error\00"
@.str.3 = [10 x i8] c"127.0.0.1\00"
@.str.4 = [16 x i8] c"connect() error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = call i32 @memset(i8* %11, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @atoi(i8* %27)
%29 = trunc i64 %28 to i32
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %30, i32* %31
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i64 @connect(i32 %15, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %20
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
%39 = bitcast i32* %5 to i8*
%40 = add nuw nsw i32 %15, 1
br label %41
41: ; preds = %51, %38
%42 = call i32 @FD_ZERO(i32* %5)
%43 = call i32 @FD_SET(i32 %15, i32* %5)
%44 = call i64 @select(i32 %40, i32* %5, i32* null, i32* null, i32* null)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @sleep(double 2.000000e-01)
br label %51
48: ; preds = %41
%49 = call i64 @FD_ISSET(i32 %15, i32* %5)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48, %52, %46
br label %41
52: ; preds = %48
%53 = call i32 @read(i32 %15, i8* %12, i32 1024)
%54 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %51
}