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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
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*/ stdout;
int /*<<< orphan*/ strlen(char *);
void dg_cli(FILE *fp, int sockfd, struct sockaddr *servaddr,
            socklen_t servlen) {
  int n;
  char sendline[1024];
  char recvline[1024 + 1];
  while (fgets(sendline, 1024, fp) != NULL) {
    sendto(sockfd, sendline, strlen(sendline), 0, servaddr, servlen);
    n = recvfrom(sockfd, recvline, 1024, 0, NULL, NULL);
    recvline[n] = '\0';
    fputs(recvline, stdout);
  }
}


x86 assembly (clang, -O0):
<dg_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1210 <dg_cli+0xc0>
mov    -0xc(%rbp),%eax
mov    %eax,-0x83c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x83c(%rbp),%edi
mov    -0x838(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    -0xc(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1070 <recvfrom@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
movb   $0x0,-0x830(%rbp,%rax,1)
lea    -0x830(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
jmp    1169 <dg_cli+0x19>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dg_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r12
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f3 <dg_cli+0xa3>
mov    %rsp,%rbx
lea    0x400(%rsp),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r14d,%r9d
call   1050 <sendto@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1070 <recvfrom@plt>
cltq
movb   $0x0,0x400(%rsp,%rax,1)
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1040 <fputs@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1190 <dg_cli+0x40>
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@stdout = global i32 0
define void @dg_cli(i32* %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca [1025 x i8]
store i32* %0, i32** %5
store i32 %1, i32* %6
store %struct.sockaddr* %2, %struct.sockaddr** %7
store i32 %3, i32* %8
br label %12
12: ; preds = %17, %4
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%14 = load i32*, i32** %5
%15 = call i32* @fgets(i8* %13, i32 1024, i32* %14)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %34
17: ; preds = %12
%18 = load i32, i32* %6
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = load %struct.sockaddr*, %struct.sockaddr** %7
%23 = load i32, i32* %8
%24 = call i32 @sendto(i32 %18, i8* %19, i32 %21, i32 0, %struct.sockaddr* %22, i32 %23)
%25 = load i32, i32* %6
%26 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 0
%27 = call i32 @recvfrom(i32 %25, i8* %26, i32 1024, i32 0, i32* null, i32* null)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 %29
store i8 0, i8* %30
%31 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 0
%32 = load i32, i32* @stdout
%33 = call i32 @fputs(i8* %31, i32 %32)
br label %12
34: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@stdout = global i32 0
define void @dg_cli(i32* nocapture %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = alloca [1025 x i8]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%9 = call i32* @fgets(i8* %7, i32 1024, i32* %0)
%10 = icmp eq i32* %9, null
br i1 %10, label %21, label %11
11: ; preds = %4, %11
%12 = call i32 @strlen(i8* %7)
%13 = call i32 @sendto(i32 %1, i8* %7, i32 %12, i32 0, %struct.sockaddr* %2, i32 %3)
%14 = call i32 @recvfrom(i32 %1, i8* %8, i32 1024, i32 0, i32* null, i32* null)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 %15
store i8 0, i8* %16
%17 = load i32, i32* @stdout
%18 = call i32 @fputs(i8* %8, i32 %17)
%19 = call i32* @fgets(i8* %7, i32 1024, i32* %0)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %11
21: ; preds = %11, %4
ret void
}