#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 recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
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) {
    sendto(sockfd, sendmsg, strlen(sendmsg), 0, svaddr, addrlen);
    n = recvfrom(sockfd, recvmsg, 1024, 0, NULL, NULL);
    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   1060 <fgets@plt>
cmp    $0x0,%rax
je     1213 <str_echo+0xc3>
mov    -0x4(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    -0x4(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1070 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x820(%rbp,%rax,1)
lea    -0x820(%rbp),%rsi
lea    0xdf7(%rip),%rdi
call   1040 <printf@plt>
jmp    1165 <str_echo+0x15>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1200 <str_echo+0xb0>
mov    %rsp,%r12
lea    0x400(%rsp),%rbx
lea    0xe6a(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r14d,%r9d
call   1050 <sendto@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1070 <recvfrom@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11a0 <str_echo+0x50>
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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 %32
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 = load %struct.sockaddr*, %struct.sockaddr** %5
%21 = load i32, i32* %6
%22 = call i32 @sendto(i32 %16, i8* %17, i32 %19, i32 0, %struct.sockaddr* %20, i32 %21)
%23 = load i32, i32* %4
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%25 = call i32 @recvfrom(i32 %23, i8* %24, i32 1024, i32 0, i32* null, i32* null)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %30)
br label %10
32: ; 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* %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 @sendto(i32 %0, i8* %6, i32 %12, i32 0, %struct.sockaddr* %1, i32 %2)
%14 = call i32 @recvfrom(i32 %0, i8* %7, i32 1024, i32 0, i32* null, i32* null)
%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
}