#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ Fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int sockfd, n;
  char recvline[4096 + 1];
  struct sockaddr_in servaddr;
  if (argc != 2) {
    printf("usage: client <IP>\n");
    return -1;
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(13);
  inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
  connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  while ((n = read(sockfd, recvline, 4096)) > 0) {
    recvline[n] = 0;
    Fputs(recvline, stdout);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bb <main+0x3b>
lea    0xe56(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <main+0x105>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x1030(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1028(%rbp)
mov    $0xd,%edi
call   1040 <htons@plt>
mov    %eax,-0x102c(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x1030(%rbp),%rdx
call   1070 <inet_pton@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    127e <main+0xfe>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rdi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1030 <Fputs@plt>
jmp    1238 <main+0xb8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1020,%rsp
cmp    $0x2,%edi
jne    1257 <main+0xd7>
mov    %rsi,%rbx
mov    0x2e46(%rip),%r12
mov    (%r12),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    $0xd,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%edi
mov    0x8(%rbx),%rsi
mov    %r15,%rdx
call   1070 <inet_pton@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1269 <main+0xe9>
mov    0x2db4(%rip),%r15
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r15),%esi
mov    %rbx,%rdi
call   1030 <Fputs@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1230 <main+0xb0>
jmp    1269 <main+0xe9>
lea    0xda2(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x1020,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"usage: client <IP>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4097 x i8]
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 13)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%28 = call i32 @inet_pton(i32 %23, i8* %26, i32* %27)
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
br label %32
32: ; preds = %37, %14
%33 = load i32, i32* %6
%34 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 4096)
store i32 %35, i32* %7
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [4097 x i8], [4097 x i8]* %8, i64 0, i64 0
%42 = load i32, i32* @stdout
%43 = call i32 @Fputs(i8* %41, i32 %42)
br label %32
44: ; preds = %32
store i32 0, i32* %3
br label %45
45: ; preds = %44, %12
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stdout = global i32 0
@str = [19 x i8] c"usage: client <IP>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4097 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %36
10: ; preds = %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 13)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%23 = call i32 @inet_pton(i32 %19, i8* %21, i32* %22)
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @connect(i32 %13, %struct.sockaddr* %24, i32 12)
%26 = call i32 @read(i32 %13, i8* %5, i32 4096)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %10, %28
%29 = phi i32 [ %34, %28 ], [ %26, %10 ]
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = load i32, i32* @stdout
%33 = call i32 @Fputs(i8* %5, i32 %32)
%34 = call i32 @read(i32 %13, i8* %5, i32 4096)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %28, label %36
36: ; preds = %28, %10, %8
%37 = phi i32 [ -1, %8 ], [ 0, %10 ], [ 0, %28 ]
ret i32 %37
}