#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 */

/* Variables and functions */
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int do_accept(int fd) {
  int fda = fd;
  fd = accept(fda, NULL, NULL);
  if (fd == -1)
    error(1, errno, "accept");
  if (close(fda))
    error(1, errno, "close listen sock");
  return fd;
}


x86 assembly (clang, -O0):
<do_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1050 <accept@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1177 <do_accept+0x47>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe8e(%rip),%rdx
call   1040 <error@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     11a3 <do_accept+0x73>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe69(%rip),%rdx
call   1040 <error@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_accept>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %esi,%esi
xor    %edx,%edx
call   1050 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    115f <do_accept+0x2f>
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdx
mov    $0x1,%edi
call   1040 <error@plt>
mov    %ebp,%edi
call   1030 <close@plt>
test   %rax,%rax
je     1185 <do_accept+0x55>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdx
mov    $0x1,%edi
call   1040 <error@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [18 x i8] c"close listen sock\00"
define i32 @do_accept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @accept(i32 %5, i32* null, i32* null)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = call i32 @error(i32 1, i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %9, %1
%13 = load i32, i32* %3
%14 = call i64 @close(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = call i32 @error(i32 1, i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %16, %12
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [7 x i8] c"accept\00"
@.str.1 = [18 x i8] c"close listen sock\00"
define i32 @do_accept(i32 %0) {
%2 = tail call i32 @accept(i32 %0, i32* null, i32* null)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = tail call i32 @error(i32 1, i32 %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %4, %1
%8 = tail call i64 @close(i32 %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = tail call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %10, %7
ret i32 %2
}