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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, 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);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
void do_cli(int sockfd, struct sockaddr *seraddr, int len) {
  char buffer[1024];
  int n;
  while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
    fprintf(stderr, "read from tty:%s\n", buffer);
    sendto(sockfd, buffer, strlen(buffer), 0, seraddr, len);
    n = recvfrom(sockfd, buffer, 1024, 0, NULL, NULL);
    buffer[n] = '\0';
    fprintf(stderr, "recvfrom server content:%s\n", buffer);
  }
}


x86 assembly (clang, -O0):
<do_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%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   1050 <fgets@plt>
cmp    $0x0,%rax
je     123a <do_cli+0xea>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xe60(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x434(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
xor    %ecx,%ecx
call   1040 <sendto@plt>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1070 <recvfrom@plt>
mov    %eax,-0x424(%rbp)
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xde2(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1165 <do_cli+0x15>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     1214 <do_cli+0xc4>
mov    0x2e49(%rip),%r15
mov    %rsp,%rbx
lea    0xe79(%rip),%r13
nopl   0x0(%rax)
mov    (%r15),%edi
lea    0xe56(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r14d,%r9d
call   1040 <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,(%rsp,%rax,1)
mov    (%r15),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
jne    11a0 <do_cli+0x50>
add    $0x408,%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
@stderr = global i32 0
@.str = [18 x i8] c"read from tty:%s\0A\00"
@.str.1 = [28 x i8] c"recvfrom server content:%s\0A\00"
define void @do_cli(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32 %2, i32* %6
br label %9
9: ; preds = %14, %3
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i32* @fgets(i8* %10, i32 1024, i32 %11)
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %34
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %4
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = load %struct.sockaddr*, %struct.sockaddr** %5
%23 = load i32, i32* %6
%24 = call i32 @sendto(i32 %18, i8* %19, i32 %21, i32 0, %struct.sockaddr* %22, i32 %23)
%25 = load i32, i32* %4
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%27 = call i32 @recvfrom(i32 %25, i8* %26, i32 1024, i32 0, i32* null, i32* null)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* @stderr
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %32)
br label %9
34: ; preds = %9
ret void
}

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