#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 /*<<< orphan*/ tv;
struct timeval {
  int tv_usec;
};
struct sockaddr {
  int /*<<< orphan*/ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setup_rx(struct sockaddr *addr, socklen_t alen) {
  struct timeval tv = {.tv_usec = 100 * 1000};
  int fd;
  fd = socket(addr->sa_family, SOCK_DGRAM, 0);
  if (fd == -1)
    error(1, errno, "socket r");
  if (bind(fd, addr, alen))
    error(1, errno, "bind");
  if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
    error(1, errno, "setsockopt rcv timeout");
  return fd;
}


x86 assembly (clang, -O0):
<setup_rx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xeab(%rip),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1195 <setup_rx+0x55>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe74(%rip),%rdx
call   1050 <error@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
je     11c8 <setup_rx+0x88>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe4a(%rip),%rdx
call   1050 <error@plt>
mov    -0x14(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1210 <setup_rx+0xd0>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe07(%rip),%rdx
call   1050 <error@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_rx>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
movl   $0x186a0,0x8(%rsp)
mov    (%rdi),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1189 <setup_rx+0x49>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdx
mov    $0x1,%edi
call   1050 <error@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <bind@plt>
test   %rax,%rax
je     11b5 <setup_rx+0x75>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdx
mov    $0x1,%edi
call   1050 <error@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11f8 <setup_rx+0xb8>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    0xe20(%rip),%rdx
mov    $0x1,%edi
call   1050 <error@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.sockaddr = type { i32 }
@__const.setup_rx.tv = %struct.timeval { i32 100000 }
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"socket r\00"
@.str.1 = [5 x i8] c"bind\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.2 = [23 x i8] c"setsockopt rcv timeout\00"
define i32 @setup_rx(%struct.sockaddr* %0, i32 %1) {
%3 = alloca %struct.sockaddr*
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %3
store i32 %1, i32* %4
%7 = bitcast %struct.timeval* %5 to i8*
%8 = load %struct.sockaddr*, %struct.sockaddr** %3
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %8, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i32 @error(i32 1, i32 %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %2
%19 = load i32, i32* %6
%20 = load %struct.sockaddr*, %struct.sockaddr** %3
%21 = load i32, i32* %4
%22 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* @errno
%26 = call i32 @error(i32 1, i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %18
%28 = load i32, i32* %6
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_RCVTIMEO
%31 = call i64 @setsockopt(i32 %28, i32 %29, i32 %30, %struct.timeval* %5, i32 4)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i32, i32* @errno
%35 = call i32 @error(i32 1, i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %33, %27
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"socket r\00"
@.str.1 = [5 x i8] c"bind\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.2 = [23 x i8] c"setsockopt rcv timeout\00"
define i32 @setup_rx(%struct.sockaddr* %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 100000, i32* %5
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = tail call i32 @error(i32 1, i32 %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %2
%15 = tail call i64 @bind(i32 %9, %struct.sockaddr* %0, i32 %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = tail call i32 @error(i32 1, i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVTIMEO
%23 = call i64 @setsockopt(i32 %9, i32 %21, i32 %22, %struct.timeval* %3, i32 4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = call i32 @error(i32 1, i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %25, %20
ret i32 %9
}