#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int udp_receive(int *sock, struct sockaddr_in6 *sock_addr, char *buffer,
                int *length) {
  int tmp_length;
  int addrlen;
  addrlen = sizeof(struct sockaddr_in6);
  if ((tmp_length =
           recvfrom(*sock, buffer, 512, 0, (struct sockaddr *)sock_addr,
                    (socklen_t *)&addrlen)) == -1) {
    char str_ip[128];
    inet_ntop(AF_INET6, &(sock_addr->sin6_addr), (char *)&str_ip, 128);
    fprintf(stderr, "[Error] Unable to read from UDP socket [%s]:%d\n", str_ip,
            53);
    return 1;
  }
  memcpy(length, &tmp_length, sizeof(int));
  return 0;
}


x86 assembly (clang, -O0):
<udp_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x4,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x30(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    11d7 <udp_receive+0x97>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
lea    -0xb0(%rbp),%rdx
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    -0xb0(%rbp),%rdx
lea    0xe3f(%rip),%rsi
mov    $0x35,%ecx
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f0 <udp_receive+0xb0>
mov    -0x28(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_receive>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rcx,%r14
mov    %rsi,%rbx
movl   $0x4,0x8(%rsp)
mov    (%rdi),%edi
xor    %ebp,%ebp
lea    0x8(%rsp),%r9
mov    %rdx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1060 <recvfrom@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     1191 <udp_receive+0x51>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
jmp    11d1 <udp_receive+0x91>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rbp
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    $0x80,%ecx
call   1030 <inet_ntop@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    %rbp,%rdx
mov    $0x35,%ecx
call   1040 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"[Error] Unable to read from UDP socket [%s]:%d\0A\00"
define i32 @udp_receive(i32* %0, %struct.sockaddr_in6* %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca %struct.sockaddr_in6*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [128 x i8]
store i32* %0, i32** %6
store %struct.sockaddr_in6* %1, %struct.sockaddr_in6** %7
store i8* %2, i8** %8
store i32* %3, i32** %9
store i32 4, i32* %11
%13 = load i32*, i32** %6
%14 = load i32, i32* %13
%15 = load i8*, i8** %8
%16 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%17 = bitcast %struct.sockaddr_in6* %16 to %struct.sockaddr*
%18 = call i32 @recvfrom(i32 %14, i8* %15, i32 512, i32 0, %struct.sockaddr* %17, i32* %11)
store i32 %18, i32* %10
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %29
20: ; preds = %4
%21 = load i32, i32* @AF_INET6
%22 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %22, i32 0, i32 0
%24 = bitcast [128 x i8]* %12 to i8*
%25 = call i32 @inet_ntop(i32 %21, i32* %23, i8* %24, i32 128)
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %27, i32 53)
store i32 1, i32* %5
br label %32
29: ; preds = %4
%30 = load i32*, i32** %9
%31 = call i32 @memcpy(i32* %30, i32* %10, i32 4)
store i32 0, i32* %5
br label %32
32: ; preds = %29, %20
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"[Error] Unable to read from UDP socket [%s]:%d\0A\00"
define i32 @udp_receive(i32* nocapture %0, %struct.sockaddr_in6* %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%10 = load i32, i32* %0
%11 = bitcast %struct.sockaddr_in6* %1 to %struct.sockaddr*
%12 = call i32 @recvfrom(i32 %10, i8* %2, i32 512, i32 0, %struct.sockaddr* %11, i32* %6)
store i32 %12, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %4
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%16 = load i32, i32* @AF_INET6
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 0, i32 0
%18 = call i32 @inet_ntop(i32 %16, i32* %17, i8* %15, i32 128)
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %15, i32 53)
br label %23
21: ; preds = %4
%22 = call i32 @memcpy(i32* %3, i32* %5, i32 4)
br label %23
23: ; preds = %21, %14
%24 = phi i32 [ 1, %14 ], [ 0, %21 ]
ret i32 %24
}