#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 */
struct __user_cap_header_struct {
  int /*<<< orphan*/ version;
  int /*<<< orphan*/ pid;
};
struct __user_cap_data_struct {
  int effective;
  int permitted;
  int inheritable;
};
typedef struct __user_cap_header_struct *cap_user_header_t;
typedef struct __user_cap_data_struct *cap_user_data_t;

/* Variables and functions */
int /*<<< orphan*/ _LINUX_CAPABILITY_VERSION_1;
scalar_t__ capget(struct __user_cap_header_struct *,
                  struct __user_cap_data_struct *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int main() {
  struct __user_cap_header_struct cap_header_data;
  cap_user_header_t cap_header = &cap_header_data;
  struct __user_cap_data_struct cap_data_data;
  cap_user_data_t cap_data = &cap_data_data;
  cap_header->pid = getpid();
  cap_header->version = _LINUX_CAPABILITY_VERSION_1;
  if (capget(cap_header, cap_data) < 0) {
    perror("Failed capget");
    exit(1);
  }
  printf("Cap data 0x%x,0x%x,0x%x\n", cap_data->effective, cap_data->permitted,
         cap_data->inheritable);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2e61(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <capget@plt>
cmp    $0x0,%rax
jge    11b4 <main+0x64>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,0x4(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1050 <capget@plt>
test   %rax,%rax
js     119c <main+0x4c>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.__user_cap_header_struct = type { i32, i32 }
%struct.__user_cap_data_struct = type { i32, i32, i32 }
@_LINUX_CAPABILITY_VERSION_1 = global i32 0
@.str = [14 x i8] c"Failed capget\00"
@.str.1 = [25 x i8] c"Cap data 0x%x,0x%x,0x%x\0A\00"
define i32 @main() {
%1 = alloca %struct.__user_cap_header_struct
%2 = alloca %struct.__user_cap_header_struct*
%3 = alloca %struct.__user_cap_data_struct
%4 = alloca %struct.__user_cap_data_struct*
store %struct.__user_cap_header_struct* %1, %struct.__user_cap_header_struct** %2
store %struct.__user_cap_data_struct* %3, %struct.__user_cap_data_struct** %4
%5 = call i32 (...) @getpid()
%6 = load %struct.__user_cap_header_struct*, %struct.__user_cap_header_struct** %2
%7 = getelementptr inbounds %struct.__user_cap_header_struct, %struct.__user_cap_header_struct* %6, i32 0, i32 1
store i32 %5, i32* %7
%8 = load i32, i32* @_LINUX_CAPABILITY_VERSION_1
%9 = load %struct.__user_cap_header_struct*, %struct.__user_cap_header_struct** %2
%10 = getelementptr inbounds %struct.__user_cap_header_struct, %struct.__user_cap_header_struct* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load %struct.__user_cap_header_struct*, %struct.__user_cap_header_struct** %2
%12 = load %struct.__user_cap_data_struct*, %struct.__user_cap_data_struct** %4
%13 = call i64 @capget(%struct.__user_cap_header_struct* %11, %struct.__user_cap_data_struct* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = load %struct.__user_cap_data_struct*, %struct.__user_cap_data_struct** %4
%20 = getelementptr inbounds %struct.__user_cap_data_struct, %struct.__user_cap_data_struct* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load %struct.__user_cap_data_struct*, %struct.__user_cap_data_struct** %4
%23 = getelementptr inbounds %struct.__user_cap_data_struct, %struct.__user_cap_data_struct* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load %struct.__user_cap_data_struct*, %struct.__user_cap_data_struct** %4
%26 = getelementptr inbounds %struct.__user_cap_data_struct, %struct.__user_cap_data_struct* %25, i32 0, i32 2
%27 = load i32, i32* %26
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %24, i32 %27)
ret i32 0
}

LLVM IR (-O2):
%struct.__user_cap_header_struct = type { i32, i32 }
%struct.__user_cap_data_struct = type { i32, i32, i32 }
@_LINUX_CAPABILITY_VERSION_1 = global i32 0
@.str = [14 x i8] c"Failed capget\00"
@.str.1 = [25 x i8] c"Cap data 0x%x,0x%x,0x%x\0A\00"
define i32 @main() {
%1 = alloca %struct.__user_cap_header_struct
%2 = alloca %struct.__user_cap_data_struct
%3 = bitcast %struct.__user_cap_header_struct* %1 to i8*
%4 = bitcast %struct.__user_cap_data_struct* %2 to i8*
%5 = tail call i32 (...) @getpid()
%6 = getelementptr inbounds %struct.__user_cap_header_struct, %struct.__user_cap_header_struct* %1, i64 0, i32 1
store i32 %5, i32* %6
%7 = load i32, i32* @_LINUX_CAPABILITY_VERSION_1
%8 = getelementptr inbounds %struct.__user_cap_header_struct, %struct.__user_cap_header_struct* %1, i64 0, i32 0
store i32 %7, i32* %8
%9 = call i64 @capget(%struct.__user_cap_header_struct* %1, %struct.__user_cap_data_struct* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = getelementptr inbounds %struct.__user_cap_data_struct, %struct.__user_cap_data_struct* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.__user_cap_data_struct, %struct.__user_cap_data_struct* %2, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.__user_cap_data_struct, %struct.__user_cap_data_struct* %2, i64 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %18, i32 %20)
ret i32 0
}