#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 {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sbuf;
typedef int /*<<< orphan*/ in_addr;
typedef int /*<<< orphan*/ hbuf;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int NI_MAXHOST;
int NI_MAXSERV;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int getnameinfo(struct sockaddr *, int, char *, int, char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, char *, char *);
int get_client_fd(int sfd) {
  struct sockaddr in_addr;
  socklen_t in_len = sizeof(in_addr);
  char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
  int infd;
  infd = accept(sfd, &in_addr, &in_len);
  if (infd == -1) {
    if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
      perror("accept");
    }
    return -1;
  }
  int s;
  extern int user;
  s = getnameinfo(&in_addr, in_len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
                  NI_NUMERICHOST | NI_NUMERICSERV);
  if (s == 0)
    printf("Accepted %d connection on descriptor %d"
           "(host=%s, port=%s)\n",
           ++user, infd, hbuf, sbuf);
  return infd;
}


x86 assembly (clang, -O0):
<get_client_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    121b <get_client_fd+0xdb>
mov    0x2df9(%rip),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
je     1208 <get_client_fd+0xc8>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%rax
je     1208 <get_client_fd+0xc8>
lea    0xdfd(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    129d <get_client_fd+0x15d>
mov    -0x48(%rbp),%r8
mov    -0x58(%rbp),%rdx
mov    -0x50(%rbp),%rax
mov    -0x60(%rbp),%rcx
mov    -0x14(%rbp),%esi
mov    %eax,%r9d
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    0x2da7(%rip),%rdi
or     (%rdi),%eax
lea    -0x10(%rbp),%rdi
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    1290 <get_client_fd+0x150>
mov    -0x48(%rbp),%r8
mov    -0x58(%rbp),%rcx
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    0x2d39(%rip),%rax
mov    %esi,(%rax)
mov    -0x34(%rbp),%edx
lea    0xd7c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_client_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,-0x38(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r15
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1204 <get_client_fd+0xc4>
mov    -0x2c(%rbp),%esi
mov    0x2e1e(%rip),%rdx
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%eax
or     (%rdx),%eax
sub    $0x8,%rsp
lea    -0x30(%rbp),%rdi
mov    %r15,%rdx
mov    %r12d,%ecx
mov    %rbx,%r8
mov    %r13d,%r9d
push   %rax
call   1040 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
jne    1226 <get_client_fd+0xe6>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    %esi,(%rax)
lea    0xe13(%rip),%rdi
mov    %r14d,%edx
mov    %r15,%rcx
mov    %rbx,%r8
call   1030 <printf@plt>
jmp    1226 <get_client_fd+0xe6>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
je     1226 <get_client_fd+0xe6>
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
jne    123c <get_client_fd+0xfc>
mov    -0x38(%rbp),%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbd(%rip),%rdi
call   1050 <perror@plt>
jmp    1226 <get_client_fd+0xe6>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [7 x i8] c"accept\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.1 = [59 x i8] c"Accepted %d connection on descriptor %d(host=%s, port=%s)\0A\00"
@user = external global i32
define i32 @get_client_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %5
%12 = load i32, i32* @NI_MAXHOST
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i32, i32* @NI_MAXSERV
%17 = zext i32 %16 to i64
%18 = alloca i8, i64 %17
store i64 %17, i64* %8
%19 = load i32, i32* %3
%20 = call i32 @accept(i32 %19, %struct.sockaddr* %4, i32* %5)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %34
23: ; preds = %1
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @EWOULDBLOCK
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %27, %23
store i32 -1, i32* %2
store i32 1, i32* %10
br label %51
34: ; preds = %1
%35 = load i32, i32* %5
%36 = trunc i64 %13 to i32
%37 = trunc i64 %17 to i32
%38 = load i32, i32* @NI_NUMERICHOST
%39 = load i32, i32* @NI_NUMERICSERV
%40 = or i32 %38, %39
%41 = call i32 @getnameinfo(%struct.sockaddr* %4, i32 %35, i8* %15, i32 %36, i8* %18, i32 %37, i32 %40)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %34
%45 = load i32, i32* @user
%46 = add nsw i32 %45, 1
store i32 %46, i32* @user
%47 = load i32, i32* %9
%48 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %46, i32 %47, i8* %15, i8* %18)
br label %49
49: ; preds = %44, %34
%50 = load i32, i32* %9
store i32 %50, i32* %2
store i32 1, i32* %10
br label %51
51: ; preds = %49, %33
%52 = load i8*, i8** %6
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [7 x i8] c"accept\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.1 = [59 x i8] c"Accepted %d connection on descriptor %d(host=%s, port=%s)\0A\00"
@user = external global i32
define i32 @get_client_fd(i32 %0) {
%2 = alloca %struct.sockaddr
%3 = alloca i32
%4 = bitcast %struct.sockaddr* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = load i32, i32* @NI_MAXHOST
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = load i32, i32* @NI_MAXSERV
%11 = zext i32 %10 to i64
%12 = alloca i8, i64 %11
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %2, i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %16, %17
%19 = load i64, i64* @EWOULDBLOCK
%20 = icmp eq i64 %16, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %35, label %22
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %35
24: ; preds = %1
%25 = load i32, i32* %3
%26 = load i32, i32* @NI_NUMERICHOST
%27 = load i32, i32* @NI_NUMERICSERV
%28 = or i32 %27, %26
%29 = call i32 @getnameinfo(%struct.sockaddr* %2, i32 %25, i8* %9, i32 %6, i8* %12, i32 %10, i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i32, i32* @user
%33 = add nsw i32 %32, 1
store i32 %33, i32* @user
%34 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %13, i8* %9, i8* %12)
br label %35
35: ; preds = %24, %31, %15, %22
ret i32 %13
}