#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 EOF;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int get_last_cap(unsigned *last_cap) {
  FILE *file = fopen("/proc/sys/kernel/cap_last_cap", "r");
  if (file == NULL) {
    int saved_errno = errno;
    fprintf(stderr, "failed to open /proc/sys/kernel/cap_last_cap: %s\n",
            strerror(errno));
    return -saved_errno;
  }
  int res = fscanf(file, "%u", last_cap);
  if (res == EOF) {
    int saved_errno = errno;
    fprintf(stderr,
            "could not read number from /proc/sys/kernel/cap_last_cap: %s\n",
            strerror(errno));
    return -saved_errno;
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<get_last_cap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <get_last_cap+0x73>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe6f(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <get_last_cap+0xf1>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe80(%rip),%rsi
call   1040 <fscanf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
jne    1231 <get_last_cap+0xe1>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe36(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <get_last_cap+0xf1>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_last_cap>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea2(%rip),%rdi
lea    0xeb9(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b1 <get_last_cap+0x61>
mov    %rax,%rbx
lea    0xed9(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fscanf@plt>
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%eax
jne    11df <get_last_cap+0x8f>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e39(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xea6(%rip),%rsi
jmp    11d1 <get_last_cap+0x81>
mov    0x2e18(%rip),%rax
mov    (%rax),%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe4f(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
neg    %ebx
jmp    11e9 <get_last_cap+0x99>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"/proc/sys/kernel/cap_last_cap\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [50 x i8] c"failed to open /proc/sys/kernel/cap_last_cap: %s\0A\00"
@.str.3 = [3 x i8] c"%u\00"
@EOF = global i32 0
@.str.4 = [62 x i8] c"could not read number from /proc/sys/kernel/cap_last_cap: %s\0A\00"
define i32 @get_last_cap(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %19
11: ; preds = %1
%12 = load i32, i32* @errno
store i32 %12, i32* %5
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i32, i32* %5
%18 = sub nsw i32 0, %17
store i32 %18, i32* %2
br label %37
19: ; preds = %1
%20 = load i32*, i32** %4
%21 = load i32*, i32** %3
%22 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %34
26: ; preds = %19
%27 = load i32, i32* @errno
store i32 %27, i32* %7
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %7
%33 = sub nsw i32 0, %32
store i32 %33, i32* %2
br label %37
34: ; preds = %19
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
store i32 0, i32* %2
br label %37
37: ; preds = %34, %26, %11
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [30 x i8] c"/proc/sys/kernel/cap_last_cap\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@stderr = global i32 0
@.str.2 = [50 x i8] c"failed to open /proc/sys/kernel/cap_last_cap: %s\0A\00"
@.str.3 = [3 x i8] c"%u\00"
@EOF = global i32 0
@.str.4 = [62 x i8] c"could not read number from /proc/sys/kernel/cap_last_cap: %s\0A\00"
define i32 @get_last_cap(i32* %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %10
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = load i32, i32* @stderr
%7 = tail call i8* @strerror(i32 %5)
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%9 = sub nsw i32 0, %5
br label %22
10: ; preds = %1
%11 = tail call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %0)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = load i32, i32* @stderr
%17 = tail call i8* @strerror(i32 %15)
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i8* %17)
%19 = sub nsw i32 0, %15
br label %22
20: ; preds = %10
%21 = tail call i32 @fclose(i32* %2)
br label %22
22: ; preds = %14, %20, %4
%23 = phi i32 [ %9, %4 ], [ %19, %14 ], [ 0, %20 ]
ret i32 %23
}