#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 scalar_t__ pid_t;

/* Variables and functions */
int R_OK;
int X_OK;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ access(char *, int);
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int gpu_info_get() {
  int pipefd[2];
  pipe(pipefd);
  pid_t pid = fork();
  if (pid < 0) {
    return 0;
  } else if (pid == 0) {
    close(pipefd[0]);
    dup2(pipefd[1], fileno(stdout));
    char *args[] = {"cctools_gpu_autodetect", NULL};
    if (!access("cctools_gpu_autodetect", R_OK | X_OK)) {
      execv("cctools_gpu_autodetect", args);
    } else {
      execvp("cctools_gpu_autodetect", args);
    }
    _exit(0);
  } else {
    close(pipefd[1]);
    int status = 0;
    int gpu_count = 0;
    char buffer[10];
    if (read(pipefd[0], buffer, 10)) {
      waitpid(pid, &status, 0);
      gpu_count = atoi(buffer);
    }
    close(pipefd[0]);
    return gpu_count;
  }
}


x86 assembly (clang, -O0):
<gpu_info_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11f3 <gpu_info_get+0x33>
movl   $0x0,-0x4(%rbp)
jmp    12e7 <gpu_info_get+0x127>
cmpq   $0x0,-0x18(%rbp)
jne    128d <gpu_info_get+0xcd>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    -0x48(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    0x2bc5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2bc2(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    0x2d96(%rip),%rax
or     (%rax),%esi
lea    0xdad(%rip),%rdi
call   10a0 <access@plt>
cmp    $0x0,%eax
jne    1276 <gpu_info_get+0xb6>
lea    -0x30(%rbp),%rsi
lea    0xd94(%rip),%rdi
call   10d0 <execv@plt>
jmp    1286 <gpu_info_get+0xc6>
lea    -0x30(%rbp),%rsi
lea    0xd7f(%rip),%rdi
call   10b0 <execvp@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x42(%rbp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
je     12d9 <gpu_info_get+0x119>
mov    -0x18(%rbp),%rdi
lea    -0x34(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
lea    -0x42(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpu_info_get>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     1234 <gpu_info_get+0x74>
je     123d <gpu_info_get+0x7d>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1050 <close@plt>
movl   $0x0,0xc(%rsp)
mov    0x4(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
test   %rax,%rax
je     1229 <gpu_info_get+0x69>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <waitpid@plt>
lea    0x10(%rsp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
jmp    122b <gpu_info_get+0x6b>
xor    %ebp,%ebp
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%ebx
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
movaps 0x2b88(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2d54(%rip),%rax
mov    0x2d65(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xd7a(%rip),%rdi
call   10a0 <access@plt>
lea    0xd6e(%rip),%rdi
lea    0x10(%rsp),%rsi
test   %eax,%eax
jne    12a7 <gpu_info_get+0xe7>
call   10d0 <execv@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
call   10b0 <execvp@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [23 x i8] c"cctools_gpu_autodetect\00"
@__const.gpu_info_get.args = [2 x i8*] [i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i32 0, i32 0), i8* null]
@R_OK = global i32 0
@X_OK = global i32 0
define i32 @gpu_info_get() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca [2 x i8*]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i64 (...) @fork()
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 0, i32* %1
br label %59
14: ; preds = %0
%15 = load i64, i64* %3
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %14
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = load i32, i32* @stdout
%24 = call i32 @fileno(i32 %23)
%25 = call i32 @dup2(i32 %22, i32 %24)
%26 = bitcast [2 x i8*]* %4 to i8*
%27 = load i32, i32* @R_OK
%28 = load i32, i32* @X_OK
%29 = or i32 %27, %28
%30 = call i32 @access(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %17
%33 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
%34 = call i32 @execv(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %33)
br label %38
35: ; preds = %17
%36 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
%37 = call i32 @execvp(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %36)
br label %38
38: ; preds = %35, %32
%39 = call i32 @_exit(i32 0)
unreachable
40: ; preds = %14
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
store i32 0, i32* %5
store i32 0, i32* %6
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%45 = load i32, i32* %44
%46 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%47 = call i64 @read(i32 %45, i8* %46, i32 10)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %40
%50 = load i64, i64* %3
%51 = call i32 @waitpid(i64 %50, i32* %5, i32 0)
%52 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%53 = call i32 @atoi(i8* %52)
store i32 %53, i32* %6
br label %54
54: ; preds = %49, %40
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %6
store i32 %58, i32* %1
br label %59
59: ; preds = %54, %13
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [23 x i8] c"cctools_gpu_autodetect\00"
@__const.gpu_info_get.args = [2 x i8*] [i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i32 0, i32 0), i8* null]
@R_OK = global i32 0
@X_OK = global i32 0
define i32 @gpu_info_get() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i8*]
%3 = alloca i32
%4 = alloca [10 x i8]
%5 = bitcast [2 x i32]* %1 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %49, label %10
10: ; preds = %0
%11 = icmp eq i64 %8, 0
br i1 %11, label %12, label %33
12: ; preds = %10
%13 = load i32, i32* %6
%14 = call i32 @close(i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @stdout
%18 = call i32 @fileno(i32 %17)
%19 = call i32 @dup2(i32 %16, i32 %18)
%20 = bitcast [2 x i8*]* %2 to i8*
%21 = load i32, i32* @R_OK
%22 = load i32, i32* @X_OK
%23 = or i32 %22, %21
%24 = call i32 @access(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = icmp eq i32 %24, 0
%26 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
br i1 %25, label %27, label %29
27: ; preds = %12
%28 = call i32 @execv(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %26)
br label %31
29: ; preds = %12
%30 = call i32 @execvp(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %26)
br label %31
31: ; preds = %29, %27
%32 = call i32 @_exit(i32 0)
unreachable
33: ; preds = %10
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%39 = load i32, i32* %6
%40 = call i64 @read(i32 %39, i8* %38, i32 10)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %33
%43 = call i32 @waitpid(i64 %8, i32* %3, i32 0)
%44 = call i32 @atoi(i8* %38)
br label %45
45: ; preds = %42, %33
%46 = phi i32 [ %44, %42 ], [ 0, %33 ]
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %0, %45
%50 = phi i32 [ %46, %45 ], [ 0, %0 ]
ret i32 %50
}