#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*/ timeout;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int POLLERR;
int POLLIN;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int /*<<< orphan*/ stderr;
int get_client(int listen_socket, int *out_client_socket) {
  struct pollfd pfd;
  pfd.fd = listen_socket;
  pfd.events = POLLIN | POLLERR;
  int ret = poll(&pfd, 1, (5000));
  if (ret < 0) {
    perror("poll(listenfd)");
    return ret;
  }
  if (ret == 0) {
    fprintf(stderr, "poll timed out for listen");
    return -1;
  }
  if (pfd.revents != POLLIN) {
    fprintf(stderr, "poll gave unexpected revents for listen %hd", pfd.revents);
    return -1;
  }
  int client_socket = accept(listen_socket, NULL, NULL);
  if (client_socket < 0) {
    perror("accept");
    return client_socket;
  }
  *out_client_socket = client_socket;
  struct timeval timeout;
  timeout.tv_sec = (20);
  timeout.tv_usec = 0;
  ret = setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout,
                   sizeof(timeout));
  if (ret != 0) {
    perror("setsockopt(SO_RCVTIMEO)");
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    0x2e6b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b1 <get_client+0x61>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b0 <get_client+0x160>
cmpl   $0x0,-0x24(%rbp)
jne    11de <get_client+0x8e>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <get_client+0x160>
mov    -0x18(%rbp),%eax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%eax
je     1216 <get_client+0xc6>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <get_client+0x160>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1249 <get_client+0xf9>
lea    0xe1c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b0 <get_client+0x160>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x14,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a9 <get_client+0x159>
lea    0xdc3(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b0 <get_client+0x160>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_client>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edi,0x8(%rsp)
mov    0x2e6e(%rip),%r15
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
or     (%r15),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
test   %eax,%eax
js     1239 <get_client+0xe9>
je     11fd <get_client+0xad>
mov    0x10(%rsp),%edx
cmp    (%r15),%edx
jne    121b <get_client+0xcb>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1244 <get_client+0xf4>
mov    %ebx,(%r14)
movl   $0x14,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1250 <get_client+0x100>
lea    0xe61(%rip),%rdi
jmp    124b <get_client+0xfb>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1250 <get_client+0x100>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1250 <get_client+0x100>
mov    %eax,%ebx
lea    0xdbe(%rip),%rdi
jmp    124b <get_client+0xfb>
lea    0xe0a(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
@POLLIN = global i32 0
@POLLERR = global i32 0
@.str = [15 x i8] c"poll(listenfd)\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"poll timed out for listen\00"
@.str.2 = [44 x i8] c"poll gave unexpected revents for listen %hd\00"
@.str.3 = [7 x i8] c"accept\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.4 = [24 x i8] c"setsockopt(SO_RCVTIMEO)\00"
define i32 @get_client(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.pollfd
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i32 %0, i32* %4
store i32* %1, i32** %5
%10 = load i32, i32* %4
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @POLLIN
%13 = load i32, i32* @POLLERR
%14 = or i32 %12, %13
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 5000)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %7
store i32 %21, i32* %3
br label %61
22: ; preds = %2
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
28: ; preds = %22
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
%30 = load i32, i32* %29
%31 = load i32, i32* @POLLIN
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %36)
store i32 -1, i32* %3
br label %61
38: ; preds = %28
%39 = load i32, i32* %4
%40 = call i32 @accept(i32 %39, i32* null, i32* null)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* %8
store i32 %45, i32* %3
br label %61
46: ; preds = %38
%47 = load i32, i32* %8
%48 = load i32*, i32** %5
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 20, i32* %49
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %50
%51 = load i32, i32* %8
%52 = load i32, i32* @SOL_SOCKET
%53 = load i32, i32* @SO_RCVTIMEO
%54 = call i32 @setsockopt(i32 %51, i32 %52, i32 %53, %struct.timeval* %9, i32 16)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %46
%58 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32, i32* %7
store i32 %59, i32* %3
br label %61
60: ; preds = %46
store i32 0, i32* %3
br label %61
61: ; preds = %60, %57, %43, %33, %25, %19
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
@POLLIN = global i32 0
@POLLERR = global i32 0
@.str = [15 x i8] c"poll(listenfd)\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"poll timed out for listen\00"
@.str.2 = [44 x i8] c"poll gave unexpected revents for listen %hd\00"
@.str.3 = [7 x i8] c"accept\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str.4 = [24 x i8] c"setsockopt(SO_RCVTIMEO)\00"
define i32 @get_client(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.pollfd
%4 = alloca %struct.timeval
%5 = bitcast %struct.pollfd* %3 to i8*
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %0, i32* %6
%7 = load i32, i32* @POLLIN
%8 = load i32, i32* @POLLERR
%9 = or i32 %8, %7
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 5000)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %44
15: ; preds = %2
%16 = icmp eq i32 %11, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %44
20: ; preds = %15
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
%22 = load i32, i32* %21
%23 = load i32, i32* @POLLIN
%24 = icmp eq i32 %22, %23
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %44
28: ; preds = %20
%29 = call i32 @accept(i32 %0, i32* null, i32* null)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %44
33: ; preds = %28
store i32 %29, i32* %1
%34 = bitcast %struct.timeval* %4 to i8*
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 20, i32* %35
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %36
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_RCVTIMEO
%39 = call i32 @setsockopt(i32 %29, i32 %37, i32 %38, %struct.timeval* %4, i32 16)
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %33, %41
br label %44
44: ; preds = %31, %43, %25, %17, %13
%45 = phi i32 [ %11, %13 ], [ -1, %17 ], [ -1, %25 ], [ %29, %31 ], [ %39, %43 ]
ret i32 %45
}