#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;
typedef int /*<<< orphan*/ serv;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ host;

/* Variables and functions */
int NI_MAXHOST;
int NI_MAXSERV;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ gai_strerror(int);
int getnameinfo(struct sockaddr *, int, char *, int, char *, int, int);
int /*<<< orphan*/ printf(char *, char *, char *);
int socket_accept(int ls) {
  char host[NI_MAXHOST], serv[NI_MAXSERV];
  struct sockaddr_storage sa;
  socklen_t salen;
  int error, as;
  salen = sizeof(sa);
  as = accept(ls, (struct sockaddr *)&sa, &salen);
  if (as == -1)
    err(1, "accept");
  error = getnameinfo((struct sockaddr *)&sa, salen, host, sizeof(host), serv,
                      sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
  if (error)
    errx(1, "accept getnameinfo: %s", gai_strerror(error));
  printf("accept %s %s\n", host, serv);
  return as;
}


x86 assembly (clang, -O0):
<socket_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11f9 <socket_accept+0x99>
mov    $0x1,%edi
lea    0xe0c(%rip),%rsi
call   1070 <err@plt>
mov    -0x40(%rbp),%r8
mov    -0x50(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    -0x58(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %eax,%r9d
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    0x2dc5(%rip),%r10
or     (%r10),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     125e <socket_accept+0xfe>
mov    -0x30(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xdae(%rip),%rsi
call   1030 <errx@plt>
mov    -0x40(%rbp),%rdx
mov    -0x50(%rbp),%rsi
lea    0xdb1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_accept>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e53(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r14
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e4d(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x4,-0x2c(%rbp)
lea    -0x30(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11d7 <socket_accept+0x77>
lea    0xe33(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    -0x2c(%rbp),%esi
mov    0x2def(%rip),%rdx
mov    0x2e00(%rip),%rcx
mov    (%rcx),%eax
or     (%rdx),%eax
sub    $0x8,%rsp
lea    -0x30(%rbp),%rdi
mov    %r14,%rdx
mov    %r15d,%ecx
mov    %rbx,%r8
mov    %r13d,%r9d
push   %rax
call   1060 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     1228 <socket_accept+0xc8>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdeb(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
lea    0xdef(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <printf@plt>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@.str = [7 x i8] c"accept\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.1 = [23 x i8] c"accept getnameinfo: %s\00"
@.str.2 = [14 x i8] c"accept %s %s\0A\00"
define i32 @socket_accept(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %2
%10 = load i32, i32* @NI_MAXHOST
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %3
%13 = alloca i8, i64 %11
store i64 %11, i64* %4
%14 = load i32, i32* @NI_MAXSERV
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
store i64 %15, i64* %5
store i32 4, i32* %7
%17 = load i32, i32* %2
%18 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%19 = call i32 @accept(i32 %17, %struct.sockaddr* %18, i32* %7)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %1
%25 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%26 = load i32, i32* %7
%27 = trunc i64 %11 to i32
%28 = trunc i64 %15 to i32
%29 = load i32, i32* @NI_NUMERICHOST
%30 = load i32, i32* @NI_NUMERICSERV
%31 = or i32 %29, %30
%32 = call i32 @getnameinfo(%struct.sockaddr* %25, i32 %26, i8* %13, i32 %27, i8* %16, i32 %28, i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %24
%36 = load i32, i32* %8
%37 = call i32 @gai_strerror(i32 %36)
%38 = call i32 @errx(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %35, %24
%40 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* %16)
%41 = load i32, i32* %9
%42 = load i8*, i8** %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@.str = [7 x i8] c"accept\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.1 = [23 x i8] c"accept getnameinfo: %s\00"
@.str.2 = [14 x i8] c"accept %s %s\0A\00"
define i32 @socket_accept(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = load i32, i32* @NI_MAXHOST
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = load i32, i32* @NI_MAXSERV
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast %struct.sockaddr_storage* %2 to i8*
%11 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%12 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %12, i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
%18 = load i32, i32* %3
%19 = load i32, i32* @NI_NUMERICHOST
%20 = load i32, i32* @NI_NUMERICSERV
%21 = or i32 %20, %19
%22 = call i32 @getnameinfo(%struct.sockaddr* %12, i32 %18, i8* %6, i32 %4, i8* %9, i32 %7, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %17
%25 = call i32 @gai_strerror(i32 %22)
%26 = call i32 @errx(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %17
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %9)
ret i32 %13
}