#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 EPERM;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ asprintf(char **, char *, char, int, char const *);
char *deduce_xid_helper(int, char);
int errno;
int /*<<< orphan*/ free(char *);
int system(char *);
int new_xid_map(int pid, char x, const char *id_map) {
  char *helper;
  if (id_map) {
    if (asprintf(&helper, "new%cidmap %d %s", x, pid, id_map) < 0)
      return -1;
  } else {
    if (!(helper = deduce_xid_helper(pid, x)))
      return -1;
  }
  int status = system(helper);
  int err = errno;
  free(helper);
  if (status == -1) {
    errno = err;
    return -1;
  }
  if (!WIFEXITED(status) || WEXITSTATUS(status)) {
    errno = EPERM;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<new_xid_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b8 <new_xid_map+0x58>
mov    -0x9(%rbp),%al
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%r8
lea    -0x20(%rbp),%rdi
lea    0xe6b(%rip),%rsi
movsbl %al,%edx
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jge    11b3 <new_xid_map+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <new_xid_map+0x10e>
jmp    11e3 <new_xid_map+0x83>
mov    -0x8(%rbp),%edi
movsbl -0x9(%rbp),%esi
call   1050 <deduce_xid_helper@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11de <new_xid_map+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <new_xid_map+0x10e>
jmp    11e3 <new_xid_map+0x83>
mov    -0x20(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1226 <new_xid_map+0xc6>
mov    -0x28(%rbp),%ecx
mov    0x2dc8(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <new_xid_map+0x10e>
mov    -0x24(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1249 <new_xid_map+0xe9>
mov    -0x24(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     1267 <new_xid_map+0x107>
mov    0x2d80(%rip),%rax
mov    (%rax),%ecx
mov    0x2d87(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <new_xid_map+0x10e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_xid_map>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ecx
test   %rdx,%rdx
je     1193 <new_xid_map+0x33>
mov    %rdx,%r8
movsbl %sil,%edx
lea    0xe83(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
test   %rax,%rax
js     11ee <new_xid_map+0x8e>
mov    0x8(%rsp),%rdi
jmp    11ab <new_xid_map+0x4b>
movsbl %sil,%esi
mov    %ecx,%edi
call   1050 <deduce_xid_helper@plt>
mov    %rax,%rdi
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     11ee <new_xid_map+0x8e>
call   1040 <system@plt>
mov    %eax,%ebx
mov    0x2e27(%rip),%r14
mov    (%r14),%ebp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
je     11eb <new_xid_map+0x8b>
mov    %ebx,%edi
call   1080 <WIFEXITED@plt>
test   %eax,%eax
je     11e2 <new_xid_map+0x82>
mov    %ebx,%edi
call   1070 <WEXITSTATUS@plt>
test   %rax,%rax
je     11fc <new_xid_map+0x9c>
mov    0x2de7(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,(%r14)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    11f3 <new_xid_map+0x93>

LLVM IR (-O0):
@.str = [17 x i8] c"new%cidmap %d %s\00"
@errno = global i32 0
@EPERM = global i32 0
define i32 @new_xid_map(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8 %1, i8* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %21
13: ; preds = %3
%14 = load i8, i8* %6
%15 = load i32, i32* %5
%16 = load i8*, i8** %7
%17 = call i64 @asprintf(i8** %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 signext %14, i32 %15, i8* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %4
br label %49
20: ; preds = %13
br label %28
21: ; preds = %3
%22 = load i32, i32* %5
%23 = load i8, i8* %6
%24 = call i8* @deduce_xid_helper(i32 %22, i8 signext %23)
store i8* %24, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %21
store i32 -1, i32* %4
br label %49
27: ; preds = %21
br label %28
28: ; preds = %27, %20
%29 = load i8*, i8** %8
%30 = call i32 @system(i8* %29)
store i32 %30, i32* %9
%31 = load i32, i32* @errno
store i32 %31, i32* %10
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = load i32, i32* %10
store i32 %37, i32* @errno
store i32 -1, i32* %4
br label %49
38: ; preds = %28
%39 = load i32, i32* %9
%40 = call i32 @WIFEXITED(i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* %9
%44 = call i64 @WEXITSTATUS(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42, %38
%47 = load i32, i32* @EPERM
store i32 %47, i32* @errno
store i32 -1, i32* %4
br label %49
48: ; preds = %42
store i32 0, i32* %4
br label %49
49: ; preds = %48, %46, %36, %26, %19
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@.str = [17 x i8] c"new%cidmap %d %s\00"
@errno = global i32 0
@EPERM = global i32 0
define i32 @new_xid_map(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = icmp eq i8* %2, null
br i1 %6, label %12, label %7
7: ; preds = %3
%8 = call i64 @asprintf(i8** %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 signext %1, i32 %0, i8* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %7
%11 = load i8*, i8** %4
br label %15
12: ; preds = %3
%13 = tail call i8* @deduce_xid_helper(i32 %0, i8 signext %1)
store i8* %13, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %32, label %15
15: ; preds = %10, %12
%16 = phi i8* [ %11, %10 ], [ %13, %12 ]
%17 = call i32 @system(i8* %16)
%18 = load i32, i32* @errno
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
%21 = icmp eq i32 %17, -1
br i1 %21, label %30, label %22
22: ; preds = %15
%23 = call i32 @WIFEXITED(i32 %17)
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = call i64 @WEXITSTATUS(i32 %17)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25, %22
%29 = load i32, i32* @EPERM
br label %30
30: ; preds = %15, %28
%31 = phi i32 [ %29, %28 ], [ %18, %15 ]
store i32 %31, i32* @errno
br label %32
32: ; preds = %30, %25, %12, %7
%33 = phi i32 [ -1, %7 ], [ -1, %12 ], [ 0, %25 ], [ -1, %30 ]
ret i32 %33
}