#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_usec;
  int /*<<< orphan*/ member_0;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int wait_connection(int sockfd) {
  int new_sockfd = -1;
  if (sockfd != -1) {
    int clilen;
    struct sockaddr_in cli_addr;
    listen(sockfd, 10);
    clilen = sizeof(cli_addr);
    new_sockfd =
        accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
    if (new_sockfd != -1) {
      struct timeval tv = {0};
      tv.tv_usec = 100;
      setsockopt(new_sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
                 sizeof(struct timeval));
      printf("New connection incoming: %s:%d\n", inet_ntoa(cli_addr.sin_addr),
             ntohs(cli_addr.sin_port));
    }
  }
  return new_sockfd;
}


x86 assembly (clang, -O0):
<wait_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     1223 <wait_connection+0xb3>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
movl   $0x8,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     121e <wait_connection+0xae>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
movl   $0x64,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xde7(%rip),%rdi
call   1050 <printf@plt>
jmp    1223 <wait_connection+0xb3>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_connection>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0xffffffff,%edi
je     11f1 <wait_connection+0x91>
mov    %edi,%ebx
mov    $0xa,%esi
call   1060 <listen@plt>
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1080 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11f6 <wait_connection+0x96>
movq   $0x64,0x10(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x8(%rsp),%edi
call   1070 <ntohs@plt>
lea    0xe1b(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
jmp    11f6 <wait_connection+0x96>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [32 x i8] c"New connection incoming: %s:%d\0A\00"
define i32 @wait_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.timeval
store i32 %0, i32* %2
store i32 -1, i32* %3
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, -1
br i1 %8, label %9, label %33
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i32 @listen(i32 %10, i32 10)
store i32 8, i32* %4
%12 = load i32, i32* %2
%13 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%14 = call i32 @accept(i32 %12, %struct.sockaddr* %13, i32* %4)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %32
17: ; preds = %9
%18 = bitcast %struct.timeval* %6 to i8*
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 100, i32* %19
%20 = load i32, i32* %3
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVTIMEO
%23 = bitcast %struct.timeval* %6 to i8*
%24 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i8* %23, i32 8)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%26 = load i32, i32* %25
%27 = call i8* @inet_ntoa(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
%31 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %27, i32 %30)
br label %32
32: ; preds = %17, %9
br label %33
33: ; preds = %32, %1
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [32 x i8] c"New connection incoming: %s:%d\0A\00"
define i32 @wait_connection(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = icmp eq i32 %0, -1
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = bitcast i32* %2 to i8*
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = tail call i32 @listen(i32 %0, i32 10)
store i32 8, i32* %2
%10 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%11 = call i32 @accept(i32 %0, %struct.sockaddr* %10, i32* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %25, label %13
13: ; preds = %6
%14 = bitcast i64* %4 to i8*
store i64 100, i64* %4
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_RCVTIMEO
%17 = call i32 @setsockopt(i32 %11, i32 %15, i32 %16, i8* %14, i32 8)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%19 = load i32, i32* %18
%20 = call i8* @inet_ntoa(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @ntohs(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %23)
br label %25
25: ; preds = %13, %6
br label %26
26: ; preds = %25, %1
%27 = phi i32 [ %11, %25 ], [ -1, %1 ]
ret i32 %27
}