#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int client_handshake(int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, scalar_t__);
int /*<<< orphan*/ stdin;
scalar_t__ *strchr(char *, char);
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  int to_server;
  int from_server;
  char buffer[256];
  to_server = client_handshake(&from_server);
  while (1) {
    printf("Enter text: ");
    fgets(buffer, sizeof(buffer), stdin);
    *(strchr(buffer, '\n')) = 0;
    if (strncmp(buffer, "exit", sizeof(buffer)) == 0)
      break;
    write(to_server, buffer, strlen(buffer) + 1);
    read(from_server, buffer, strlen(buffer) + 1);
    printf("\tReceived: %s\n", buffer);
    strncpy(buffer, "", sizeof(buffer));
  }
  close(to_server);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   10c0 <client_handshake@plt>
mov    %eax,-0x8(%rbp)
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   10b0 <fgets@plt>
lea    -0x110(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
movq   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
lea    0xe01(%rip),%rsi
mov    $0x100,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1225 <main+0x85>
jmp    12cf <main+0x12f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x12c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x110(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x12c(%rbp),%edi
mov    -0x128(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1050 <write@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x11c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x11c(%rbp),%edi
mov    -0x118(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   10a0 <read@plt>
lea    -0x110(%rbp),%rsi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x110(%rbp),%rdi
lea    0xd60(%rip),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
jmp    11be <main+0x1e>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xc(%rsp),%rdi
call   10c0 <client_handshake@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0xe44(%rip),%r15
lea    0xe42(%rip),%r12
lea    0xe49(%rip),%r13
nopw   0x0(%rax,%rax,1)
lea    0xe19(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10b0 <fgets@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
movq   $0x0,(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x100,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1281 <main+0xe1>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <write@plt>
mov    0xc(%rsp),%r14d
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   10a0 <read@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
jmp    11e0 <main+0x40>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Enter text: \00"
@stdin = global i32 0
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [15 x i8] c"\09Received: %s\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
store i32 0, i32* %1
%5 = call i32 @client_handshake(i32* %3)
store i32 %5, i32* %2
br label %6
6: ; preds = %0, %17
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 256, i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%12 = call i64* @strchr(i8* %11, i8 signext 10)
store i64 0, i64* %12
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 256)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %6
br label %34
17: ; preds = %6
%18 = load i32, i32* %2
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%21 = call i64 @strlen(i8* %20)
%22 = add nsw i64 %21, 1
%23 = call i32 @write(i32 %18, i8* %19, i64 %22)
%24 = load i32, i32* %3
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%27 = call i64 @strlen(i8* %26)
%28 = add nsw i64 %27, 1
%29 = call i32 @read(i32 %24, i8* %25, i64 %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%33 = call i32 @strncpy(i8* %32, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 256)
br label %6
34: ; preds = %16
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"Enter text: \00"
@stdin = global i32 0
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [15 x i8] c"\09Received: %s\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = call i32 @client_handshake(i32* %1)
br label %6
6: ; preds = %13, %0
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %4, i32 256, i32 %8)
%10 = call i64* @strchr(i8* %4, i8 signext 10)
store i64 0, i64* %10
%11 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 256)
%12 = icmp eq i64 %11, 0
br i1 %12, label %23, label %13
13: ; preds = %6
%14 = call i64 @strlen(i8* %4)
%15 = add nsw i64 %14, 1
%16 = call i32 @write(i32 %5, i8* %4, i64 %15)
%17 = load i32, i32* %1
%18 = call i64 @strlen(i8* %4)
%19 = add nsw i64 %18, 1
%20 = call i32 @read(i32 %17, i8* %4, i64 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%22 = call i32 @strncpy(i8* %4, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 256)
br label %6
23: ; preds = %6
%24 = call i32 @close(i32 %5)
ret i32 0
}