#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 pid_t;
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ fatal(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int,
                            char const *);
int procfs_opennode(const char *procfs, pid_t pid, const char *node) {
  char filename[PATH_MAX];
  int fd;
  snprintf(filename, sizeof(filename), "%s/%u/%s", procfs, pid, node);
  filename[sizeof(filename) - 1] = '\0';
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    fatal(EX_OSERR, "cannot open %s: %m", filename);
  return fd;
}


x86 assembly (clang, -O0):
<procfs_opennode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    -0x18(%rbp),%r9
lea    0xe78(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d1 <procfs_opennode+0xa1>
mov    -0x40(%rbp),%rdx
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1050 <fatal@plt>
mov    -0x2c(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<procfs_opennode>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rdx,%r9
mov    %esi,%r8d
mov    %rdi,%rcx
mov    0x2ea9(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe9e(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <snprintf@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11a3 <procfs_opennode+0x73>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fatal@plt>
mov    %r14d,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"%s/%u/%s\00"
@O_RDONLY = global i32 0
@EX_OSERR = global i32 0
@.str.1 = [19 x i8] c"cannot open %s: %m\00"
define i32 @procfs_opennode(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = trunc i64 %11 to i32
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16, i8* %17)
%19 = sub i64 %11, 1
%20 = getelementptr inbounds i8, i8* %13, i64 %19
store i8 0, i8* %20
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %13, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %3
%26 = load i32, i32* @EX_OSERR
%27 = call i32 @fatal(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %28
28: ; preds = %25, %3
%29 = load i32, i32* %9
%30 = load i8*, i8** %7
ret i32 %29
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"%s/%u/%s\00"
@O_RDONLY = global i32 0
@EX_OSERR = global i32 0
@.str.1 = [19 x i8] c"cannot open %s: %m\00"
define i32 @procfs_opennode(i8* %0, i32 %1, i8* %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i8* %2)
%8 = add nsw i64 %5, -1
%9 = getelementptr inbounds i8, i8* %6, i64 %8
store i8 0, i8* %9
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %6, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @EX_OSERR
%15 = call i32 @fatal(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %16
16: ; preds = %13, %3
ret i32 %11
}