#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_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  scalar_t__ revents;
  int /*<<< orphan*/ events;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ POLLIN;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setnonblocking(int);
int accept_timeout(int fd, struct sockaddr_in *peeraddr, int timeout) {
  printf("starting to accept\n");
  int ret = 0;
  setnonblocking(fd);
  socklen_t len = sizeof(struct sockaddr_in);
  struct pollfd fds[1];
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  fds[0].revents = 0;
  if (timeout > 0 || timeout == -1) {
    do {
      ret = poll(fds, 1, timeout);
    } while (ret < 0 && errno == EINTR);
    if (ret == 0) {
      errno = ETIMEDOUT;
      return -1;
    } else if (ret == -1)
      return -1;
  }
  if (peeraddr != NULL)
    ret = accept(fd, (struct sockaddr *)peeraddr, &len);
  else
    ret = accept(fd, NULL, NULL);
  if (ret == -1)
    do {
      perror("accept");
      exit(EXIT_FAILURE);
    } while (0);
  return ret;
}


x86 assembly (clang, -O0):
<accept_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    0xe87(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <setnonblocking@plt>
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11c2 <accept_timeout+0x62>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1259 <accept_timeout+0xf9>
jmp    11c7 <accept_timeout+0x67>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0x1,%esi
call   1040 <poll@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x41(%rbp)
jge    1204 <accept_timeout+0xa4>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11c7 <accept_timeout+0x67>
cmpl   $0x0,-0x18(%rbp)
jne    1239 <accept_timeout+0xd9>
mov    0x2da8(%rip),%rax
mov    (%rax),%rcx
mov    0x2dae(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <accept_timeout+0x15d>
cmpl   $0xffffffff,-0x18(%rbp)
jne    124f <accept_timeout+0xef>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bd <accept_timeout+0x15d>
jmp    1254 <accept_timeout+0xf4>
jmp    1259 <accept_timeout+0xf9>
cmpq   $0x0,-0x10(%rbp)
je     127c <accept_timeout+0x11c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x18(%rbp)
jmp    128e <accept_timeout+0x12e>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12b7 <accept_timeout+0x157>
jmp    129d <accept_timeout+0x13d>
lea    0xd70(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1050 <setnonblocking@plt>
movl   $0x4,0xc(%rsp)
mov    %r15d,0x10(%rsp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
test   %ebp,%ebp
jg     11b6 <accept_timeout+0x56>
cmp    $0xffffffff,%ebp
jne    120a <accept_timeout+0xaa>
lea    0x10(%rsp),%r13
mov    0x2e16(%rip),%r12
mov    0x2df7(%rip),%rbx
nopl   0x0(%rax)
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1040 <poll@plt>
test   %eax,%eax
jns    11ec <accept_timeout+0x8c>
mov    (%rbx),%rcx
cmp    %rcx,(%r12)
je     11d0 <accept_timeout+0x70>
cmp    $0xffffffff,%eax
je     122d <accept_timeout+0xcd>
test   %eax,%eax
jne    120a <accept_timeout+0xaa>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
mov    $0xffffffff,%eax
jmp    122d <accept_timeout+0xcd>
test   %r14,%r14
je     121c <accept_timeout+0xbc>
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
mov    %r14,%rsi
jmp    1223 <accept_timeout+0xc3>
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
cmp    $0xffffffff,%eax
je     123c <accept_timeout+0xdc>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc9(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.pollfd = type { i32, i64, i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"starting to accept\0A\00"
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
@.str.1 = [7 x i8] c"accept\00"
@EXIT_FAILURE = global i32 0
define i32 @accept_timeout(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1 x %struct.pollfd]
store i32 %0, i32* %5
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %6
store i32 %2, i32* %7
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %8
%12 = load i32, i32* %5
%13 = call i32 @setnonblocking(i32 %12)
store i32 4, i32* %9
%14 = load i32, i32* %5
%15 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* @POLLIN
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 2
store i32 %17, i32* %19
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 1
store i64 0, i64* %21
%22 = load i32, i32* %7
%23 = icmp sgt i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %3
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %52
27: ; preds = %24, %3
br label %28
28: ; preds = %39, %27
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %10, i64 0, i64 0
%30 = load i32, i32* %7
%31 = call i32 @poll(%struct.pollfd* %29, i32 1, i32 %30)
store i32 %31, i32* %8
br label %32
32: ; preds = %28
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br label %39
39: ; preds = %35, %32
%40 = phi i1 [ false, %32 ], [ %38, %35 ]
br i1 %40, label %28, label %41
41: ; preds = %39
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = load i64, i64* @ETIMEDOUT
store i64 %45, i64* @errno
store i32 -1, i32* %4
br label %74
46: ; preds = %41
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %46
store i32 -1, i32* %4
br label %74
50: ; preds = %46
br label %51
51: ; preds = %50
br label %52
52: ; preds = %51, %24
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%54 = icmp ne %struct.sockaddr_in* %53, null
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i32, i32* %5
%57 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%58 = bitcast %struct.sockaddr_in* %57 to %struct.sockaddr*
%59 = call i32 @accept(i32 %56, %struct.sockaddr* %58, i32* %9)
store i32 %59, i32* %8
br label %63
60: ; preds = %52
%61 = load i32, i32* %5
%62 = call i32 @accept(i32 %61, %struct.sockaddr* null, i32* null)
store i32 %62, i32* %8
br label %63
63: ; preds = %60, %55
%64 = load i32, i32* %8
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %72
66: ; preds = %63
br label %67
67: ; preds = %66
%68 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; No predecessors!
br label %72
72: ; preds = %71, %63
%73 = load i32, i32* %8
store i32 %73, i32* %4
br label %74
74: ; preds = %72, %49, %44
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.pollfd = type { i32, i64, i32 }
%struct.sockaddr = type { i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
@.str.1 = [7 x i8] c"accept\00"
@EXIT_FAILURE = global i32 0
@str = [19 x i8] c"starting to accept\00"
define i32 @accept_timeout(i32 %0, %struct.sockaddr_in* %1, i32 %2) {
%4 = alloca i32
%5 = alloca [1 x %struct.pollfd]
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @setnonblocking(i32 %0)
%8 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%9 = bitcast [1 x %struct.pollfd]* %5 to i8*
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0
%11 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 0
store i32 %0, i32* %11
%12 = load i32, i32* @POLLIN
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 2
store i32 %12, i32* %13
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %5, i64 0, i64 0, i32 1
store i64 0, i64* %14
%15 = icmp sgt i32 %2, 0
%16 = icmp eq i32 %2, -1
%17 = or i1 %15, %16
br i1 %17, label %18, label %28
18: ; preds = %3, %18
%19 = call i32 @poll(%struct.pollfd* %10, i32 1, i32 %2)
%20 = icmp slt i32 %19, 0
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
%24 = select i1 %20, i1 %23, i1 false
br i1 %24, label %18, label %25
25: ; preds = %18
switch i32 %19, label %28 [
i32 0, label %26
i32 -1, label %42
]
26: ; preds = %25
%27 = load i64, i64* @ETIMEDOUT
store i64 %27, i64* @errno
br label %42
28: ; preds = %25, %3
%29 = icmp eq %struct.sockaddr_in* %1, null
br i1 %29, label %33, label %30
30: ; preds = %28
%31 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%32 = call i32 @accept(i32 %0, %struct.sockaddr* %31, i32* %4)
br label %35
33: ; preds = %28
%34 = call i32 @accept(i32 %0, %struct.sockaddr* null, i32* null)
br label %35
35: ; preds = %33, %30
%36 = phi i32 [ %32, %30 ], [ %34, %33 ]
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %35, %25, %26
%43 = phi i32 [ -1, %26 ], [ %19, %25 ], [ %36, %35 ]
ret i32 %43
}