#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 */

/* Variables and functions */
int atoi(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ sleep(double);
int strlen(char *);
int tcpsocket_connect(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char const *argv[]) {
  if (argc < 2) {
    printf("Usage: ./client <port>\n");
    exit(1);
  }
  int port = atoi(argv[1]);
  int sfd = tcpsocket_connect(port);
  if (sfd < 0) {
    printf("tcpclient_connect()\n");
    exit(2);
  }
  char buf[1024];
  sleep(0.5);
  printf("client started\n");
  while (1) {
    scanf("%s", buf);
    if (atoi(buf) == -1) {
      write(sfd, "-1", 2);
      close(sfd);
      printf("client exiting...\n");
      exit(1);
    }
    write(sfd, buf, strlen(buf));
    read(sfd, buf, 1024);
    printf("%s\n", buf);
    memset(buf, 0, 1024);
  }
  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    11eb <main+0x3b>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <tcpsocket_connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1228 <main+0x78>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
movsd  0xdd0(%rip),%xmm0
call   10d0 <sleep@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rsi
lea    0xdf4(%rip),%rdi
call   10a0 <scanf@plt>
lea    -0x420(%rbp),%rdi
call   10b0 <atoi@plt>
cmp    $0xffffffff,%eax
jne    129f <main+0xef>
mov    -0x18(%rbp),%edi
lea    0xdd3(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
lea    -0x420(%rbp),%rsi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
jmp    1243 <main+0x93>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
cmp    $0x1,%edi
jg     11cc <main+0x1c>
lea    0xe7c(%rip),%rdi
jmp    12b3 <main+0x103>
mov    0x8(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1090 <tcpsocket_connect@plt>
test   %eax,%eax
jns    11f6 <main+0x46>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
mov    %eax,%ebp
movsd  0xe00(%rip),%xmm0
call   10d0 <sleep@plt>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf0(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    %rbx,%rdi
call   10b0 <atoi@plt>
cmp    $0xffffffff,%eax
je     1292 <main+0xe2>
mov    %rsp,%rbx
lea    0xdce(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    %rbx,%rdi
call   10b0 <atoi@plt>
cmp    $0xffffffff,%eax
jne    1240 <main+0x90>
lea    0xd72(%rip),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xd6a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Usage: ./client <port>\0A\00"
@.str.1 = [21 x i8] c"tcpclient_connect()\0A\00"
@.str.2 = [16 x i8] c"client started\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"-1\00"
@.str.5 = [19 x i8] c"client exiting...\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i32 @tcpsocket_connect(i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %14
%27 = call i32 @sleep(double 5.000000e-01)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %26, %42
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%33 = call i32 @atoi(i8* %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %42
35: ; preds = %29
%36 = load i32, i32* %7
%37 = call i32 @write(i32 %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
%43 = load i32, i32* %7
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
%48 = load i32, i32* %7
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%50 = call i32 @read(i32 %48, i8* %49, i32 1024)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %51)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%54 = call i32 @memset(i8* %53, i32 0, i32 1024)
br label %29
}

LLVM IR (-O2):
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"-1\00"
@str = [15 x i8] c"client started\00"
@str.7 = [18 x i8] c"client exiting...\00"
@str.8 = [20 x i8] c"tcpclient_connect()\00"
@str.9 = [23 x i8] c"Usage: ./client <port>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call i32 @tcpsocket_connect(i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %8
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%19 = tail call i32 @sleep(double 5.000000e-01)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%22 = call i32 @atoi(i8* %18)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %29, %17
%25 = call i32 @write(i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%26 = call i32 @close(i32 %12)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17, %29
%30 = call i32 @strlen(i8* %18)
%31 = call i32 @write(i32 %12, i8* %18, i32 %30)
%32 = call i32 @read(i32 %12, i8* %18, i32 1024)
%33 = call i32 @puts(i8* %18)
%34 = call i32 @memset(i8* %18, i32 0, i32 1024)
%35 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%36 = call i32 @atoi(i8* %18)
%37 = icmp eq i32 %36, -1
br i1 %37, label %24, label %29
}