#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;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ set_nonblock(int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int wait_for_connection(int server_sockfd) {
  int client_sockfd, len;
  struct sockaddr_in peer;
  len = sizeof(struct sockaddr);
  if ((client_sockfd = accept(server_sockfd, (struct sockaddr *)&peer, &len)) <
      0) {
    if (errno == EAGAIN || errno == EWOULDBLOCK)
      return -1;
    else if (errno != EINTR) {
      fprintf(stderr, "Error creating client socket: %s\n", strerror(errno));
      return -1;
    }
  }
  set_nonblock(client_sockfd);
  return client_sockfd;
}


x86 assembly (clang, -O0):
<wait_for_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    120b <wait_for_connection+0xcb>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
je     11a2 <wait_for_connection+0x62>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    11ae <wait_for_connection+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <wait_for_connection+0xd9>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
je     1201 <wait_for_connection+0xc1>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe10(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <wait_for_connection+0xd9>
jmp    1206 <wait_for_connection+0xc6>
jmp    120b <wait_for_connection+0xcb>
mov    -0xc(%rbp),%edi
call   1040 <set_nonblock@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_connection>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1050 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1175 <wait_for_connection+0x35>
mov    %ebp,%edi
call   1040 <set_nonblock@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e5c(%rip),%rax
mov    (%rax),%rdi
mov    0x2e32(%rip),%rax
mov    $0xffffffff,%ebx
cmp    (%rax),%rdi
je     116c <wait_for_connection+0x2c>
mov    0x2e39(%rip),%rax
cmp    (%rax),%rdi
je     116c <wait_for_connection+0x2c>
mov    0x2e25(%rip),%rax
cmp    (%rax),%rdi
je     1163 <wait_for_connection+0x23>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebp
call   1060 <strerror@plt>
lea    0xe43(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
jmp    116c <wait_for_connection+0x2c>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [34 x i8] c"Error creating client socket: %s\0A\00"
define i32 @wait_for_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 4, i32* %5
%7 = load i32, i32* %3
%8 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%9 = call i32 @accept(i32 %7, %struct.sockaddr* %8, i32* %5)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %31
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i64, i64* @errno
%17 = load i64, i64* @EWOULDBLOCK
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15, %11
store i32 -1, i32* %2
br label %35
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i64, i64* @errno
%27 = call i8* @strerror(i64 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 -1, i32* %2
br label %35
29: ; preds = %20
br label %30
30: ; preds = %29
br label %31
31: ; preds = %30, %1
%32 = load i32, i32* %4
%33 = call i32 @set_nonblock(i32 %32)
%34 = load i32, i32* %4
store i32 %34, i32* %2
br label %35
35: ; preds = %31, %24, %19
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [34 x i8] c"Error creating client socket: %s\0A\00"
define i32 @wait_for_connection(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 4, i32* %2
%6 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %10, %11
%13 = load i64, i64* @EWOULDBLOCK
%14 = icmp eq i64 %10, %13
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %25, label %16
16: ; preds = %9
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %10, %17
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i8* @strerror(i64 %10)
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %25
23: ; preds = %16, %1
%24 = call i32 @set_nonblock(i32 %7)
br label %25
25: ; preds = %9, %23, %19
%26 = phi i32 [ -1, %19 ], [ %7, %23 ], [ -1, %9 ]
ret i32 %26
}