#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_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ mk_file(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
scalar_t__ write(int, char *, int);
int test_modify_helper(char *path, void *dummy) {
  int fd;
  char buffer[1];
  if (mk_file(path, 4096) < 0)
    return -1;
  if ((fd = open(path, O_RDWR)) < 0) {
    fprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
    return -1;
  }
  if (write(fd, buffer, 1) < 0) {
    fprintf(stderr, "Cannot read %s: %s\n", path, strerror(errno));
    (void)unlink(path);
    return -1;
  }
  (void)unlink(path);
  return 0;
}


x86 assembly (clang, -O0):
<test_modify_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <mk_file@plt>
cmp    $0x0,%rax
jge    1194 <test_modify_helper+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <test_modify_helper+0x10e>
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11f6 <test_modify_helper+0x96>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <test_modify_helper+0x10e>
mov    -0x1c(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    125e <test_modify_helper+0xfe>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd0(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <test_modify_helper+0x10e>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_modify_helper>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    $0x1000,%esi
call   1050 <mk_file@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     121e <test_modify_helper+0xbe>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ba <test_modify_helper+0x5a>
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1040 <write@plt>
test   %rax,%rax
js     11e9 <test_modify_helper+0x89>
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %ebp,%ebp
jmp    121e <test_modify_helper+0xbe>
mov    0x2e17(%rip),%rax
mov    (%rax),%r14d
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe27(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
jmp    121e <test_modify_helper+0xbe>
mov    0x2de8(%rip),%rax
mov    (%rax),%r14d
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0c(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"Cannot read %s: %s\0A\00"
define i32 @test_modify_helper(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [1 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i64 @mk_file(i8* %8, i32 4096)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %39
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %4
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %21)
store i32 -1, i32* %3
br label %39
23: ; preds = %12
%24 = load i32, i32* %6
%25 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
%26 = call i64 @write(i32 %24, i8* %25, i32 1)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %4
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %32)
%34 = load i8*, i8** %4
%35 = call i32 @unlink(i8* %34)
store i32 -1, i32* %3
br label %39
36: ; preds = %23
%37 = load i8*, i8** %4
%38 = call i32 @unlink(i8* %37)
store i32 0, i32* %3
br label %39
39: ; preds = %36, %28, %17, %11
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Cannot open %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"Cannot read %s: %s\0A\00"
define i32 @test_modify_helper(i8* %0, i8* nocapture readnone %1) {
%3 = alloca [1 x i8]
%4 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @mk_file(i8* %0, i32 4096)
%6 = icmp slt i64 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %14)
br label %27
16: ; preds = %7
%17 = call i64 @write(i32 %9, i8* %4, i32 1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %22)
%24 = tail call i32 @unlink(i8* %0)
br label %27
25: ; preds = %16
%26 = tail call i32 @unlink(i8* %0)
br label %27
27: ; preds = %2, %25, %19, %11
%28 = phi i32 [ -1, %11 ], [ -1, %19 ], [ 0, %25 ], [ -1, %2 ]
ret i32 %28
}