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

/* Variables and functions */
int POLLIN;
int POLLOUT;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ lcr_uis_dispatch(int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ uis_lcr_bind(int *);
void *lcr_uis_server(void *data) {
  struct pollfd ufds[2];
  struct sockaddr_un un_addr;
  socklen_t addrlen;
  int nfds = 1;
  int res;
  uis_lcr_bind(&ufds[0].fd);
  printf("UIS server thread started %d\n", ufds[0].fd);
  ufds[0].events = POLLIN;
  ufds[1].events = POLLOUT;
  for (;;) {
    res = poll(ufds, nfds, -1);
    if (nfds == 1 && ufds[0].revents & POLLIN) {
      ufds[1].fd = accept(ufds[0].fd, (struct sockaddr *)&un_addr, &addrlen);
      nfds = 2;
    }
    if (ufds[0].revents & POLLIN) {
      lcr_uis_dispatch(ufds[1].fd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<lcr_uis_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <uis_lcr_bind@plt>
mov    -0x20(%rbp),%esi
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x30(%rbp)
jne    11e0 <lcr_uis_server+0x90>
mov    -0x18(%rbp),%eax
mov    0x2e25(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11e0 <lcr_uis_server+0x90>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x14(%rbp)
movl   $0x2,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2df6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11fd <lcr_uis_server+0xad>
mov    -0x14(%rbp),%edi
call   1030 <lcr_uis_dispatch@plt>
jmp    1193 <lcr_uis_server+0x43>

x86 assembly (clang, -O2):
<lcr_uis_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsp,%r12
mov    %r12,%rdi
call   1050 <uis_lcr_bind@plt>
mov    (%rsp),%esi
lea    0xe8d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e61(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x1,%ebx
lea    0x20(%rsp),%r14
lea    0x24(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%ebp
mov    %r12,%rdi
mov    %ebx,%esi
mov    $0xffffffff,%edx
call   1060 <poll@plt>
mov    $0x2,%ebx
cmp    $0x1,%ebp
jne    11f0 <lcr_uis_server+0xa0>
mov    0x0(%r13),%eax
test   %eax,0x8(%rsp)
je     11e9 <lcr_uis_server+0x99>
mov    (%rsp),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <accept@plt>
mov    %eax,0xc(%rsp)
jmp    11f0 <lcr_uis_server+0xa0>
mov    $0x1,%ebx
xchg   %ax,%ax
mov    0x0(%r13),%eax
test   %eax,0x8(%rsp)
je     11b0 <lcr_uis_server+0x60>
mov    0xc(%rsp),%edi
call   1030 <lcr_uis_dispatch@plt>
jmp    11b0 <lcr_uis_server+0x60>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"UIS server thread started %d\0A\00"
@POLLIN = global i32 0
@POLLOUT = global i32 0
define i8* @lcr_uis_server(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x %struct.pollfd]
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i32 1, i32* %6
%8 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 0
%10 = call i32 @uis_lcr_bind(i32* %9)
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @POLLIN
%16 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load i32, i32* @POLLOUT
%19 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %19, i32 0, i32 1
store i32 %18, i32* %20
br label %21
21: ; preds = %54, %1
%22 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%23 = load i32, i32* %6
%24 = call i32 @poll(%struct.pollfd* %22, i32 %23, i32 -1)
store i32 %24, i32* %7
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %42
27: ; preds = %21
%28 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 2
%30 = load i32, i32* %29
%31 = load i32, i32* @POLLIN
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %27
%35 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%36 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %5)
%40 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1
%41 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %40, i32 0, i32 0
store i32 %39, i32* %41
store i32 2, i32* %6
br label %42
42: ; preds = %34, %27, %21
%43 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 0
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = load i32, i32* @POLLIN
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %3, i64 0, i64 1
%51 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @lcr_uis_dispatch(i32 %52)
br label %54
54: ; preds = %49, %42
br label %21
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"UIS server thread started %d\0A\00"
@POLLIN = global i32 0
@POLLOUT = global i32 0
define noalias i8* @lcr_uis_server(i8* nocapture readnone %0) {
%2 = alloca [2 x %struct.pollfd]
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = bitcast [2 x %struct.pollfd]* %2 to i8*
%6 = bitcast %struct.sockaddr_un* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%9 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
%10 = call i32 @uis_lcr_bind(i32* %9)
%11 = load i32, i32* %9
%12 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @POLLOUT
%16 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 2
%18 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%19 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 0
br label %20
20: ; preds = %41, %1
%21 = phi i32 [ 1, %1 ], [ %33, %41 ]
%22 = call i32 @poll(%struct.pollfd* %8, i32 %21, i32 -1)
%23 = icmp eq i32 %21, 1
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* %17
%26 = load i32, i32* @POLLIN
%27 = and i32 %26, %25
%28 = icmp eq i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = load i32, i32* %9
%31 = call i32 @accept(i32 %30, %struct.sockaddr* %18, i32* %4)
store i32 %31, i32* %19
br label %32
32: ; preds = %29, %24, %20
%33 = phi i32 [ 2, %29 ], [ 1, %24 ], [ 2, %20 ]
%34 = load i32, i32* %17
%35 = load i32, i32* @POLLIN
%36 = and i32 %35, %34
%37 = icmp eq i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %32
%39 = load i32, i32* %19
%40 = call i32 @lcr_uis_dispatch(i32 %39)
br label %41
41: ; preds = %38, %32
br label %20
}