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


x86 assembly (clang, -O0):
<libamg_process_set_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    0x2e8d(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    119a <libamg_process_set_flag+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <libamg_process_set_flag+0x7c>
mov    -0x114(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libamg_process_set_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    0x2e9d(%rip),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %eax,%eax
js     117d <libamg_process_set_flag+0x4d>
mov    %eax,%edi
call   1030 <close@plt>
xor    %eax,%eax
jmp    1182 <libamg_process_set_flag+0x52>
mov    $0xffffffff,%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_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @libamg_process_set_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_CREAT
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %20
17: ; preds = %1
%18 = load i32, i32* %5
%19 = call i32 @close(i32 %18)
store i32 0, i32* %2
br label %20
20: ; preds = %17, %16
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [16 x i8] c"/var/run/flags/\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @libamg_process_set_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_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %3, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = tail call i32 @close(i32 %8)
br label %12
12: ; preds = %1, %10
%13 = phi i32 [ 0, %10 ], [ -1, %1 ]
ret i32 %13
}