#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 {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void str_echo(int sockfd, struct sockaddr *svaddr, socklen_t addrlen) {
  ssize_t n;
  char sendmsg[1024], recvmsg[1024];
  while (fgets(sendmsg, 1024, stdin) != NULL) {
    write(sockfd, sendmsg, strlen(sendmsg));
    n = read(sockfd, recvmsg, 1024);
    recvmsg[n - 1] = '\0';
    printf("%s\n", recvmsg);
  }
}


x86 assembly (clang, -O0):
<str_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x420(%rbp),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     11ff <str_echo+0xaf>
mov    -0x4(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x820(%rbp,%rax,1)
lea    -0x820(%rbp),%rsi
lea    0xe0b(%rip),%rdi
call   1050 <printf@plt>
jmp    1165 <str_echo+0x15>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_echo>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %edi,%ebx
mov    0x2e7f(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     11ec <str_echo+0x9c>
mov    %rsp,%r14
lea    0x400(%rsp),%r15
lea    0xe6f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
cltq
movb   $0x0,-0x1(%rax,%r15,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    11a0 <str_echo+0x50>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@stdin = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define void @str_echo(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32 %2, i32* %6
br label %10
10: ; preds = %15, %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%12 = load i32, i32* @stdin
%13 = call i32* @fgets(i8* %11, i32 1024, i32 %12)
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %30
15: ; preds = %10
%16 = load i32, i32* %4
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
%21 = load i32, i32* %4
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 1024)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %28)
br label %10
30: ; preds = %10
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@stdin = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define void @str_echo(i32 %0, %struct.sockaddr* nocapture readnone %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32* @fgets(i8* %6, i32 1024, i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %22, label %11
11: ; preds = %3, %11
%12 = call i32 @strlen(i8* %6)
%13 = call i32 @write(i32 %0, i8* %6, i32 %12)
%14 = call i32 @read(i32 %0, i8* %7, i32 1024)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %7)
%19 = load i32, i32* @stdin
%20 = call i32* @fgets(i8* %6, i32 1024, i32 %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %11
22: ; preds = %11, %3
ret void
}