#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int libamg_process_check_flag(char *flagname) {
  char filename[256];
  int fd;
  sprintf(filename, "%s%s", "/var/run/flags/", flagname);
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    return 0;
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<libamg_process_check_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xeaf(%rip),%rsi
lea    0xead(%rip),%rdx
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1191 <libamg_process_check_flag+0x61>
movl   $0x0,-0x4(%rbp)
jmp    11a3 <libamg_process_check_flag+0x73>
mov    -0x114(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libamg_process_check_flag>:
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rcx
lea    0xebe(%rip),%rsi
lea    0xebc(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %eax,%eax
js     1177 <libamg_process_check_flag+0x47>
mov    %eax,%edi
call   1030 <close@plt>
mov    $0x1,%eax
jmp    1179 <libamg_process_check_flag+0x49>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [16 x i8] c"/var/run/flags/\00"
@O_RDONLY = global i32 0
define i32 @libamg_process_check_flag(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i32
store i8* %0, i8** %3
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i8*, i8** %3
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 0, i32* %2
br label %18
15: ; preds = %1
%16 = load i32, i32* %5
%17 = call i32 @close(i32 %16)
store i32 1, i32* %2
br label %18
18: ; preds = %15, %14
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [16 x i8] c"/var/run/flags/\00"
@O_RDONLY = global i32 0
define i32 @libamg_process_check_flag(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @close(i32 %6)
br label %10
10: ; preds = %1, %8
%11 = phi i32 [ 1, %8 ], [ 0, %1 ]
ret i32 %11
}