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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(void) {
  int socket_fd;
  struct sockaddr_in saddr;
  int ret;
  char buf[1024];
  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_fd < 0) {
    printf("create socket failure socket_fd = %d\n", socket_fd);
    return -1;
  }
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(8099);
  saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  ret = connect(socket_fd, (struct sockaddr *)&saddr, sizeof(saddr));
  if (ret < 0) {
    printf("connect failure, ret = %d\n", ret);
    return -1;
  }
  ret = read(socket_fd, buf, 32);
  if (ret < 0) {
    printf("read failure. ret = %d\n", ret);
  } else {
    printf("server: %s\n", buf);
  }
  while (1) {
    scanf("%s", buf);
    ret = write(socket_fd, buf, 1024);
    if (ret < 0) {
      printf("write failure. ret = %d\n", ret);
      continue;
    }
    ret = read(socket_fd, buf, 1024);
    if (ret < 0) {
      printf("read server failure. ret = %d\n", ret);
      continue;
    }
    printf("server: %s\n", buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d5 <main+0x55>
mov    -0x8(%rbp),%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <main+0x1a4>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1fa3,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    0xe31(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1238 <main+0xb8>
mov    -0x1c(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1324 <main+0x1a4>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    126f <main+0xef>
mov    -0x1c(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1284 <main+0x104>
lea    -0x420(%rbp),%rsi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1289 <main+0x109>
lea    -0x420(%rbp),%rsi
lea    0xdd8(%rip),%rdi
call   1080 <scanf@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12d3 <main+0x153>
mov    -0x1c(%rbp),%esi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1289 <main+0x109>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    130a <main+0x18a>
mov    -0x1c(%rbp),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1289 <main+0x109>
lea    -0x420(%rbp),%rsi
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1289 <main+0x109>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2e50(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1210 <main+0x90>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x1fa3,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe5b(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %eax,%eax
js     121b <main+0x9b>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x20,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1242 <main+0xc2>
lea    0xe61(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1252 <main+0xd2>
lea    0xde9(%rip),%rdi
mov    %ebp,%esi
jmp    1224 <main+0xa4>
lea    0xe0e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe16(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe0d(%rip),%r15
lea    0xe1f(%rip),%r12
lea    0xdf0(%rip),%r13
jmp    128c <main+0x10c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1280 <main+0x100>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
js     12d0 <main+0x150>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    128c <main+0x10c>
nopl   0x0(%rax)
mov    %r12,%rdi
jmp    1283 <main+0x103>

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

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