#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_storage {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef long ssize_t;
typedef int /*<<< orphan*/ src_addr;
typedef int socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
long recvfrom(int, char *, long, int /*<<< orphan*/, struct sockaddr *, int *);
char *strerror(int /*<<< orphan*/);
long listenToSocket(int sock, char *buffer, long maxLen) {
  struct sockaddr_storage src_addr;
  socklen_t src_addr_len = sizeof(src_addr);
  memset(buffer, 0, maxLen);
  ssize_t count = recvfrom(sock, buffer, maxLen, 0,
                           (struct sockaddr *)&src_addr, &src_addr_len);
  if (count < 0) {
    printf("%s", strerror(errno));
    return 0;
  } else if (count == 0) {
    puts("??");
  } else if (count >= maxLen) {
    printf("datagram too large for buffer: truncated (%zd >= %i)", count,
           (int)maxLen);
    return 0;
  }
  char ipstr[INET6_ADDRSTRLEN];
  if (src_addr.ss_family == AF_INET) {
    struct sockaddr_in *s = (struct sockaddr_in *)&src_addr;
    inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
  } else {
    struct sockaddr_in6 *s = (struct sockaddr_in6 *)&src_addr;
    inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr);
  }
  return count;
}


x86 assembly (clang, -O0):
<listenToSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11de <listenToSocket+0x7e>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <listenToSocket+0x179>
cmpq   $0x0,-0x38(%rbp)
jne    11fa <listenToSocket+0x9a>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
jmp    1232 <listenToSocket+0xd2>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jl     122d <listenToSocket+0xcd>
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d9 <listenToSocket+0x179>
jmp    1232 <listenToSocket+0xd2>
jmp    1237 <listenToSocket+0xd7>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d6e(%rip),%rcx
cmp    (%rcx),%rax
jne    12a5 <listenToSocket+0x145>
mov    -0x60(%rbp),%rdx
mov    -0x68(%rbp),%rax
lea    -0x28(%rbp),%rcx
mov    %rcx,-0x50(%rbp)
mov    0x2d4e(%rip),%rcx
mov    (%rcx),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
jmp    12ca <listenToSocket+0x16a>
mov    -0x60(%rbp),%rdx
mov    -0x68(%rbp),%rax
lea    -0x28(%rbp),%rcx
mov    %rcx,-0x58(%rbp)
mov    0x2d1c(%rip),%rcx
mov    (%rcx),%rdi
mov    -0x58(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenToSocket>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%r14d
movl   $0x8,-0x1c(%rbp)
mov    %rsi,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    -0x28(%rbp),%r8
lea    -0x1c(%rbp),%r9
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1070 <recvfrom@plt>
test   %rax,%rax
js     11c7 <listenToSocket+0x67>
mov    %rax,%r14
je     11eb <listenToSocket+0x8b>
cmp    %r15,%r14
jl     11f7 <listenToSocket+0x97>
lea    0xe51(%rip),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %r14d,%r14d
jmp    123d <listenToSocket+0xdd>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe24(%rip),%rdi
xor    %r14d,%r14d
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123d <listenToSocket+0xdd>
lea    0xe11(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rbx
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdx
mov    %rdx,%rsp
mov    -0x28(%rbp),%rdi
mov    0x2dbe(%rip),%rax
cmp    (%rax),%rdi
je     1231 <listenToSocket+0xd1>
mov    0x2daa(%rip),%rax
mov    (%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <inet_ntop@plt>
mov    %rbx,%rsp
mov    %r14,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"??\00"
@.str.2 = [53 x i8] c"datagram too large for buffer: truncated (%zd >= %i)\00"
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i64 @listenToSocket(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca %struct.sockaddr_in*
%14 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 8, i32* %9
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i32 @memset(i8* %15, i32 0, i64 %16)
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = load i64, i64* %7
%21 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%22 = call i64 @recvfrom(i32 %18, i8* %19, i64 %20, i32 0, %struct.sockaddr* %21, i32* %9)
store i64 %22, i64* %10
%23 = load i64, i64* %10
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %3
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %27)
store i64 0, i64* %4
br label %71
29: ; preds = %3
%30 = load i64, i64* %10
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %44
34: ; preds = %29
%35 = load i64, i64* %10
%36 = load i64, i64* %7
%37 = icmp sge i64 %35, %36
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load i64, i64* %10
%40 = load i64, i64* %7
%41 = trunc i64 %40 to i32
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i64 %39, i32 %41)
store i64 0, i64* %4
br label %71
43: ; preds = %34
br label %44
44: ; preds = %43, %32
br label %45
45: ; preds = %44
%46 = load i32, i32* @INET6_ADDRSTRLEN
%47 = zext i32 %46 to i64
%48 = call i8* @llvm.stacksave()
store i8* %48, i8** %11
%49 = alloca i8, i64 %47
store i64 %47, i64* %12
%50 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %8, i32 0, i32 0
%51 = load i64, i64* %50
%52 = load i64, i64* @AF_INET
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %61
54: ; preds = %45
%55 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr_in*
store %struct.sockaddr_in* %55, %struct.sockaddr_in** %13
%56 = load i64, i64* @AF_INET
%57 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %57, i32 0, i32 0
%59 = trunc i64 %47 to i32
%60 = call i32 @inet_ntop(i64 %56, i32* %58, i8* %49, i32 %59)
br label %68
61: ; preds = %45
%62 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %62, %struct.sockaddr_in6** %14
%63 = load i64, i64* @AF_INET6
%64 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %14
%65 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %64, i32 0, i32 0
%66 = trunc i64 %47 to i32
%67 = call i32 @inet_ntop(i64 %63, i32* %65, i8* %49, i32 %66)
br label %68
68: ; preds = %61, %54
%69 = load i64, i64* %10
store i64 %69, i64* %4
%70 = load i8*, i8** %11
br label %71
71: ; preds = %68, %38, %25
%72 = load i64, i64* %4
ret i64 %72
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"??\00"
@.str.2 = [53 x i8] c"datagram too large for buffer: truncated (%zd >= %i)\00"
@INET6_ADDRSTRLEN = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i64 @listenToSocket(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_storage* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%8 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%9 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%10 = call i64 @recvfrom(i32 %0, i8* %1, i64 %2, i32 0, %struct.sockaddr* %9, i32* %5)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %38
16: ; preds = %3
%17 = icmp eq i64 %10, 0
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %25
20: ; preds = %16
%21 = icmp slt i64 %10, %2
br i1 %21, label %25, label %22
22: ; preds = %20
%23 = trunc i64 %2 to i32
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i64 %10, i32 %23)
br label %38
25: ; preds = %18, %20
%26 = load i32, i32* @INET6_ADDRSTRLEN
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
%29 = alloca i8, i64 %27
%30 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* @AF_INET
%33 = icmp eq i64 %31, %32
%34 = load i64, i64* @AF_INET6
%35 = select i1 %33, i64 %31, i64 %34
%36 = bitcast %struct.sockaddr_storage* %4 to i32*
%37 = call i32 @inet_ntop(i64 %35, i32* %36, i8* %29, i32 %26)
br label %38
38: ; preds = %25, %22, %12
%39 = phi i64 [ 0, %12 ], [ %10, %25 ], [ 0, %22 ]
ret i64 %39
}