#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 pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int POLLERR;
int POLLIN;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int set_socket_timeout(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;
  ret = set_socket_timeout(client_socket);
  if (ret != 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    0x2e73(%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    127a <get_client+0x12a>
cmpl   $0x0,-0x24(%rbp)
jne    11de <get_client+0x8e>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <get_client+0x12a>
mov    -0x18(%rbp),%eax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
je     1216 <get_client+0xc6>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <get_client+0x12a>
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    127a <get_client+0x12a>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
call   1040 <set_socket_timeout@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1273 <get_client+0x123>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <get_client+0x12a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_client>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    %edi,0x8(%rsp)
mov    0x2e76(%rip),%r15
mov    0x2e77(%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     1200 <get_client+0xb0>
je     11be <get_client+0x6e>
mov    0x10(%rsp),%edx
cmp    (%r15),%edx
jne    11d7 <get_client+0x87>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1210 <get_client+0xc0>
mov    %ebp,(%r14)
mov    %ebp,%edi
call   1040 <set_socket_timeout@plt>
neg    %eax
sbb    %ebp,%ebp
jmp    11f3 <get_client+0xa3>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11ee <get_client+0x9e>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xdf7(%rip),%rdi
call   1060 <perror@plt>
jmp    11f3 <get_client+0xa3>
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
jmp    11f3 <get_client+0xa3>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@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"
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
store i32 %0, i32* %4
store i32* %1, i32** %5
%9 = load i32, i32* %4
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @POLLIN
%12 = load i32, i32* @POLLERR
%13 = or i32 %11, %12
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 1
store i32 %13, i32* %14
%15 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 5000)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %7
store i32 %20, i32* %3
br label %54
21: ; preds = %2
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
27: ; preds = %21
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
%29 = load i32, i32* %28
%30 = load i32, i32* @POLLIN
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %35)
store i32 -1, i32* %3
br label %54
37: ; preds = %27
%38 = load i32, i32* %4
%39 = call i32 @accept(i32 %38, i32* null, i32* null)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %8
store i32 %44, i32* %3
br label %54
45: ; preds = %37
%46 = load i32, i32* %8
%47 = load i32*, i32** %5
store i32 %46, i32* %47
%48 = load i32, i32* %8
%49 = call i32 @set_socket_timeout(i32 %48)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %45
store i32 -1, i32* %3
br label %54
53: ; preds = %45
store i32 0, i32* %3
br label %54
54: ; preds = %53, %52, %42, %32, %24, %18
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@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"
define i32 @get_client(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.pollfd
%4 = bitcast %struct.pollfd* %3 to i8*
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %0, i32* %5
%6 = load i32, i32* @POLLIN
%7 = load i32, i32* @POLLERR
%8 = or i32 %7, %6
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 5000)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %36
14: ; preds = %2
%15 = icmp eq i32 %10, 0
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %36
19: ; preds = %14
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
%21 = load i32, i32* %20
%22 = load i32, i32* @POLLIN
%23 = icmp eq i32 %21, %22
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %36
27: ; preds = %19
%28 = call i32 @accept(i32 %0, i32* null, i32* null)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %36
32: ; preds = %27
store i32 %28, i32* %1
%33 = call i32 @set_socket_timeout(i32 %28)
%34 = icmp ne i32 %33, 0
%35 = sext i1 %34 to i32
br label %36
36: ; preds = %30, %32, %24, %16, %12
%37 = phi i32 [ %10, %12 ], [ -1, %16 ], [ -1, %24 ], [ %28, %30 ], [ %35, %32 ]
ret i32 %37
}