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

/* Variables and functions */
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *get_line(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
char *gpu_name_get() {
  char *nvidia_cmd = "/bin/nvidia-smi";
  if (access(nvidia_cmd, X_OK) != 0) {
    return NULL;
  }
  FILE *pipe =
      popen("/bin/nvidia-smi --query-gpu=gpu_name --format=csv,noheader", "r");
  if (!pipe) {
    return NULL;
  }
  char *gpu_name = get_line(pipe);
  fclose(pipe);
  return gpu_name;
}


x86 assembly (clang, -O0):
<gpu_name_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
je     117c <gpu_name_get+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    11c9 <gpu_name_get+0x89>
lea    0xe8d(%rip),%rdi
lea    0xebf(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ab <gpu_name_get+0x6b>
movq   $0x0,-0x8(%rbp)
jmp    11c9 <gpu_name_get+0x89>
mov    -0x18(%rbp),%rdi
call   1030 <get_line@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpu_name_get>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
call   1050 <access@plt>
test   %rax,%rax
je     1162 <gpu_name_get+0x22>
xor    %eax,%eax
jmp    1193 <gpu_name_get+0x53>
lea    0xea7(%rip),%rdi
lea    0xed9(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     115e <gpu_name_get+0x1e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <get_line@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/bin/nvidia-smi\00"
@X_OK = global i32 0
@.str.1 = [59 x i8] c"/bin/nvidia-smi --query-gpu=gpu_name --format=csv,noheader\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @gpu_name_get() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @X_OK
%7 = call i64 @access(i8* %5, i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i8* null, i8** %1
br label %21
10: ; preds = %0
%11 = call i32* @popen(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %10
store i8* null, i8** %1
br label %21
15: ; preds = %10
%16 = load i32*, i32** %3
%17 = call i8* @get_line(i32* %16)
store i8* %17, i8** %4
%18 = load i32*, i32** %3
%19 = call i32 @fclose(i32* %18)
%20 = load i8*, i8** %4
store i8* %20, i8** %1
br label %21
21: ; preds = %15, %14, %9
%22 = load i8*, i8** %1
ret i8* %22
}

LLVM IR (-O2):
@.str = [16 x i8] c"/bin/nvidia-smi\00"
@X_OK = global i32 0
@.str.1 = [59 x i8] c"/bin/nvidia-smi --query-gpu=gpu_name --format=csv,noheader\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @gpu_name_get() {
%1 = load i32, i32* @X_OK
%2 = tail call i64 @access(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %10
4: ; preds = %0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i8* @get_line(i32* %5)
%9 = tail call i32 @fclose(i32* %5)
br label %10
10: ; preds = %7, %4, %0
%11 = phi i8* [ null, %0 ], [ %8, %7 ], [ null, %4 ]
ret i8* %11
}