#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 /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd;
  struct sockaddr_in serv_addr;
  int str_len, read_len, i;
  char msg[128];
  if (argc != 3) {
    fprintf(stderr, "Usage: ./%s <ip> <port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket() failed");
    exit(EXIT_FAILURE);
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
  serv_addr.sin_port = htons(atoi(argv[2]));
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    fprintf(stderr, "connect() failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  i = 0;
  while ((read_len = read(sockfd, &msg[i++], 1))) {
    if (read_len == -1) {
      perror("read() failed");
      close(sockfd);
      exit(EXIT_FAILURE);
    }
    str_len += read_len;
  }
  msg[str_len] = '\0';
  printf("[recv %d bytes] %s\n", str_len, msg);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     121d <main+0x4d>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    125c <main+0x8c>
lea    0xdd0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f9 <main+0x129>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xb4(%rbp)
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0xb4(%rbp),%edi
mov    %rax,%rdx
lea    0xd43(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cltq
lea    -0xb0(%rbp),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     136a <main+0x19a>
cmpl   $0xffffffff,-0x28(%rbp)
jne    135c <main+0x18c>
lea    0xcfe(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2c6b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1300 <main+0x130>
movslq -0x24(%rbp),%rax
movb   $0x0,-0xb0(%rbp,%rax,1)
mov    -0x24(%rbp),%esi
lea    -0xb0(%rbp),%rdx
lea    0xcc6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    12e6 <main+0x116>
mov    0x2df6(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12fb <main+0x12b>
mov    %eax,%r12d
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    0x10(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     1309 <main+0x139>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %eax,%eax
je     1291 <main+0xc1>
cmp    $0xffffffff,%eax
je     12c4 <main+0xf4>
add    %eax,%ebp
add    $0x1,%rbx
jmp    1270 <main+0xa0>
movslq %ebp,%rsi
movb   $0x0,0x10(%rsp,%rsi,1)
lea    0xdad(%rip),%rdi
lea    0x10(%rsp),%rdx
call   1040 <printf@plt>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd74(%rip),%rdi
call   10a0 <perror@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd07(%rip),%rsi
jmp    132c <main+0x15c>
lea    0xd17(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d8 <main+0x108>
mov    0x2cc8(%rip),%rax
mov    (%rax),%ebx
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd02(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
jmp    12d8 <main+0x108>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Usage: ./%s <ip> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@.str.2 = [22 x i8] c"connect() failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [14 x i8] c"read() failed\00"
@.str.4 = [20 x i8] c"[recv %d bytes] %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %22
%32 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %33, i32* %34
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @inet_addr(i8* %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %6
%48 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%49 = call i32 @connect(i32 %47, %struct.sockaddr* %48, i32 12)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %58
51: ; preds = %31
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %31
store i32 0, i32* %10
br label %59
59: ; preds = %76, %58
%60 = load i32, i32* %6
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 %63
%65 = call i32 @read(i32 %60, i8* %64, i32 1)
store i32 %65, i32* %9
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %80
67: ; preds = %59
%68 = load i32, i32* %9
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %76
70: ; preds = %67
%71 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %67
%77 = load i32, i32* %9
%78 = load i32, i32* %8
%79 = add nsw i32 %78, %77
store i32 %79, i32* %8
br label %59
80: ; preds = %59
%81 = load i32, i32* %8
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 %82
store i8 0, i8* %83
%84 = load i32, i32* %8
%85 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%86 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %84, i8* %85)
%87 = load i32, i32* %6
%88 = call i32 @close(i32 %87)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Usage: ./%s <ip> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@.str.2 = [22 x i8] c"connect() failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [14 x i8] c"read() failed\00"
@.str.4 = [20 x i8] c"[recv %d bytes] %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [128 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @inet_addr(i8* %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i32 @connect(i32 %17, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %23
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %23, %56
%47 = phi i64 [ %57, %56 ], [ 0, %23 ]
%48 = phi i32 [ %58, %56 ], [ undef, %23 ]
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %47
%50 = call i32 @read(i32 %17, i8* %49, i32 1)
switch i32 %50, label %56 [
i32 0, label %59
i32 -1, label %51
]
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @close(i32 %17)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %46
%57 = add nuw i64 %47, 1
%58 = add nsw i32 %50, %48
br label %46
59: ; preds = %46
%60 = sext i32 %48 to i64
%61 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %60
store i8 0, i8* %61
%62 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %48, i8* %6)
%63 = call i32 @close(i32 %17)
ret i32 0
}