#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct linger {
  scalar_t__ l_linger;
  scalar_t__ l_onoff;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ SO_LINGER;
int /*<<< orphan*/ SO_OOBINLINE;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
scalar_t__ ntohs(int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int lw_sock_accept(int *new_sock, char *ip, int *port, int listening_sock,
                   int wait) {
  int result = 0;
  struct sockaddr_in name;
  socklen_t namelen = sizeof(struct sockaddr_in);
  fd_set read;
  struct timeval tv;
  int res;
  int enable = 1;
  int disable = 0;
  struct linger li;
  *new_sock = -1;
  ip[0] = '\0';
  *port = 0;
  if (listening_sock >= 0) {
    FD_ZERO(&read);
    FD_SET(listening_sock, &read);
    if (wait) {
      tv.tv_sec = 1;
      tv.tv_usec = 0;
    } else {
      tv.tv_sec = 0;
      tv.tv_usec = 0;
    }
    res = select(listening_sock + 1, &read, NULL, NULL, &tv);
    if (res >= 1) {
      (*new_sock) = accept(listening_sock, (struct sockaddr *)&name, &namelen);
      if ((*new_sock) >= 0) {
        li.l_onoff = 0;
        li.l_linger = 0;
        setsockopt(*new_sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&enable,
                   sizeof(int));
        setsockopt(*new_sock, SOL_SOCKET, SO_OOBINLINE, (char *)&disable,
                   sizeof(int));
        setsockopt(*new_sock, SOL_SOCKET, SO_LINGER, (char *)&li,
                   sizeof(struct linger));
        fcntl(*new_sock, F_SETFL, O_NONBLOCK, 0);
        strncpy(ip, inet_ntoa(name.sin_addr), 16 - 1);
        ip[16 - 1] = 0;
        (*port) = (int)ntohs(name.sin_port);
        result = 1;
      }
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<lw_sock_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x8,-0x34(%rbp)
movl   $0x1,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jl     1367 <lw_sock_accept+0x1d7>
lea    -0x38(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <FD_SET@plt>
cmpl   $0x0,-0x20(%rbp)
je     121f <lw_sock_accept+0x8f>
movl   $0x1,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
jmp    122e <lw_sock_accept+0x9e>
movl   $0x0,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x1,-0x4c(%rbp)
jl     1362 <lw_sock_accept+0x1d2>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jl     135d <lw_sock_accept+0x1cd>
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    0x2d10(%rip),%rax
mov    (%rax),%edx
lea    -0x50(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
mov    0x2cea(%rip),%rax
mov    (%rax),%edx
lea    -0x68(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
mov    0x2ccb(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1050 <fcntl@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <inet_ntoa@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%esi
mov    $0xf,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0xf(%rax)
mov    -0x30(%rbp),%edi
call   1090 <ntohs@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x24(%rbp)
jmp    1362 <lw_sock_accept+0x1d2>
jmp    1367 <lw_sock_accept+0x1d7>
mov    -0x24(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lw_sock_accept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
movl   $0x8,0x14(%rsp)
movl   $0x1,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0xffffffff,(%rdi)
movb   $0x0,(%rsi)
movl   $0x0,(%rdx)
test   %ecx,%ecx
js     1300 <lw_sock_accept+0x170>
mov    %r8d,%ebp
mov    %ecx,%ebx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rdx,0x20(%rsp)
lea    0x2c(%rsp),%r14
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
xor    %eax,%eax
test   %ebp,%ebp
setne  %al
mov    %eax,0x30(%rsp)
movq   $0x0,0x38(%rsp)
lea    0x1(%rbx),%edi
xor    %r13d,%r13d
lea    0x30(%rsp),%r8
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %eax,%eax
jle    1303 <lw_sock_accept+0x173>
lea    0x18(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <accept@plt>
mov    %eax,(%r12)
test   %eax,%eax
js     1303 <lw_sock_accept+0x173>
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
mov    0x2d95(%rip),%rbx
mov    (%rbx),%esi
mov    0x2d54(%rip),%rcx
mov    (%rcx),%edx
lea    0x10(%rsp),%rcx
mov    %eax,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    (%r12),%edi
mov    (%rbx),%esi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    (%r12),%edi
mov    (%rbx),%esi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
lea    0x40(%rsp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    (%r12),%edi
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1050 <fcntl@plt>
mov    0x1c(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    $0xf,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xf(%r15)
mov    0x18(%rsp),%edi
call   1090 <ntohs@plt>
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx)
mov    $0x1,%r13d
jmp    1303 <lw_sock_accept+0x173>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.linger = type { i64, i64 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_OOBINLINE = global i32 0
@SO_LINGER = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @lw_sock_accept(i32* %0, i8* %1, i32* %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.timeval
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca %struct.linger
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %11
store i32 8, i32* %13
store i32 1, i32* %17
store i32 0, i32* %18
%20 = load i32*, i32** %6
store i32 -1, i32* %20
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 0
store i8 0, i8* %22
%23 = load i32*, i32** %8
store i32 0, i32* %23
%24 = load i32, i32* %9
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %92
26: ; preds = %5
%27 = call i32 @FD_ZERO(i32* %14)
%28 = load i32, i32* %9
%29 = call i32 @FD_SET(i32 %28, i32* %14)
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i32 1, i32* %33
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %34
br label %38
35: ; preds = %26
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i32 0, i32* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %37
br label %38
38: ; preds = %35, %32
%39 = load i32, i32* %9
%40 = add nsw i32 %39, 1
%41 = call i32 @select(i32 %40, i32* %14, i32* null, i32* null, %struct.timeval* %15)
store i32 %41, i32* %16
%42 = load i32, i32* %16
%43 = icmp sge i32 %42, 1
br i1 %43, label %44, label %91
44: ; preds = %38
%45 = load i32, i32* %9
%46 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %13)
%48 = load i32*, i32** %6
store i32 %47, i32* %48
%49 = load i32*, i32** %6
%50 = load i32, i32* %49
%51 = icmp sge i32 %50, 0
br i1 %51, label %52, label %90
52: ; preds = %44
%53 = getelementptr inbounds %struct.linger, %struct.linger* %19, i32 0, i32 1
store i64 0, i64* %53
%54 = getelementptr inbounds %struct.linger, %struct.linger* %19, i32 0, i32 0
store i64 0, i64* %54
%55 = load i32*, i32** %6
%56 = load i32, i32* %55
%57 = load i32, i32* @SOL_SOCKET
%58 = load i32, i32* @SO_KEEPALIVE
%59 = bitcast i32* %17 to i8*
%60 = call i32 @setsockopt(i32 %56, i32 %57, i32 %58, i8* %59, i32 4)
%61 = load i32*, i32** %6
%62 = load i32, i32* %61
%63 = load i32, i32* @SOL_SOCKET
%64 = load i32, i32* @SO_OOBINLINE
%65 = bitcast i32* %18 to i8*
%66 = call i32 @setsockopt(i32 %62, i32 %63, i32 %64, i8* %65, i32 4)
%67 = load i32*, i32** %6
%68 = load i32, i32* %67
%69 = load i32, i32* @SOL_SOCKET
%70 = load i32, i32* @SO_LINGER
%71 = bitcast %struct.linger* %19 to i8*
%72 = call i32 @setsockopt(i32 %68, i32 %69, i32 %70, i8* %71, i32 16)
%73 = load i32*, i32** %6
%74 = load i32, i32* %73
%75 = load i32, i32* @F_SETFL
%76 = load i32, i32* @O_NONBLOCK
%77 = call i32 @fcntl(i32 %74, i32 %75, i32 %76, i32 0)
%78 = load i8*, i8** %7
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%80 = load i32, i32* %79
%81 = call i32 @inet_ntoa(i32 %80)
%82 = call i32 @strncpy(i8* %78, i32 %81, i32 15)
%83 = load i8*, i8** %7
%84 = getelementptr inbounds i8, i8* %83, i64 15
store i8 0, i8* %84
%85 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%86 = load i32, i32* %85
%87 = call i64 @ntohs(i32 %86)
%88 = trunc i64 %87 to i32
%89 = load i32*, i32** %8
store i32 %88, i32* %89
store i32 1, i32* %11
br label %90
90: ; preds = %52, %44
br label %91
91: ; preds = %90, %38
br label %92
92: ; preds = %91, %5
%93 = load i32, i32* %11
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.linger = type { i64, i64 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@SO_OOBINLINE = global i32 0
@SO_LINGER = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @lw_sock_accept(i32* nocapture %0, i8* %1, i32* nocapture writeonly %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.linger
%13 = bitcast %struct.sockaddr_in* %6 to i8*
%14 = bitcast i32* %7 to i8*
store i32 8, i32* %7
%15 = bitcast i32* %8 to i8*
%16 = bitcast %struct.timeval* %9 to i8*
%17 = bitcast i32* %10 to i8*
store i32 1, i32* %10
%18 = bitcast i32* %11 to i8*
store i32 0, i32* %11
%19 = bitcast %struct.linger* %12 to i8*
store i32 -1, i32* %0
store i8 0, i8* %1
store i32 0, i32* %2
%20 = icmp sgt i32 %3, -1
br i1 %20, label %21, label %61
21: ; preds = %5
%22 = call i32 @FD_ZERO(i32* %8)
%23 = call i32 @FD_SET(i32 %3, i32* %8)
%24 = icmp ne i32 %4, 0
%25 = zext i1 %24 to i32
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 1
store i64 0, i64* %27
%28 = add nuw nsw i32 %3, 1
%29 = call i32 @select(i32 %28, i32* %8, i32* null, i32* null, %struct.timeval* %9)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %61
31: ; preds = %21
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i32 @accept(i32 %3, %struct.sockaddr* %32, i32* %7)
store i32 %33, i32* %0
%34 = icmp sgt i32 %33, -1
br i1 %34, label %35, label %61
35: ; preds = %31
%36 = bitcast %struct.linger* %12 to i8*
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_KEEPALIVE
%39 = call i32 @setsockopt(i32 %33, i32 %37, i32 %38, i8* %17, i32 4)
%40 = load i32, i32* %0
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_OOBINLINE
%43 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i8* %18, i32 4)
%44 = load i32, i32* %0
%45 = load i32, i32* @SOL_SOCKET
%46 = load i32, i32* @SO_LINGER
%47 = call i32 @setsockopt(i32 %44, i32 %45, i32 %46, i8* %19, i32 16)
%48 = load i32, i32* %0
%49 = load i32, i32* @F_SETFL
%50 = load i32, i32* @O_NONBLOCK
%51 = call i32 @fcntl(i32 %48, i32 %49, i32 %50, i32 0)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%53 = load i32, i32* %52
%54 = call i32 @inet_ntoa(i32 %53)
%55 = call i32 @strncpy(i8* %1, i32 %54, i32 15)
%56 = getelementptr inbounds i8, i8* %1, i64 15
store i8 0, i8* %56
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @ntohs(i32 %58)
%60 = trunc i64 %59 to i32
store i32 %60, i32* %2
br label %61
61: ; preds = %21, %35, %31, %5
%62 = phi i32 [ 1, %35 ], [ 0, %31 ], [ 0, %21 ], [ 0, %5 ]
ret i32 %62
}