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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int inet_ntop(int /*<<< orphan*/, int const *, int *, int);
scalar_t__ malloc(int);
scalar_t__ memcmp(int const *, int *, int);
int /*<<< orphan*/ printf(char *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, int *, int *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          int const *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
void dg_cli(FILE *fp, int sockfd, const struct sockaddr *psa, socklen_t slen) {
  int n;
  char s[80], r[80];
  socklen_t len;
  struct sockaddr *preply_addr;
  preply_addr = (socklen_t *)malloc(slen * sizeof(socklen_t));
  while (fgets(s, 80, fp) != NULL) {
    sendto(sockfd, s, strlen(s), 0, psa, slen);
    len = slen;
    n = recvfrom(sockfd, r, 80, 0, preply_addr, &len);
    if ((len != slen) || memcmp(psa, preply_addr, len) != 0) {
      printf("reply from %d (ignored)\n",
             inet_ntop(AF_INET, psa, preply_addr, len));
      continue;
    }
    r[n] = 0;
    fputs(r, stdout);
  }
}


x86 assembly (clang, -O0):
<dg_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0xd0(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x50,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     12cd <dg_cli+0x13d>
mov    -0xc(%rbp),%eax
mov    %eax,-0xdc(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xdc(%rbp),%edi
mov    -0xd8(%rbp),%rsi
mov    %eax,%edx
mov    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xc4(%rbp)
mov    -0xc(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    -0xd0(%rbp),%r8
mov    $0x50,%edx
xor    %ecx,%ecx
lea    -0xc4(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc4(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1275 <dg_cli+0xe5>
mov    -0x18(%rbp),%rdi
mov    -0xd0(%rbp),%rsi
mov    -0xc4(%rbp),%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
je     12a7 <dg_cli+0x117>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0xd0(%rbp),%rdx
mov    -0xc4(%rbp),%ecx
call   1030 <inet_ntop@plt>
mov    %eax,%esi
lea    0xd63(%rip),%rdi
call   1050 <printf@plt>
jmp    11bf <dg_cli+0x2f>
movslq -0x20(%rbp),%rax
movb   $0x0,-0xc0(%rbp,%rax,1)
lea    -0xc0(%rbp),%rdi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
jmp    11bf <dg_cli+0x2f>
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dg_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r15
lea    0x0(,%rbx,4),%edi
call   10a0 <malloc@plt>
mov    %rax,%rbp
lea    0x60(%rsp),%rdi
mov    $0x50,%esi
mov    %r15,0x8(%rsp)
mov    %r15,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     12ab <dg_cli+0x11b>
lea    0x60(%rsp),%r13
jmp    122d <dg_cli+0x9d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x4(%rsp),%ecx
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1030 <inet_ntop@plt>
lea    0xdf1(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %r13,%rdi
mov    $0x50,%esi
mov    0x8(%rsp),%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     12ab <dg_cli+0x11b>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %ebx,%r9d
call   1070 <sendto@plt>
mov    %ebx,0x4(%rsp)
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    $0x50,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
lea    0x4(%rsp),%r9
call   10b0 <recvfrom@plt>
mov    0x4(%rsp),%ecx
cmp    %ebx,%ecx
jne    11f4 <dg_cli+0x64>
mov    %eax,%r15d
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    11f0 <dg_cli+0x60>
movslq %r15d,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1060 <fputs@plt>
jmp    1216 <dg_cli+0x86>
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type opaque
@.str = [25 x i8] c"reply from %d (ignored)\0A\00"
@AF_INET = global i32 0
@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 [80 x i8]
%11 = alloca [80 x i8]
%12 = alloca i32
%13 = alloca %struct.sockaddr*
store i32* %0, i32** %5
store i32 %1, i32* %6
store %struct.sockaddr* %2, %struct.sockaddr** %7
store i32 %3, i32* %8
%14 = load i32, i32* %8
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 4
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i32*
%20 = bitcast i32* %19 to %struct.sockaddr*
store %struct.sockaddr* %20, %struct.sockaddr** %13
br label %21
21: ; preds = %61, %52, %4
%22 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%23 = load i32*, i32** %5
%24 = call i32* @fgets(i8* %22, i32 80, i32* %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %68
26: ; preds = %21
%27 = load i32, i32* %6
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = load %struct.sockaddr*, %struct.sockaddr** %7
%32 = bitcast %struct.sockaddr* %31 to i32*
%33 = load i32, i32* %8
%34 = call i32 @sendto(i32 %27, i8* %28, i32 %30, i32 0, i32* %32, i32 %33)
%35 = load i32, i32* %8
store i32 %35, i32* %12
%36 = load i32, i32* %6
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%38 = load %struct.sockaddr*, %struct.sockaddr** %13
%39 = bitcast %struct.sockaddr* %38 to i32*
%40 = call i32 @recvfrom(i32 %36, i8* %37, i32 80, i32 0, i32* %39, i32* %12)
store i32 %40, i32* %9
%41 = load i32, i32* %12
%42 = load i32, i32* %8
%43 = icmp ne i32 %41, %42
br i1 %43, label %52, label %44
44: ; preds = %26
%45 = load %struct.sockaddr*, %struct.sockaddr** %7
%46 = bitcast %struct.sockaddr* %45 to i32*
%47 = load %struct.sockaddr*, %struct.sockaddr** %13
%48 = bitcast %struct.sockaddr* %47 to i32*
%49 = load i32, i32* %12
%50 = call i64 @memcmp(i32* %46, i32* %48, i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %61
52: ; preds = %44, %26
%53 = load i32, i32* @AF_INET
%54 = load %struct.sockaddr*, %struct.sockaddr** %7
%55 = bitcast %struct.sockaddr* %54 to i32*
%56 = load %struct.sockaddr*, %struct.sockaddr** %13
%57 = bitcast %struct.sockaddr* %56 to i32*
%58 = load i32, i32* %12
%59 = call i32 @inet_ntop(i32 %53, i32* %55, i32* %57, i32 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %59)
br label %21
61: ; preds = %44
%62 = load i32, i32* %9
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%66 = load i32, i32* @stdout
%67 = call i32 @fputs(i8* %65, i32 %66)
br label %21
68: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type opaque
@.str = [25 x i8] c"reply from %d (ignored)\0A\00"
@AF_INET = global i32 0
@stdout = global i32 0
define void @dg_cli(i32* nocapture %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca [80 x i8]
%6 = alloca [80 x i8]
%7 = alloca i32
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%10 = bitcast i32* %7 to i8*
%11 = shl i32 %3, 2
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i32*
%14 = call i32* @fgets(i8* %8, i32 80, i32* %0)
%15 = icmp eq i32* %14, null
br i1 %15, label %42, label %16
16: ; preds = %4
%17 = bitcast %struct.sockaddr* %2 to i32*
br label %18
18: ; preds = %16, %34
%19 = call i32 @strlen(i8* %8)
%20 = call i32 @sendto(i32 %1, i8* %8, i32 %19, i32 0, i32* %17, i32 %3)
store i32 %3, i32* %7
%21 = call i32 @recvfrom(i32 %1, i8* %9, i32 80, i32 0, i32* %13, i32* %7)
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, %3
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = call i64 @memcmp(i32* %17, i32* %13, i32 %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %24
%28 = load i32, i32* %7
br label %29
29: ; preds = %27, %18
%30 = phi i32 [ %28, %27 ], [ %22, %18 ]
%31 = load i32, i32* @AF_INET
%32 = call i32 @inet_ntop(i32 %31, i32* %17, i32* %13, i32 %30)
%33 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %29, %37
%35 = call i32* @fgets(i8* %8, i32 80, i32* %0)
%36 = icmp eq i32* %35, null
br i1 %36, label %42, label %18
37: ; preds = %24
%38 = sext i32 %21 to i64
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i32, i32* @stdout
%41 = call i32 @fputs(i8* %9, i32 %40)
br label %34
42: ; preds = %34, %4
ret void
}