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

/* Variables and functions */
int clamp(int, int /*<<< orphan*/, int);
int get_global_size(int);
int getElementAt(int x, int y, int z) {
  x = clamp(x, 0, (int)get_global_size(0) - 1);
  y = clamp(y, 0, (int)get_global_size(1) - 1);
  z = clamp(z, 0, (int)get_global_size(2) - 1);
  return x + y * get_global_size(0) +
         z * get_global_size(0) * get_global_size(1);
}


x86 assembly (clang, -O0):
<getElementAt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
xor    %edi,%edi
call   1040 <get_global_size@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1,%edi
call   1040 <get_global_size@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x2,%edi
call   1040 <get_global_size@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
call   1040 <get_global_size@plt>
mov    -0x20(%rbp),%ecx
mov    %eax,%edx
mov    -0x1c(%rbp),%eax
imul   %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
call   1040 <get_global_size@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    $0x1,%edi
call   1040 <get_global_size@plt>
mov    -0x14(%rbp),%ecx
mov    %eax,%edx
mov    -0x10(%rbp),%eax
imul   %edx,%ecx
add    %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getElementAt>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%ebp
xor    %edi,%edi
call   1040 <get_global_size@plt>
lea    -0x1(%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,%r15d
mov    $0x1,%edi
call   1040 <get_global_size@plt>
lea    -0x1(%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,%ebx
mov    $0x2,%edi
call   1040 <get_global_size@plt>
lea    -0x1(%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1030 <clamp@plt>
mov    %eax,%r14d
xor    %edi,%edi
call   1040 <get_global_size@plt>
mov    %eax,%ebp
imul   %ebx,%ebp
add    %r15d,%ebp
xor    %edi,%edi
call   1040 <get_global_size@plt>
mov    %eax,%ebx
mov    $0x1,%edi
call   1040 <get_global_size@plt>
imul   %ebx,%eax
imul   %r14d,%eax
add    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getElementAt(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = load i32, i32* %4
%8 = call i32 @get_global_size(i32 0)
%9 = sub nsw i32 %8, 1
%10 = call i32 @clamp(i32 %7, i32 0, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %5
%12 = call i32 @get_global_size(i32 1)
%13 = sub nsw i32 %12, 1
%14 = call i32 @clamp(i32 %11, i32 0, i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %6
%16 = call i32 @get_global_size(i32 2)
%17 = sub nsw i32 %16, 1
%18 = call i32 @clamp(i32 %15, i32 0, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = call i32 @get_global_size(i32 0)
%22 = mul nsw i32 %20, %21
%23 = add nsw i32 %19, %22
%24 = load i32, i32* %6
%25 = call i32 @get_global_size(i32 0)
%26 = mul nsw i32 %24, %25
%27 = call i32 @get_global_size(i32 1)
%28 = mul nsw i32 %26, %27
%29 = add nsw i32 %23, %28
ret i32 %29
}

LLVM IR (-O2):
define i32 @getElementAt(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @get_global_size(i32 0)
%5 = add nsw i32 %4, -1
%6 = tail call i32 @clamp(i32 %0, i32 0, i32 %5)
%7 = tail call i32 @get_global_size(i32 1)
%8 = add nsw i32 %7, -1
%9 = tail call i32 @clamp(i32 %1, i32 0, i32 %8)
%10 = tail call i32 @get_global_size(i32 2)
%11 = add nsw i32 %10, -1
%12 = tail call i32 @clamp(i32 %2, i32 0, i32 %11)
%13 = tail call i32 @get_global_size(i32 0)
%14 = mul nsw i32 %13, %9
%15 = add nsw i32 %14, %6
%16 = tail call i32 @get_global_size(i32 0)
%17 = mul nsw i32 %16, %12
%18 = tail call i32 @get_global_size(i32 1)
%19 = mul nsw i32 %17, %18
%20 = add nsw i32 %15, %19
ret i32 %20
}