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

/* Variables and functions */
int /*<<< orphan*/ XFlush(int /*<<< orphan*/ *);
int /*<<< orphan*/ XFree(int *);
int /*<<< orphan*/ XNVCTRLQueryTargetBinaryData(int /*<<< orphan*/ *, int, int,
                                                int /*<<< orphan*/, int,
                                                unsigned char **, int *);
int /*<<< orphan*/ XNVCTRLQueryTargetCount(int /*<<< orphan*/ *, int, int *);
int /*<<< orphan*/ XNVCTRLSetTargetAttribute(int /*<<< orphan*/ *, int, int,
                                             int /*<<< orphan*/, int,
                                             int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
void do_disable(Display *dpy) {
  Bool ret;
  int num_framelocks;
  int framelock;
  int gpu;
  int *data;
  int len;
  int i;
  ret = XNVCTRLQueryTargetCount(dpy, 2, &num_framelocks);
  if (!ret) {
    printf("Failed to query number of frame lock devices!\n");
    return;
  }
  printf("Found %d frame lock device(s) on server.\n", num_framelocks);
  if (!num_framelocks) {
    return;
  }
  for (framelock = 0; framelock < num_framelocks; framelock++) {
    printf("\n");
    printf("- Frame Lock Board %d :\n", framelock);
    ret = XNVCTRLQueryTargetBinaryData(dpy, 2, framelock, 0, 5,
                                       (unsigned char **)&data, &len);
    if (!ret) {
      printf("  - Failed to query list of GPUs!\n");
      continue;
    }
    if (!data[0]) {
      printf("  - No GPUs found!\n");
    } else {
      printf("  - Found %d GPU(s).\n", data[0]);
    }
    for (i = 1; i <= data[0]; i++) {
      gpu = data[i];
      printf("  - Disabling Quadro Sync Device %d - GPU %d... ", framelock,
             gpu);
      XNVCTRLSetTargetAttribute(dpy, 1, gpu, 0, 29, 0);
      XFlush(dpy);
      printf("Done.\n");
    }
    XFree(data);
  }
}


x86 assembly (clang, -O0):
<do_disable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2,%esi
lea    -0x10(%rbp),%rdx
call   1050 <XNVCTRLQueryTargetCount@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    119e <do_disable+0x3e>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12fa <do_disable+0x19a>
mov    -0x10(%rbp),%esi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jne    11be <do_disable+0x5e>
jmp    12fa <do_disable+0x19a>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12fa <do_disable+0x19a>
lea    0xecf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%r9
mov    $0x2,%esi
xor    %ecx,%ecx
mov    $0x5,%r8d
lea    -0x24(%rbp),%rax
mov    %rax,(%rsp)
call   1060 <XNVCTRLQueryTargetBinaryData@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1235 <do_disable+0xd5>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ec <do_disable+0x18c>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1255 <do_disable+0xf5>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1269 <do_disable+0x109>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jg     12e3 <do_disable+0x183>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x1,%esi
xor    %r9d,%r9d
mov    $0x1d,%r8d
mov    %r9d,%ecx
call   1080 <XNVCTRLSetTargetAttribute@plt>
mov    -0x8(%rbp),%rdi
call   1070 <XFlush@plt>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1270 <do_disable+0x110>
mov    -0x20(%rbp),%rdi
call   1040 <XFree@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c5 <do_disable+0x65>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_disable>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0x14(%rsp),%rdx
mov    $0x2,%esi
call   1070 <XNVCTRLQueryTargetCount@plt>
test   %eax,%eax
je     12db <do_disable+0x15b>
mov    0x14(%rsp),%esi
lea    0xe4d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0x14(%rsp)
jle    12e7 <do_disable+0x167>
lea    0xe8d(%rip),%rbp
lea    0xf1a(%rip),%r13
xor    %r12d,%r12d
jmp    11fb <do_disable+0x7b>
nopl   0x0(%rax,%rax,1)
lea    0xed1(%rip),%rdi
call   1040 <puts@plt>
add    $0x1,%r12d
cmp    0x14(%rsp),%r12d
jge    12e7 <do_disable+0x167>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe1e(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x24(%rsp),%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    $0x5,%r8d
lea    0x18(%rsp),%r9
call   1080 <XNVCTRLQueryTargetBinaryData@plt>
test   %eax,%eax
je     11e0 <do_disable+0x60>
mov    0x18(%rsp),%rax
mov    (%rax),%esi
test   %esi,%esi
je     1260 <do_disable+0xe0>
lea    0xdf1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    126c <do_disable+0xec>
nopl   0x0(%rax,%rax,1)
lea    0xe73(%rip),%rdi
call   1040 <puts@plt>
mov    0x18(%rsp),%rdi
cmpl   $0x0,(%rdi)
jle    12d1 <do_disable+0x151>
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    0x4(%rdi,%r15,4),%r14d
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    $0x1d,%r8d
xor    %r9d,%r9d
call   10a0 <XNVCTRLSetTargetAttribute@plt>
mov    %rbx,%rdi
call   1090 <XFlush@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    0x18(%rsp),%rdi
movslq (%rdi),%rax
add    $0x1,%r15
cmp    %rax,%r15
jl     1280 <do_disable+0x100>
call   1060 <XFree@plt>
jmp    11ec <do_disable+0x6c>
lea    0xda8(%rip),%rdi
call   1040 <puts@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"Failed to query number of frame lock devices!\0A\00"
@.str.1 = [42 x i8] c"Found %d frame lock device(s) on server.\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [25 x i8] c"- Frame Lock Board %d :\0A\00"
@.str.4 = [35 x i8] c" - Failed to query list of GPUs!\0A\00"
@.str.5 = [20 x i8] c" - No GPUs found!\0A\00"
@.str.6 = [22 x i8] c" - Found %d GPU(s).\0A\00"
@.str.7 = [49 x i8] c" - Disabling Quadro Sync Device %d - GPU %d... \00"
@.str.8 = [7 x i8] c"Done.\0A\00"
define void @do_disable(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %2
%10 = load i32*, i32** %2
%11 = call i32 @XNVCTRLQueryTargetCount(i32* %10, i32 2, i32* %4)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %82
16: ; preds = %1
%17 = load i32, i32* %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %16
br label %82
22: ; preds = %16
store i32 0, i32* %5
br label %23
23: ; preds = %79, %22
%24 = load i32, i32* %5
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %82
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %5
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32*, i32** %2
%32 = load i32, i32* %5
%33 = bitcast i32** %7 to i8**
%34 = call i32 @XNVCTRLQueryTargetBinaryData(i32* %31, i32 2, i32 %32, i32 0, i32 5, i8** %33, i32* %8)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %27
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
br label %79
39: ; preds = %27
%40 = load i32*, i32** %7
%41 = getelementptr inbounds i32, i32* %40, i64 0
%42 = load i32, i32* %41
%43 = icmp ne i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %51
46: ; preds = %39
%47 = load i32*, i32** %7
%48 = getelementptr inbounds i32, i32* %47, i64 0
%49 = load i32, i32* %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %46, %44
store i32 1, i32* %9
br label %52
52: ; preds = %73, %51
%53 = load i32, i32* %9
%54 = load i32*, i32** %7
%55 = getelementptr inbounds i32, i32* %54, i64 0
%56 = load i32, i32* %55
%57 = icmp sle i32 %53, %56
br i1 %57, label %58, label %76
58: ; preds = %52
%59 = load i32*, i32** %7
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %59, i64 %61
%63 = load i32, i32* %62
store i32 %63, i32* %6
%64 = load i32, i32* %5
%65 = load i32, i32* %6
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0), i32 %64, i32 %65)
%67 = load i32*, i32** %2
%68 = load i32, i32* %6
%69 = call i32 @XNVCTRLSetTargetAttribute(i32* %67, i32 1, i32 %68, i32 0, i32 29, i32 0)
%70 = load i32*, i32** %2
%71 = call i32 @XFlush(i32* %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %73
73: ; preds = %58
%74 = load i32, i32* %9
%75 = add nsw i32 %74, 1
store i32 %75, i32* %9
br label %52
76: ; preds = %52
%77 = load i32*, i32** %7
%78 = call i32 @XFree(i32* %77)
br label %79
79: ; preds = %76, %37
%80 = load i32, i32* %5
%81 = add nsw i32 %80, 1
store i32 %81, i32* %5
br label %23
82: ; preds = %14, %21, %23
ret void
}

LLVM IR (-O2):
@.str.1 = [42 x i8] c"Found %d frame lock device(s) on server.\0A\00"
@.str.3 = [25 x i8] c"- Frame Lock Board %d :\0A\00"
@.str.6 = [22 x i8] c" - Found %d GPU(s).\0A\00"
@.str.7 = [49 x i8] c" - Disabling Quadro Sync Device %d - GPU %d... \00"
@str = [46 x i8] c"Failed to query number of frame lock devices!\00"
@str.9 = [34 x i8] c" - Failed to query list of GPUs!\00"
@str.10 = [19 x i8] c" - No GPUs found!\00"
@str.11 = [6 x i8] c"Done.\00"
define void @do_disable(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32** %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = call i32 @XNVCTRLQueryTargetCount(i32* %0, i32 2, i32* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %60
12: ; preds = %1
%13 = load i32, i32* %2
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %60
17: ; preds = %12
%18 = bitcast i32** %3 to i8**
br label %19
19: ; preds = %17, %56
%20 = phi i32 [ 0, %17 ], [ %57, %56 ]
%21 = call i32 @putchar(i32 10)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%23 = call i32 @XNVCTRLQueryTargetBinaryData(i32* %0, i32 2, i32 %20, i32 0, i32 5, i8** %18, i32* %4)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.9, i64 0, i64 0))
br label %56
27: ; preds = %19
%28 = load i32*, i32** %3
%29 = load i32, i32* %28
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %35
33: ; preds = %27
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %29)
br label %35
35: ; preds = %33, %31
%36 = load i32*, i32** %3
%37 = load i32, i32* %36
%38 = icmp slt i32 %37, 1
br i1 %38, label %53, label %39
39: ; preds = %35, %39
%40 = phi i64 [ %48, %39 ], [ 1, %35 ]
%41 = phi i32* [ %49, %39 ], [ %36, %35 ]
%42 = getelementptr inbounds i32, i32* %41, i64 %40
%43 = load i32, i32* %42
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0), i32 %20, i32 %43)
%45 = call i32 @XNVCTRLSetTargetAttribute(i32* %0, i32 1, i32 %43, i32 0, i32 29, i32 0)
%46 = call i32 @XFlush(i32* %0)
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.11, i64 0, i64 0))
%48 = add nuw nsw i64 %40, 1
%49 = load i32*, i32** %3
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = icmp slt i64 %40, %51
br i1 %52, label %39, label %53
53: ; preds = %39, %35
%54 = phi i32* [ %36, %35 ], [ %49, %39 ]
%55 = call i32 @XFree(i32* %54)
br label %56
56: ; preds = %53, %25
%57 = add nuw nsw i32 %20, 1
%58 = load i32, i32* %2
%59 = icmp slt i32 %57, %58
br i1 %59, label %19, label %60
60: ; preds = %56, %12, %10
ret void
}