#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 scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ __mp_getenv(char *);
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ putenv(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int __mp_editfile(char *f, unsigned long l, int d) {
  char s[256];
  char t[32];
  char *v[5];
  pid_t p;
  int r;
  sprintf(s, "%s=", "LD_PRELOAD");
  sprintf(t, "%lu", l);
  if ((p = fork()) < 0)
    return -1;
  if (p == 0) {
    if (__mp_getenv("LD_PRELOAD"))
      putenv(s);
    v[0] = "mpedit";
    if (d == 0) {
      v[1] = f;
      v[2] = t;
      v[3] = NULL;
    } else {
      v[1] = "--listing";
      v[2] = f;
      v[3] = t;
      v[4] = NULL;
    }
    execvp(v[0], v);
    _exit(EXIT_FAILURE);
  }
  while (waitpid(p, &r, 0) < 0)
    if (errno != EINTR)
      return -1;
  if (!WIFEXITED(r) || (WEXITSTATUS(r) != 0))
    return -1;
  return 1;
}


x86 assembly (clang, -O0):
<__mp_editfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x120(%rbp),%rdi
lea    0xe4c(%rip),%rsi
lea    0xe49(%rip),%rdx
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x140(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x178(%rbp)
cmp    $0x0,%rax
jge    11ff <__mp_editfile+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <__mp_editfile+0x1be>
cmpq   $0x0,-0x178(%rbp)
jne    12c3 <__mp_editfile+0x133>
lea    0xdf0(%rip),%rdi
call   1050 <__mp_getenv@plt>
cmp    $0x0,%rax
je     122f <__mp_editfile+0x9f>
lea    -0x120(%rbp),%rdi
call   1040 <putenv@plt>
lea    0xddd(%rip),%rax
mov    %rax,-0x170(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1270 <__mp_editfile+0xe0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x160(%rbp)
movq   $0x0,-0x158(%rbp)
jmp    12a2 <__mp_editfile+0x112>
lea    0xda3(%rip),%rax
mov    %rax,-0x168(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x160(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x158(%rbp)
movq   $0x0,-0x150(%rbp)
mov    -0x170(%rbp),%rdi
lea    -0x170(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    12c8 <__mp_editfile+0x138>
mov    -0x178(%rbp),%rdi
lea    -0x17c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0x0,%rax
jge    1312 <__mp_editfile+0x182>
mov    0x2cf2(%rip),%rax
mov    (%rax),%rax
mov    0x2cd8(%rip),%rcx
cmp    (%rcx),%rax
je     130d <__mp_editfile+0x17d>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <__mp_editfile+0x1be>
jmp    12c8 <__mp_editfile+0x138>
mov    -0x17c(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
je     133b <__mp_editfile+0x1ab>
mov    -0x17c(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     1347 <__mp_editfile+0x1b7>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <__mp_editfile+0x1be>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mp_editfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x158,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe4f(%rip),%rsi
lea    0xe4c(%rip),%rdx
lea    0x50(%rsp),%rdi
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    0xe44(%rip),%rsi
lea    0x30(%rsp),%r12
mov    %r12,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
js     124b <__mp_editfile+0xbb>
je     1260 <__mp_editfile+0xd0>
mov    %rax,%rbx
mov    %rsp,%r14
mov    0x2de2(%rip),%r15
mov    0x2dcb(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %rax,%rax
jns    122d <__mp_editfile+0x9d>
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
je     1210 <__mp_editfile+0x80>
jmp    124b <__mp_editfile+0xbb>
mov    (%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
je     124b <__mp_editfile+0xbb>
mov    (%rsp),%edi
call   1060 <WEXITSTATUS@plt>
neg    %rax
sbb    %r13d,%r13d
or     $0x1,%r13d
mov    %r13d,%eax
add    $0x158,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9d(%rip),%rdi
call   1050 <__mp_getenv@plt>
test   %rax,%rax
je     127b <__mp_editfile+0xeb>
lea    0x50(%rsp),%rdi
call   1040 <putenv@plt>
lea    0xd91(%rip),%rax
mov    %rax,(%rsp)
test   %r15d,%r15d
jne    1292 <__mp_editfile+0x102>
xor    %eax,%eax
mov    %r14,%rcx
jmp    12aa <__mp_editfile+0x11a>
lea    0x30(%rsp),%rax
movq   $0x0,0x20(%rsp)
lea    0xd73(%rip),%rcx
mov    %r14,%r12
mov    %rcx,0x8(%rsp)
mov    %r12,0x10(%rsp)
mov    %rax,0x18(%rsp)
lea    0xd53(%rip),%rdi
mov    %rsp,%rsi
call   1080 <execvp@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s=\00"
@.str.1 = [11 x i8] c"LD_PRELOAD\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [7 x i8] c"mpedit\00"
@.str.4 = [10 x i8] c"--listing\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @__mp_editfile(i8* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca [32 x i8]
%10 = alloca [5 x i8*]
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%14 = call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%16 = load i64, i64* %6
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %16)
%18 = call i64 (...) @fork()
store i64 %18, i64* %11
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %75
21: ; preds = %3
%22 = load i64, i64* %11
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %54
24: ; preds = %21
%25 = call i64 @__mp_getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%29 = call i32 @putenv(i8* %28)
br label %30
30: ; preds = %27, %24
%31 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %31
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i8*, i8** %5
%36 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 1
store i8* %35, i8** %36
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%38 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 2
store i8* %37, i8** %38
%39 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 3
store i8* null, i8** %39
br label %47
40: ; preds = %30
%41 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %41
%42 = load i8*, i8** %5
%43 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 2
store i8* %42, i8** %43
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%45 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 3
store i8* %44, i8** %45
%46 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 4
store i8* null, i8** %46
br label %47
47: ; preds = %40, %34
%48 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 0
%49 = load i8*, i8** %48
%50 = getelementptr inbounds [5 x i8*], [5 x i8*]* %10, i64 0, i64 0
%51 = call i32 @execvp(i8* %49, i8** %50)
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @_exit(i32 %52)
unreachable
54: ; preds = %21
br label %55
55: ; preds = %64, %54
%56 = load i64, i64* %11
%57 = call i64 @waitpid(i64 %56, i32* %12, i32 0)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %55
%60 = load i64, i64* @errno
%61 = load i64, i64* @EINTR
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %59
store i32 -1, i32* %4
br label %75
64: ; preds = %59
br label %55
65: ; preds = %55
%66 = load i32, i32* %12
%67 = call i32 @WIFEXITED(i32 %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %65
%70 = load i32, i32* %12
%71 = call i64 @WEXITSTATUS(i32 %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %69, %65
store i32 -1, i32* %4
br label %75
74: ; preds = %69
store i32 1, i32* %4
br label %75
75: ; preds = %74, %73, %63, %20
%76 = load i32, i32* %4
ret i32 %76
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s=\00"
@.str.1 = [11 x i8] c"LD_PRELOAD\00"
@.str.2 = [4 x i8] c"%lu\00"
@.str.3 = [7 x i8] c"mpedit\00"
@.str.4 = [10 x i8] c"--listing\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @__mp_editfile(i8* %0, i64 %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = alloca [32 x i8]
%6 = alloca [5 x i8*]
%7 = alloca i32
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%10 = bitcast [5 x i8*]* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %1)
%14 = tail call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %54, label %16
16: ; preds = %3
%17 = icmp eq i64 %14, 0
br i1 %17, label %18, label %38
18: ; preds = %16
%19 = tail call i64 @__mp_getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i32 @putenv(i8* %8)
br label %23
23: ; preds = %21, %18
%24 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %24
%25 = icmp eq i32 %2, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 4
store i8* null, i8** %27
br label %28
28: ; preds = %23, %26
%29 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %26 ], [ %0, %23 ]
%30 = phi i8* [ %0, %26 ], [ %9, %23 ]
%31 = phi i8* [ %9, %26 ], [ null, %23 ]
%32 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 1
store i8* %29, i8** %32
%33 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 2
store i8* %30, i8** %33
%34 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 3
store i8* %31, i8** %34
%35 = call i32 @execvp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %24)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @_exit(i32 %36)
unreachable
38: ; preds = %16, %41
%39 = call i64 @waitpid(i64 %14, i32* %7, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp eq i64 %42, %43
br i1 %44, label %38, label %54
45: ; preds = %38
%46 = load i32, i32* %7
%47 = call i32 @WIFEXITED(i32 %46)
%48 = icmp eq i32 %47, 0
br i1 %48, label %54, label %49
49: ; preds = %45
%50 = load i32, i32* %7
%51 = call i64 @WEXITSTATUS(i32 %50)
%52 = icmp eq i64 %51, 0
%53 = select i1 %52, i32 1, i32 -1
br label %54
54: ; preds = %41, %49, %45, %3
%55 = phi i32 [ -1, %3 ], [ -1, %45 ], [ %53, %49 ], [ -1, %41 ]
ret i32 %55
}