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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char *);
int open_proc(int pid, char *file) {
  static char buf[100];
  int fd;
  snprintf(&buf[0], sizeof(buf), "/proc/%d/%s", pid, file);
  fd = open(&buf[0], O_RDONLY);
  if (fd < 0)
    perror(buf);
  return fd;
}


x86 assembly (clang, -O0):
<open_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    0x2f03(%rip),%rdi
mov    $0x64,%esi
lea    0xea7(%rip),%rdx
call   1030 <snprintf@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    0x2ee2(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    118c <open_proc+0x5c>
lea    0x2ec9(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_proc>:
push   %rbx
mov    %rsi,%r8
mov    %edi,%ecx
lea    0x2f13(%rip),%rbx
lea    0xebc(%rip),%rdx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1030 <snprintf@plt>
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     116c <open_proc+0x3c>
mov    %ebx,%eax
pop    %rbx
ret
lea    0x2edd(%rip),%rdi
call   1050 <perror@plt>
jmp    1168 <open_proc+0x38>

LLVM IR (-O0):
@open_proc.buf = internal global [100 x i8] zeroinitializer
@.str = [12 x i8] c"/proc/%d/%s\00"
@O_RDONLY = global i32 0
define i32 @open_proc(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i32, i32* %3
%7 = load i8*, i8** %4
%8 = call i32 @snprintf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @open_proc.buf, i64 0, i64 0), i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %6, i8* %7)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @open_proc.buf, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @open_proc.buf, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* %5
ret i32 %16
}

LLVM IR (-O2):
@open_proc.buf = internal global [100 x i8] zeroinitializer
@.str = [12 x i8] c"/proc/%d/%s\00"
@O_RDONLY = global i32 0
define i32 @open_proc(i32 %0, i8* %1) {
%3 = tail call i32 @snprintf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @open_proc.buf, i64 0, i64 0), i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @open_proc.buf, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @open_proc.buf, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
ret i32 %5
}