#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int listen(int, int const);
int tlisten_accept(int sockfd, const int BACKLOG) {
  int retfd;
  struct sockaddr_storage conn_addr;
  socklen_t conn_addr_size = sizeof conn_addr;
  if (listen(sockfd, BACKLOG) == -1) {
    return -1;
  }
  if ((retfd = accept(sockfd, (struct sockaddr *)&conn_addr,
                      &conn_addr_size)) == -1) {
    return -2;
  }
  return retfd;
}


x86 assembly (clang, -O0):
<tlisten_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <listen@plt>
cmp    $0xffffffff,%eax
jne    1155 <tlisten_accept+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1183 <tlisten_accept+0x63>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    117d <tlisten_accept+0x5d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1183 <tlisten_accept+0x63>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlisten_accept>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
call   1030 <listen@plt>
cmp    $0xffffffff,%eax
je     1159 <tlisten_accept+0x39>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1040 <accept@plt>
mov    %eax,%ecx
cmp    $0xffffffff,%eax
mov    $0xfffffffe,%eax
cmovne %ecx,%eax
jmp    115e <tlisten_accept+0x3e>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @tlisten_accept(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 4, i32* %8
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = call i32 @listen(i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %22
14: ; preds = %2
%15 = load i32, i32* %4
%16 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%17 = call i32 @accept(i32 %15, %struct.sockaddr* %16, i32* %8)
store i32 %17, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -2, i32* %3
br label %22
20: ; preds = %14
%21 = load i32, i32* %6
store i32 %21, i32* %3
br label %22
22: ; preds = %20, %19, %13
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @tlisten_accept(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = bitcast %struct.sockaddr_storage* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%7 = tail call i32 @listen(i32 %0, i32 %1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%11 = call i32 @accept(i32 %0, %struct.sockaddr* %10, i32* %4)
%12 = icmp eq i32 %11, -1
%13 = select i1 %12, i32 -2, i32 %11
br label %14
14: ; preds = %9, %2
%15 = phi i32 [ -1, %2 ], [ %13, %9 ]
ret i32 %15
}