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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
scalar_t__ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: %s <string>\n", argv[0]);
    exit(1);
  }
  char *buffer = argv[1];
  int buffer_len = strlen(buffer);
  if (buffer_len > 1024) {
    perror("String is too long");
    exit(1);
  }
  int sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("failed to create socket");
    exit(1);
  }
  struct sockaddr_in adress;
  adress.sin_family = AF_INET;
  adress.sin_port = htons(19002);
  adress.sin_addr.s_addr = INADDR_ANY;
  if (connect(sd, (struct sockaddr *)&adress, sizeof(adress)) < 0) {
    perror("Failed to connect");
    exit(1);
  }
  if (write(sd, buffer, buffer_len) < 0) {
    perror("failed to write");
    close(sd);
    exit(1);
  }
  int read_n = read(sd, buffer, buffer_len);
  if (read_n == -1) {
    perror("failed to read");
    close(sd);
    exit(1);
  }
  if (read_n != buffer_len) {
    perror("Read a string of different size");
  }
  printf("got = %s\n", buffer);
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11dd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x400,-0x1c(%rbp)
jle    1218 <main+0x78>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1254 <main+0xb4>
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x4a3a,%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12aa <main+0x10a>
lea    0xda4(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12e1 <main+0x141>
lea    0xd87(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    131b <main+0x17b>
lea    0xd5d(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1333 <main+0x193>
lea    0xd42(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%rsi
lea    0xd52(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jle    1287 <main+0xe7>
mov    0x8(%rsi),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0x401,%eax
jge    12a0 <main+0x100>
mov    %eax,%ebx
mov    0x2e15(%rip),%r15
mov    (%r15),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12a9 <main+0x109>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    $0x4a3a,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     12b2 <main+0x112>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12c8 <main+0x128>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     12d1 <main+0x131>
cmp    %ebx,%eax
jne    1279 <main+0xd9>
lea    0xe33(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
call   1090 <perror@plt>
jmp    1256 <main+0xb6>
mov    (%rsi),%rsi
lea    0xd6f(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd6d(%rip),%rdi
jmp    12b9 <main+0x119>
lea    0xd77(%rip),%rdi
jmp    12b9 <main+0x119>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd82(%rip),%rdi
jmp    12d8 <main+0x138>
lea    0xd89(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Usage: %s <string>\0A\00"
@.str.1 = [19 x i8] c"String is too long\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [24 x i8] c"failed to create socket\00"
@INADDR_ANY = global i32 0
@.str.3 = [18 x i8] c"Failed to connect\00"
@.str.4 = [16 x i8] c"failed to write\00"
@.str.5 = [15 x i8] c"failed to read\00"
@.str.6 = [32 x i8] c"Read a string of different size\00"
@.str.7 = [10 x i8] c"got = %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @strlen(i8* %11)
%13 = icmp sgt i32 %12, 1024
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = bitcast %struct.sockaddr_in* %3 to i8*
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = tail call i32 @htons(i32 19002)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %31, i32* %32
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @connect(i32 %20, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %25
%37 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %25
%40 = call i64 @write(i32 %20, i8* %11, i32 %12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @close(i32 %20)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = call i32 @read(i32 %20, i8* %11, i32 %12)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @close(i32 %20)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %46
%54 = icmp eq i32 %47, %12
br i1 %54, label %57, label %55
55: ; preds = %53
%56 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
br label %57
57: ; preds = %55, %53
%58 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %11)
%59 = call i32 @close(i32 %20)
ret i32 0
}