#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ service;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ node;
typedef int in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *,
                           char const *);
char *gai_strerror(int);
int getnameinfo(struct sockaddr *, int, char *, int, char *, int, int const);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char const *, int, char const *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char const *);
int _getnameinfo(char const *progname, in_port_t port, char const *expected) {
  struct sockaddr_in sa;
  char node[16];
  char service[16];
  const int flags = 0;
  int res;
  printf("%s: Testing getnameinfo(%u, \"%s\")\n", progname, port, expected);
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  sa.sin_port = port;
  if ((res = getnameinfo((struct sockaddr *)&sa, sizeof(sa), node, sizeof(node),
                         service, sizeof(service), flags)) != 0) {
    fprintf(stderr, "%s: %s: %s\n", progname, "getnameinfo", gai_strerror(res));
    return 1;
  }
  if (strcmp(service, expected) != 0) {
    fprintf(stderr, "%s: %s: Wrong service (expected: %s)\n", progname, service,
            expected);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_getnameinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%r8
mov    $0xc,%esi
mov    $0x10,%r9d
xor    %eax,%eax
mov    %r9d,%ecx
movl   $0x0,(%rsp)
call   1070 <getnameinfo@plt>
mov    %eax,-0x58(%rbp)
cmp    $0x0,%eax
je     124d <_getnameinfo+0xdd>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x58(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%r8
lea    0xdee(%rip),%rsi
lea    0xdf3(%rip),%rcx
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1298 <_getnameinfo+0x128>
lea    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1291 <_getnameinfo+0x121>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    -0x50(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xdbb(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1298 <_getnameinfo+0x128>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_getnameinfo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xe75(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %r15,%rcx
call   1030 <printf@plt>
lea    0x10(%rsp),%r12
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,0x10(%rsp)
movl   $0x0,(%rsp)
lea    0x30(%rsp),%rdx
lea    0x20(%rsp),%r8
mov    %r12,%rdi
mov    $0xc,%esi
mov    $0x10,%ecx
mov    $0x10,%r9d
call   1070 <getnameinfo@plt>
test   %eax,%eax
je     1226 <_getnameinfo+0xb6>
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe0e(%rip),%rsi
lea    0xe13(%rip),%rcx
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%r8
jmp    1253 <_getnameinfo+0xe3>
lea    0x20(%rsp),%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     125d <_getnameinfo+0xed>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
lea    0x20(%rsp),%rcx
mov    %r14,%rdx
mov    %r15,%r8
call   1090 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"%s: Testing getnameinfo(%u, \22%s\22)\0A\00"
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"%s: %s: %s\0A\00"
@.str.2 = [12 x i8] c"getnameinfo\00"
@.str.3 = [38 x i8] c"%s: %s: Wrong service (expected: %s)\0A\00"
define i32 @_getnameinfo(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in
%9 = alloca [16 x i8]
%10 = alloca [16 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %11
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = load i8*, i8** %7
%16 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14, i8* %15)
%17 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_LOOPBACK
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %6
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%29 = call i32 @getnameinfo(%struct.sockaddr* %26, i32 12, i8* %27, i32 16, i8* %28, i32 16, i32 0)
store i32 %29, i32* %12
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %3
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %5
%34 = load i32, i32* %12
%35 = call i8* @gai_strerror(i32 %34)
%36 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %35)
store i32 1, i32* %4
br label %49
37: ; preds = %3
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%39 = load i8*, i8** %7
%40 = call i64 @strcmp(i8* %38, i8* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %5
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%46 = load i8*, i8** %7
%47 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %45, i8* %46)
store i32 1, i32* %4
br label %49
48: ; preds = %37
store i32 0, i32* %4
br label %49
49: ; preds = %48, %42, %31
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"%s: Testing getnameinfo(%u, \22%s\22)\0A\00"
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"%s: %s: %s\0A\00"
@.str.2 = [12 x i8] c"getnameinfo\00"
@.str.3 = [38 x i8] c"%s: %s: Wrong service (expected: %s)\0A\00"
define i32 @_getnameinfo(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca [16 x i8]
%6 = alloca [16 x i8]
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%10 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i8* %2)
%11 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_LOOPBACK
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %1, i32* %17
%18 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%19 = call i32 @getnameinfo(%struct.sockaddr* %18, i32 12, i8* %8, i32 16, i8* %9, i32 16, i32 0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = call i8* @gai_strerror(i32 %19)
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %31
25: ; preds = %3
%26 = call i64 @strcmp(i8* %9, i8* %2)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %9, i8* %2)
br label %31
31: ; preds = %25, %28, %21
%32 = phi i32 [ 1, %21 ], [ 1, %28 ], [ 0, %25 ]
ret i32 %32
}