#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*/ their_addr;
struct sockaddr_storage {
  int /*<<< orphan*/ ss_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ s;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int INET6_ADDRSTRLEN;
int O_NONBLOCK;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
void check_incoming_connection(int fd, int *new_fd) {
  if (*new_fd != -1)
    return;
  struct sockaddr_storage their_addr;
  socklen_t sin_size = sizeof(their_addr);
  char s[INET6_ADDRSTRLEN];
  *new_fd = accept(fd, (struct sockaddr *)&their_addr, &sin_size);
  if (*new_fd == -1) {
    if (errno != EAGAIN && errno != EWOULDBLOCK)
      perror("accept");
    return;
  }
  inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr),
            s, sizeof(s));
  printf("server: got connection from %s\n", s);
  int flags = fcntl(*new_fd, F_GETFL, 0);
  fcntl(*new_fd, F_SETFL, flags | O_NONBLOCK);
}


x86 assembly (clang, -O0):
<check_incoming_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
je     1181 <check_incoming_connection+0x21>
jmp    12a3 <check_incoming_connection+0x143>
movl   $0x4,-0x1c(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    1229 <check_incoming_connection+0xc9>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%rax
je     121d <check_incoming_connection+0xbd>
mov    0x2dca(%rip),%rax
mov    (%rax),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
je     121d <check_incoming_connection+0xbd>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x34(%rbp)
jmp    129c <check_incoming_connection+0x13c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <get_in_addr@plt>
mov    -0x4c(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%esi
mov    -0x48(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x40(%rbp),%rsi
lea    0xdb0(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%edx
mov    0x2d52(%rip),%rax
or     (%rax),%edx
call   1050 <fcntl@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_incoming_connection>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmpl   $0xffffffff,(%rsi)
jne    1239 <check_incoming_connection+0xd9>
mov    %rsi,%r14
mov    %rsp,%r13
movl   $0x4,-0x34(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
je     1214 <check_incoming_connection+0xb4>
mov    -0x30(%rbp),%r12d
lea    -0x30(%rbp),%rdi
call   1040 <get_in_addr@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <inet_ntop@plt>
lea    0xe2c(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    (%r14),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    (%r14),%edi
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%edx
call   1050 <fcntl@plt>
jmp    1236 <check_incoming_connection+0xd6>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%rax
je     1236 <check_incoming_connection+0xd6>
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
jne    1248 <check_incoming_connection+0xe8>
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb1(%rip),%rdi
call   1070 <perror@plt>
jmp    1236 <check_incoming_connection+0xd6>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [32 x i8] c"server: got connection from %s\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define void @check_incoming_connection(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%11 = load i32*, i32** %4
%12 = load i32, i32* %11
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
br label %60
15: ; preds = %2
store i32 4, i32* %6
%16 = load i32, i32* @INET6_ADDRSTRLEN
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %7
%19 = alloca i8, i64 %17
store i64 %17, i64* %8
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%22 = call i32 @accept(i32 %20, %struct.sockaddr* %21, i32* %6)
%23 = load i32*, i32** %4
store i32 %22, i32* %23
%24 = load i32*, i32** %4
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %38
27: ; preds = %15
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EWOULDBLOCK
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %35, %31, %27
store i32 1, i32* %9
br label %57
38: ; preds = %15
%39 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%42 = call i32 @get_in_addr(%struct.sockaddr* %41)
%43 = trunc i64 %17 to i32
%44 = call i32 @inet_ntop(i32 %40, i32 %42, i8* %19, i32 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%46 = load i32*, i32** %4
%47 = load i32, i32* %46
%48 = load i32, i32* @F_GETFL
%49 = call i32 @fcntl(i32 %47, i32 %48, i32 0)
store i32 %49, i32* %10
%50 = load i32*, i32** %4
%51 = load i32, i32* %50
%52 = load i32, i32* @F_SETFL
%53 = load i32, i32* %10
%54 = load i32, i32* @O_NONBLOCK
%55 = or i32 %53, %54
%56 = call i32 @fcntl(i32 %51, i32 %52, i32 %55)
store i32 0, i32* %9
br label %57
57: ; preds = %38, %37
%58 = load i8*, i8** %7
%59 = load i32, i32* %9
switch i32 %59, label %61 [
i32 0, label %60
i32 1, label %60
]
60: ; preds = %14, %57, %57
ret void
61: ; preds = %57
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [32 x i8] c"server: got connection from %s\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define void @check_incoming_connection(i32 %0, i32* nocapture %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %41
7: ; preds = %2
%8 = bitcast %struct.sockaddr_storage* %3 to i8*
%9 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%10 = load i32, i32* @INET6_ADDRSTRLEN
%11 = zext i32 %10 to i64
%12 = tail call i8* @llvm.stacksave()
%13 = alloca i8, i64 %11
%14 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%15 = call i32 @accept(i32 %0, %struct.sockaddr* %14, i32* %4)
store i32 %15, i32* %1
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %26
17: ; preds = %7
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
%21 = load i64, i64* @EWOULDBLOCK
%22 = icmp eq i64 %18, %21
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %40, label %24
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %40
26: ; preds = %7
%27 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @get_in_addr(%struct.sockaddr* %14)
%30 = call i32 @inet_ntop(i32 %28, i32 %29, i8* %13, i32 %10)
%31 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%32 = load i32, i32* %1
%33 = load i32, i32* @F_GETFL
%34 = call i32 @fcntl(i32 %32, i32 %33, i32 0)
%35 = load i32, i32* %1
%36 = load i32, i32* @F_SETFL
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %37, %34
%39 = call i32 @fcntl(i32 %35, i32 %36, i32 %38)
br label %40
40: ; preds = %17, %24, %26
br label %41
41: ; preds = %2, %40
ret void
}