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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ set_sockaddr(struct sockaddr_in *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void client_run() {
  int n;
  int c;
  int sockfd;
  char buf[(256)];
  struct sockaddr_in srv_addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  set_sockaddr(&srv_addr);
  if (connect(sockfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr)) < 0) {
    perror("connect()");
    exit(1);
  }
  for (;;) {
    printf("input: ");
    fgets(buf, sizeof(buf), stdin);
    c = strlen(buf) - 1;
    buf[c] = '\0';
    write(sockfd, buf, c + 1);
    bzero(buf, sizeof(buf));
    while (errno != EAGAIN && (n = read(sockfd, buf, sizeof(buf))) > 0) {
      printf("echo: %s\n", buf);
      bzero(buf, sizeof(buf));
      c -= n;
      if (c <= 0) {
        break;
      }
    }
  }
  close(sockfd);
}


x86 assembly (clang, -O0):
<client_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
lea    -0x118(%rbp),%rdi
call   1080 <set_sockaddr@plt>
mov    -0xc(%rbp),%edi
lea    -0x118(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    1217 <client_run+0x67>
lea    0xdf8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    121c <client_run+0x6c>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1070 <fgets@plt>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x8(%rbp),%edx
add    $0x1,%edx
call   1030 <write@plt>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2d1d(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x119(%rbp)
je     12cd <client_run+0x11d>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x119(%rbp)
mov    -0x119(%rbp),%al
test   $0x1,%al
jne    12e0 <client_run+0x130>
jmp    1325 <client_run+0x175>
lea    -0x110(%rbp),%rsi
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
mov    -0x4(%rbp),%ecx
mov    -0x8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1320 <client_run+0x170>
jmp    1325 <client_run+0x175>
jmp    1288 <client_run+0xd8>
jmp    121c <client_run+0x6c>

x86 assembly (clang, -O2):
<client_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r12d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <set_sockaddr@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     12b7 <client_run+0x107>
lea    0x10(%rsp),%rbx
mov    0x2db9(%rip),%r14
mov    0x2da2(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
lea    0xde3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdx
lea    -0x1(%rdx),%r15
movb   $0x0,0xf(%rsp,%rdx,1)
mov    %r12d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
mov    (%r14),%rax
cmp    0x0(%r13),%rax
je     1220 <client_run+0x70>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1220 <client_run+0x70>
mov    %eax,%ebp
lea    0xd7c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
sub    %ebp,%r15d
jg     1270 <client_run+0xc0>
jmp    1220 <client_run+0x70>
lea    0xd42(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"connect()\00"
@.str.1 = [8 x i8] c"input: \00"
@stdin = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [10 x i8] c"echo: %s\0A\00"
define void @client_run() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.sockaddr_in
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = call i32 @set_sockaddr(%struct.sockaddr_in* %5)
%10 = load i32, i32* %3
%11 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%12 = call i64 @connect(i32 %10, %struct.sockaddr* %11, i32 4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
br label %18
18: ; preds = %59, %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%21 = load i32, i32* @stdin
%22 = call i32 @fgets(i8* %20, i32 256, i32 %21)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = sub nsw i32 %24, 1
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %27
store i8 0, i8* %28
%29 = load i32, i32* %3
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
%33 = call i32 @write(i32 %29, i8* %30, i32 %32)
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%35 = call i32 @bzero(i8* %34, i32 256)
br label %36
36: ; preds = %58, %18
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* %3
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 256)
store i32 %43, i32* %1
%44 = icmp sgt i32 %43, 0
br label %45
45: ; preds = %40, %36
%46 = phi i1 [ false, %36 ], [ %44, %40 ]
br i1 %46, label %47, label %59
47: ; preds = %45
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%51 = call i32 @bzero(i8* %50, i32 256)
%52 = load i32, i32* %1
%53 = load i32, i32* %2
%54 = sub nsw i32 %53, %52
store i32 %54, i32* %2
%55 = load i32, i32* %2
%56 = icmp sle i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %47
br label %59
58: ; preds = %47
br label %36
59: ; preds = %57, %45
br label %18
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"connect()\00"
@.str.1 = [8 x i8] c"input: \00"
@stdin = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [10 x i8] c"echo: %s\0A\00"
define void @client_run() {
%1 = alloca [256 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = call i32 @set_sockaddr(%struct.sockaddr_in* %2)
%9 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%10 = call i64 @connect(i32 %7, %struct.sockaddr* %9, i32 4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %26, %34, %31
br label %16
16: ; preds = %0, %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @stdin
%19 = call i32 @fgets(i8* %3, i32 256, i32 %18)
%20 = call i32 @strlen(i8* %3)
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %22
store i8 0, i8* %23
%24 = call i32 @write(i32 %7, i8* %3, i32 %20)
%25 = call i32 @bzero(i8* %3, i32 256)
br label %26
26: ; preds = %34, %16
%27 = phi i32 [ %21, %16 ], [ %37, %34 ]
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp eq i64 %28, %29
br i1 %30, label %15, label %31
31: ; preds = %26
%32 = call i32 @read(i32 %7, i8* %3, i32 256)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %15
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%36 = call i32 @bzero(i8* %3, i32 256)
%37 = sub nsw i32 %27, %32
%38 = icmp slt i32 %37, 1
br i1 %38, label %15, label %26
}