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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINTR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("%s serverip\n", argv[0]);
    return 0;
  }
  int s = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in si;
  si.sin_family = AF_INET;
  inet_pton(AF_INET, argv[1], &si.sin_addr);
  si.sin_port = htons(8888);
  if (connect(s, (struct sockaddr *)&si, sizeof(si)) < 0) {
    perror("connect");
    return 1;
  }
  char buf[1024];
  for (;;) {
    int n = read(s, buf, sizeof(buf));
    if (n <= 0) {
      if (errno != EINTR)
        break;
      else
        continue;
    }
    write(1, buf, n);
    write(1, "input:", 6);
    n = read(0, buf, sizeof(buf));
    write(s, buf, n);
  }
  close(s);
}


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)
je     11d2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1310 <main+0x180>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
add    $0x4,%rdx
call   1080 <inet_pton@plt>
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1258 <main+0xc8>
lea    0xdc6(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1310 <main+0x180>
jmp    125d <main+0xcd>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jg     12a8 <main+0x118>
mov    0x2d45(%rip),%rax
mov    (%rax),%rax
mov    0x2d33(%rip),%rcx
cmp    (%rcx),%rax
je     12a3 <main+0x113>
jmp    1308 <main+0x178>
jmp    125d <main+0xcd>
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    $0x1,%edi
lea    0xd4a(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
lea    -0x420(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x424(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
jmp    125d <main+0xcd>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1281 <main+0xf1>
mov    0x2e36(%rip),%r14
mov    (%r14),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    (%r14),%edi
mov    %edi,0x8(%rsp)
mov    0x8(%rbx),%rsi
lea    0x4(%rsp),%rdx
call   1080 <inet_pton@plt>
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     12a2 <main+0x112>
lea    0x10(%rsp),%rbx
mov    0x2dc3(%rip),%r15
mov    0x2db4(%rip),%r12
lea    0xdfa(%rip),%r14
jmp    125c <main+0xcc>
nopl   (%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     1220 <main+0x90>
mov    (%r15),%rax
cmp    (%r12),%rax
je     125c <main+0xcc>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1290 <main+0x100>
mov    (%rbx),%rsi
lea    0xd75(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd64(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    1292 <main+0x102>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"%s serverip\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [7 x i8] c"input:\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 [1024 x i8]
%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 ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
br label %64
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
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* %7, i32 0, i32 1
%28 = call i32 @inet_pton(i32 %23, i8* %26, i32* %27)
%29 = call i32 @htons(i32 8888)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%33 = call i64 @connect(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %17
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %64
37: ; preds = %17
br label %38
38: ; preds = %50, %49, %37
%39 = load i32, i32* %6
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 1024)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp sle i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %38
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
br label %61
49: ; preds = %44
br label %38
50: ; preds = %38
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%52 = load i32, i32* %9
%53 = call i32 @write(i32 1, i8* %51, i32 %52)
%54 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%56 = call i32 @read(i32 0, i8* %55, i32 1024)
store i32 %56, i32* %9
%57 = load i32, i32* %6
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %9
%60 = call i32 @write(i32 %57, i8* %58, i32 %59)
br label %38
61: ; preds = %48
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %35, %12
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"%s serverip\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [7 x i8] c"input:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %46
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%19 = call i32 @inet_pton(i32 %14, i8* %17, i32* %18)
%20 = call i32 @htons(i32 8888)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @connect(i32 %12, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %9
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %44
27: ; preds = %9
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
br label %29
29: ; preds = %36, %27
%30 = call i32 @read(i32 %12, i8* %28, i32 1024)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %32, %37
br label %29
37: ; preds = %29
%38 = call i32 @write(i32 1, i8* %28, i32 %30)
%39 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%40 = call i32 @read(i32 0, i8* %28, i32 1024)
%41 = call i32 @write(i32 %12, i8* %28, i32 %40)
br label %36
42: ; preds = %32
%43 = call i32 @close(i32 %12)
br label %44
44: ; preds = %42, %25
%45 = phi i32 [ 1, %25 ], [ 0, %42 ]
br label %46
46: ; preds = %44, %6
%47 = phi i32 [ 0, %6 ], [ %45, %44 ]
ret i32 %47
}