#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int snprintf(char *, size_t, char *, char const *, int /*<<< orphan*/);
int nvqr_ipc_get_socket_name(char *dest, size_t len, pid_t pid) {
  int total_len;
  static const char *basename = "nvidia-query-resource-opengl-socket.";
  memset(dest, 0, len);
  total_len = snprintf(dest, len, "0%s%d", basename, pid);
  dest[0] = '\0';
  if (total_len >= len) {
    dest[len - 1] = '\0';
  }
  return total_len;
}


x86 assembly (clang, -O0):
<nvqr_ipc_get_socket_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2edf(%rip),%rcx
mov    -0x14(%rbp),%r8d
lea    0xec9(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
movslq -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     1189 <nvqr_ipc_get_socket_name+0x69>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvqr_ipc_get_socket_name>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
lea    0xee8(%rip),%rdx
lea    0xebc(%rip),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%r8d
call   1030 <snprintf@plt>
movb   $0x0,(%rbx)
movslq %eax,%rcx
cmp    %r14,%rcx
jb     1163 <nvqr_ipc_get_socket_name+0x43>
movb   $0x0,-0x1(%r14,%rbx,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@nvqr_ipc_get_socket_name.basename = internal global i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i32 0, i32 0)
@.str = [37 x i8] c"nvidia-query-resource-opengl-socket.\00"
@.str.1 = [6 x i8] c"0%s%d\00"
define i32 @nvqr_ipc_get_socket_name(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i64, i64* %5
%10 = call i32 @memset(i8* %8, i32 0, i64 %9)
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = load i8*, i8** @nvqr_ipc_get_socket_name.basename
%14 = load i32, i32* %6
%15 = call i32 @snprintf(i8* %11, i64 %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %14)
store i32 %15, i32* %7
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = load i64, i64* %5
%21 = icmp uge i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i8*, i8** %4
%24 = load i64, i64* %5
%25 = sub i64 %24, 1
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %22, %3
%28 = load i32, i32* %7
ret i32 %28
}

LLVM IR (-O2):
@.str = [37 x i8] c"nvidia-query-resource-opengl-socket.\00"
@.str.1 = [6 x i8] c"0%s%d\00"
define i32 @nvqr_ipc_get_socket_name(i8* %0, i64 %1, i32 %2) {
%4 = tail call i32 @memset(i8* %0, i32 0, i64 %1)
%5 = tail call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %2)
store i8 0, i8* %0
%6 = sext i32 %5 to i64
%7 = icmp ult i64 %6, %1
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = add i64 %1, -1
%10 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %10
br label %11
11: ; preds = %8, %3
ret i32 %5
}