#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__ uid_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
scalar_t__ asprintf(char **, char *, char, int, scalar_t__, scalar_t__);
int /*<<< orphan*/ errno;
scalar_t__ find_one_subxid(char);
scalar_t__ getgid();
scalar_t__ getuid();
char *deduce_xid_helper(int pid, char x) {
  uid_t xid;
  switch (x) {
  case 'u':
    xid = getuid();
    break;
  case 'g':
    xid = getgid();
    break;
  default:
    errno = EINVAL;
    return NULL;
  }
  uid_t subxid = find_one_subxid(x);
  if (subxid == (uid_t)-1)
    return NULL;
  char *helper;
  if (asprintf(&helper, "new%cidmap %d 0 %u 1 1 %u 1", x, pid, xid, subxid) < 0)
    return NULL;
  return helper;
}


x86 assembly (clang, -O0):
<deduce_xid_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x67,%eax
je     1187 <deduce_xid_helper+0x47>
jmp    1166 <deduce_xid_helper+0x26>
mov    -0x2c(%rbp),%eax
sub    $0x75,%eax
jne    1197 <deduce_xid_helper+0x57>
jmp    1177 <deduce_xid_helper+0x37>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,-0x18(%rbp)
jmp    11b6 <deduce_xid_helper+0x76>
mov    $0x0,%al
call   1050 <getgid@plt>
mov    %rax,-0x18(%rbp)
jmp    11b6 <deduce_xid_helper+0x76>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e39(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    121b <deduce_xid_helper+0xdb>
movsbl -0xd(%rbp),%edi
call   1040 <find_one_subxid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11db <deduce_xid_helper+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    121b <deduce_xid_helper+0xdb>
mov    -0xd(%rbp),%al
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
lea    -0x28(%rbp),%rdi
lea    0xe0c(%rip),%rsi
movsbl %al,%edx
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jge    1213 <deduce_xid_helper+0xd3>
movq   $0x0,-0x8(%rbp)
jmp    121b <deduce_xid_helper+0xdb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deduce_xid_helper>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
cmp    $0x67,%esi
je     1160 <deduce_xid_helper+0x20>
cmp    $0x75,%ebp
jne    11a3 <deduce_xid_helper+0x63>
xor    %eax,%eax
call   1030 <getuid@plt>
jmp    1167 <deduce_xid_helper+0x27>
xor    %eax,%eax
call   1050 <getgid@plt>
mov    %rax,%rbx
movsbl %bpl,%ebp
mov    %ebp,%edi
call   1040 <find_one_subxid@plt>
cmp    $0xffffffffffffffff,%rax
je     11b5 <deduce_xid_helper+0x75>
lea    0xe7e(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %rbx,%r8
mov    %rax,%r9
call   1060 <asprintf@plt>
test   %rax,%rax
js     11b5 <deduce_xid_helper+0x75>
mov    0x8(%rsp),%rax
jmp    11b7 <deduce_xid_helper+0x77>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    0x2e2d(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [28 x i8] c"new%cidmap %d 0 %u 1 1 %u 1\00"
define i8* @deduce_xid_helper(i32 %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %4
store i8 %1, i8* %5
%9 = load i8, i8* %5
%10 = sext i8 %9 to i32
switch i32 %10, label %15 [
i32 117, label %11
i32 103, label %13
]
11: ; preds = %2
%12 = call i64 (...) @getuid()
store i64 %12, i64* %6
br label %17
13: ; preds = %2
%14 = call i64 (...) @getgid()
store i64 %14, i64* %6
br label %17
15: ; preds = %2
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
store i8* null, i8** %3
br label %33
17: ; preds = %13, %11
%18 = load i8, i8* %5
%19 = call i64 @find_one_subxid(i8 signext %18)
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %17
store i8* null, i8** %3
br label %33
23: ; preds = %17
%24 = load i8, i8* %5
%25 = load i32, i32* %4
%26 = load i64, i64* %6
%27 = load i64, i64* %7
%28 = call i64 @asprintf(i8** %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8 signext %24, i32 %25, i64 %26, i64 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
store i8* null, i8** %3
br label %33
31: ; preds = %23
%32 = load i8*, i8** %8
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %30, %22, %15
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [28 x i8] c"new%cidmap %d 0 %u 1 1 %u 1\00"
define i8* @deduce_xid_helper(i32 %0, i8 signext %1) {
%3 = alloca i8*
%4 = sext i8 %1 to i32
switch i32 %4, label %9 [
i32 117, label %5
i32 103, label %7
]
5: ; preds = %2
%6 = tail call i64 (...) @getuid()
br label %11
7: ; preds = %2
%8 = tail call i64 (...) @getgid()
br label %11
9: ; preds = %2
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
br label %21
11: ; preds = %7, %5
%12 = phi i64 [ %8, %7 ], [ %6, %5 ]
%13 = tail call i64 @find_one_subxid(i8 signext %1)
%14 = icmp eq i64 %13, -1
br i1 %14, label %21, label %15
15: ; preds = %11
%16 = bitcast i8** %3 to i8*
%17 = call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8 signext %1, i32 %0, i64 %12, i64 %13)
%18 = icmp slt i64 %17, 0
%19 = load i8*, i8** %3
%20 = select i1 %18, i8* null, i8* %19
br label %21
21: ; preds = %15, %11, %9
%22 = phi i8* [ null, %9 ], [ %20, %15 ], [ null, %11 ]
ret i8* %22
}