#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 size_t ssize_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ sendbuf;
typedef int /*<<< orphan*/ recvbuf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ exit_print(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
size_t read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  struct sockaddr_in servaddr;
  int i, sockfd[1];
  for (i = 0; i < 1; i++) {
    if ((sockfd[i] = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
      exit_print("socket error.\n");
    }
    memset((void *)&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(9999);
    inet_aton("127.0.0.1", &servaddr.sin_addr);
    connect(sockfd[i], (const struct sockaddr *)&servaddr, sizeof(servaddr));
  }
  char sendbuf[80];
  char recvbuf[80];
  ssize_t len;
  while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) {
    if (write(sockfd[0], sendbuf, strlen(sendbuf)) < 0) {
      exit_print("client write error.\n");
    }
    sleep(1);
    if (write(sockfd[0], sendbuf + 1, strlen(sendbuf) - 1) < 0) {
      exit_print("client write error.\n");
    }
    if ((len = read(sockfd[0], recvbuf, sizeof(recvbuf))) <= 0) {
      exit_print("client read error: server terminated prematurely.\n");
    }
    recvbuf[len] = '\0';
    fputs("client: ", stdout);
    fputs(recvbuf, stdout);
    fflush(stdout);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    128d <main+0xbd>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
movslq -0x24(%rbp),%rcx
mov    %eax,-0x28(%rbp,%rcx,4)
cmp    $0x0,%eax
jge    1230 <main+0x60>
lea    0xdd5(%rip),%rdi
call   1040 <exit_print@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x270f,%edi
call   1060 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xdab(%rip),%rdi
call   10b0 <inet_aton@plt>
movslq -0x24(%rbp),%rax
mov    -0x28(%rbp,%rax,4),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11f0 <main+0x20>
jmp    1292 <main+0xc2>
lea    -0x80(%rbp),%rdi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     13d2 <main+0x202>
mov    -0x28(%rbp),%eax
mov    %eax,-0xe4(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0xe0(%rbp)
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12fb <main+0x12b>
lea    0xd23(%rip),%rdi
call   1040 <exit_print@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0xf4(%rbp)
lea    -0x80(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xf0(%rbp)
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xf4(%rbp),%edi
mov    -0xf0(%rbp),%rsi
mov    %rax,%rdx
sub    $0x1,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1355 <main+0x185>
lea    0xcc9(%rip),%rdi
call   1040 <exit_print@plt>
mov    -0x28(%rbp),%edi
lea    -0xd0(%rbp),%rsi
mov    $0x50,%edx
call   1090 <read@plt>
mov    %rax,-0xd8(%rbp)
cmp    $0x0,%rax
ja     1386 <main+0x1b6>
lea    0xcad(%rip),%rdi
call   1040 <exit_print@plt>
mov    -0xd8(%rbp),%rax
movb   $0x0,-0xd0(%rbp,%rax,1)
mov    0x2c2c(%rip),%rax
mov    (%rax),%esi
lea    0xcbc(%rip),%rdi
call   1070 <fputs@plt>
lea    -0xd0(%rbp),%rdi
mov    0x2c10(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
mov    0x2c02(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
jmp    1292 <main+0xc2>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    0x2e00(%rip),%r14
mov    (%r14),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    120d <main+0x3d>
lea    0xdf8(%rip),%rdi
call   1040 <exit_print@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    $0x270f,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xdd4(%rip),%rdi
mov    %rbp,%rsi
call   10b0 <inet_aton@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     1358 <main+0x188>
lea    0x10(%rsp),%r15
lea    0x60(%rsp),%r13
mov    0x2d44(%rip),%r14
lea    0xdd6(%rip),%rbp
jmp    12d3 <main+0x103>
nopl   (%rax)
movb   $0x0,0x60(%rsp,%r12,1)
mov    (%r14),%esi
mov    %rbp,%rdi
call   1070 <fputs@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   1070 <fputs@plt>
mov    (%r14),%edi
call   10c0 <fflush@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    $0x50,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     1358 <main+0x188>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    12f9 <main+0x129>
lea    0xd25(%rip),%rdi
call   1040 <exit_print@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    -0x1(%rax),%rdx
mov    %ebx,%edi
lea    0x11(%rsp),%rsi
call   1030 <write@plt>
test   %rax,%rax
jns    132c <main+0x15c>
lea    0xcf2(%rip),%rdi
call   1040 <exit_print@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x50,%edx
call   1090 <read@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1290 <main+0xc0>
lea    0xce0(%rip),%rdi
call   1040 <exit_print@plt>
jmp    1290 <main+0xc0>
xor    %eax,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"socket error.\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stdin = global i32 0
@.str.2 = [21 x i8] c"client write error.\0A\00"
@.str.3 = [51 x i8] c"client read error: server terminated prematurely.\0A\00"
@.str.4 = [9 x i8] c"client: \00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [1 x i32]
%9 = alloca [80 x i8]
%10 = alloca [80 x i8]
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
br label %12
12: ; preds = %40, %2
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %43
15: ; preds = %12
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [1 x i32], [1 x i32]* %8, i64 0, i64 %20
store i32 %18, i32* %21
%22 = icmp slt i32 %18, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @exit_print(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = bitcast %struct.sockaddr_in* %6 to i8*
%27 = call i32 @memset(i8* %26, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %28, i32* %29
%30 = call i32 @htons(i32 9999)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%33 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %32)
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [1 x i32], [1 x i32]* %8, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%39 = call i32 @connect(i32 %37, %struct.sockaddr* %38, i32 12)
br label %40
40: ; preds = %25
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %12
43: ; preds = %12
br label %44
44: ; preds = %80, %43
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* @stdin
%47 = call i32* @fgets(i8* %45, i32 80, i32 %46)
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %90
49: ; preds = %44
%50 = getelementptr inbounds [1 x i32], [1 x i32]* %8, i64 0, i64 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%53 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%54 = call i64 @strlen(i8* %53)
%55 = call i64 @write(i32 %51, i8* %52, i64 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @exit_print(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
%60 = call i32 @sleep(i32 1)
%61 = getelementptr inbounds [1 x i32], [1 x i32]* %8, i64 0, i64 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%64 = getelementptr inbounds i8, i8* %63, i64 1
%65 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%66 = call i64 @strlen(i8* %65)
%67 = sub nsw i64 %66, 1
%68 = call i64 @write(i32 %62, i8* %64, i64 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %59
%71 = call i32 @exit_print(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %72
72: ; preds = %70, %59
%73 = getelementptr inbounds [1 x i32], [1 x i32]* %8, i64 0, i64 0
%74 = load i32, i32* %73
%75 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%76 = call i64 @read(i32 %74, i8* %75, i32 80)
store i64 %76, i64* %11
%77 = icmp ule i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %72
%79 = call i32 @exit_print(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
br label %80
80: ; preds = %78, %72
%81 = load i64, i64* %11
%82 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 %81
store i8 0, i8* %82
%83 = load i32, i32* @stdout
%84 = call i32 @fputs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %83)
%85 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%86 = load i32, i32* @stdout
%87 = call i32 @fputs(i8* %85, i32 %86)
%88 = load i32, i32* @stdout
%89 = call i32 @fflush(i32 %88)
br label %44
90: ; preds = %44
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"socket error.\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stdin = global i32 0
@.str.2 = [21 x i8] c"client write error.\0A\00"
@.str.3 = [51 x i8] c"client read error: server terminated prematurely.\0A\00"
@.str.4 = [9 x i8] c"client: \00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [80 x i8]
%5 = alloca [80 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @exit_print(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = call i32 @memset(i8* %6, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 9999)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%20 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %19)
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @connect(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%25 = load i32, i32* @stdin
%26 = call i32* @fgets(i8* %23, i32 80, i32 %25)
%27 = icmp eq i32* %26, null
br i1 %27, label %60, label %28
28: ; preds = %13
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 1
br label %30
30: ; preds = %28, %49
%31 = call i64 @strlen(i8* %23)
%32 = call i64 @write(i32 %9, i8* %23, i64 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @exit_print(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = call i32 @sleep(i32 1)
%38 = call i64 @strlen(i8* %23)
%39 = add nsw i64 %38, -1
%40 = call i64 @write(i32 %9, i8* %29, i64 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = call i32 @exit_print(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %36
%45 = call i64 @read(i32 %9, i8* %24, i32 80)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @exit_print(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 %45
store i8 0, i8* %50
%51 = load i32, i32* @stdout
%52 = call i32 @fputs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %51)
%53 = load i32, i32* @stdout
%54 = call i32 @fputs(i8* %24, i32 %53)
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
%57 = load i32, i32* @stdin
%58 = call i32* @fgets(i8* %23, i32 80, i32 %57)
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %30
60: ; preds = %49, %13
ret i32 0
}