#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 int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ append(char **, char *, char *, unsigned int, unsigned int,
                          unsigned int);
int /*<<< orphan*/ die(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
int sscanf(char *, char *, unsigned int *, unsigned int *, unsigned int *);
char *string(char *, ...);
char *getmap(pid_t pid, int type) {
  char *line = NULL, *result = NULL, *path;
  size_t size;
  unsigned count, first, lower;
  FILE *file;
  if (pid == -1)
    path = string("/proc/self/%s", ((type) == 0 ? "gid_map" : "uid_map"));
  else
    path = string("/proc/%d/%s", pid, ((type) == 0 ? "gid_map" : "uid_map"));
  if (!(file = fopen(path, "r")))
    die(0, "Cannot read %s", path);
  while (getline(&line, &size, file) >= 0) {
    if (sscanf(line, " %u %u %u", &first, &lower, &count) != 3)
      die(0, "Invalid map data in %s", path);
    append(&result, "%s%u:%u:%u", result ? "," : "", first, lower, count);
  }
  if (!result)
    die(0, "Invalid map data in %s", path);
  fclose(file);
  free(line);
  free(path);
  return result;
}


x86 assembly (clang, -O0):
<getmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11d7 <getmap+0x57>
mov    -0x8(%rbp),%ecx
lea    0xe64(%rip),%rsi
lea    0xe55(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <string@plt>
mov    %rax,-0x20(%rbp)
jmp    1204 <getmap+0x84>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%ecx
lea    0xe32(%rip),%rdx
lea    0xe23(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rdx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1060 <string@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1234 <getmap+0xb4>
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
lea    0xdfd(%rip),%rsi
call   1090 <die@plt>
jmp    1239 <getmap+0xb9>
mov    -0x40(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <getline@plt>
cmp    $0x0,%rax
jl     12c5 <getmap+0x145>
mov    -0x10(%rbp),%rdi
lea    0xddc(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1070 <sscanf@plt>
cmp    $0x3,%eax
je     128b <getmap+0x10b>
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
lea    0xdbf(%rip),%rsi
call   1090 <die@plt>
mov    -0x18(%rbp),%rcx
lea    0xdd2(%rip),%rdx
lea    0xdca(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rdx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
lea    -0x18(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1040 <append@plt>
jmp    1239 <getmap+0xb9>
cmpq   $0x0,-0x18(%rbp)
jne    12e2 <getmap+0x162>
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
lea    0xd68(%rip),%rsi
call   1090 <die@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getmap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
test   %esi,%esi
lea    0xe65(%rip),%rcx
lea    0xe66(%rip),%rdx
cmove  %rcx,%rdx
cmp    $0xffffffff,%edi
je     11cd <getmap+0x4d>
mov    %edi,%eax
lea    0xe5c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <string@plt>
jmp    11de <getmap+0x5e>
lea    0xe2c(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1060 <string@plt>
mov    %rax,%rbp
lea    0xe42(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1209 <getmap+0x89>
lea    0xe2d(%rip),%rsi
xor    %edi,%edi
mov    %rbp,%rdx
call   1090 <die@plt>
lea    0x10(%rsp),%rdi
lea    0x30(%rsp),%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
test   %rax,%rax
js     12d7 <getmap+0x157>
mov    %rbp,0x28(%rsp)
lea    0xe37(%rip),%rbp
lea    0xe25(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0x30(%rsp),%r13
jmp    1290 <getmap+0x110>
nopl   0x0(%rax,%rax,1)
cmpq   $0x0,0x8(%rsp)
mov    %rbp,%rdx
lea    0xe08(%rip),%rax
cmove  %rax,%rdx
mov    0x20(%rsp),%ecx
mov    0x1c(%rsp),%r8d
mov    0x24(%rsp),%r9d
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <append@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
test   %rax,%rax
js     12ca <getmap+0x14a>
mov    0x10(%rsp),%rdi
lea    0xd9f(%rip),%rsi
lea    0x20(%rsp),%rdx
lea    0x1c(%rsp),%rcx
lea    0x24(%rsp),%r8
call   1070 <sscanf@plt>
cmp    $0x3,%eax
je     1250 <getmap+0xd0>
xor    %edi,%edi
lea    0xd87(%rip),%rsi
mov    0x28(%rsp),%rdx
call   1090 <die@plt>
jmp    1250 <getmap+0xd0>
cmpq   $0x0,0x8(%rsp)
mov    0x28(%rsp),%rbp
jne    12e8 <getmap+0x168>
lea    0xd67(%rip),%rsi
xor    %edi,%edi
mov    %rbp,%rdx
call   1090 <die@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/self/%s\00"
@.str.1 = [8 x i8] c"gid_map\00"
@.str.2 = [8 x i8] c"uid_map\00"
@.str.3 = [12 x i8] c"/proc/%d/%s\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [15 x i8] c"Cannot read %s\00"
@.str.6 = [10 x i8] c" %u %u %u\00"
@.str.7 = [23 x i8] c"Invalid map data in %s\00"
@.str.8 = [11 x i8] c"%s%u:%u:%u\00"
@.str.9 = [2 x i8] c",\00"
@.str.10 = [1 x i8] zeroinitializer
define i8* @getmap(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i8* null, i8** %5
store i8* null, i8** %6
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0)
%20 = call i8* (i8*, ...) @string(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %19)
store i8* %20, i8** %7
br label %28
21: ; preds = %2
%22 = load i32, i32* %3
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0)
%27 = call i8* (i8*, ...) @string(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %22, i8* %26)
store i8* %27, i8** %7
br label %28
28: ; preds = %21, %15
%29 = load i8*, i8** %7
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %30, i32** %12
%31 = icmp ne i32* %30, null
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i8*, i8** %7
%34 = call i32 @die(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %28
br label %36
36: ; preds = %47, %35
%37 = load i32*, i32** %12
%38 = call i64 @getline(i8** %5, i64* %8, i32* %37)
%39 = icmp sge i64 %38, 0
br i1 %39, label %40, label %56
40: ; preds = %36
%41 = load i8*, i8** %5
%42 = call i32 @sscanf(i8* %41, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32* %10, i32* %11, i32* %9)
%43 = icmp ne i32 %42, 3
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i8*, i8** %7
%46 = call i32 @die(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %44, %40
%48 = load i8*, i8** %6
%49 = icmp ne i8* %48, null
%50 = zext i1 %49 to i64
%51 = select i1 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0)
%52 = load i32, i32* %10
%53 = load i32, i32* %11
%54 = load i32, i32* %9
%55 = call i32 @append(i8** %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i8* %51, i32 %52, i32 %53, i32 %54)
br label %36
56: ; preds = %36
%57 = load i8*, i8** %6
%58 = icmp ne i8* %57, null
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = load i8*, i8** %7
%61 = call i32 @die(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %59, %56
%63 = load i32*, i32** %12
%64 = call i32 @fclose(i32* %63)
%65 = load i8*, i8** %5
%66 = call i32 @free(i8* %65)
%67 = load i8*, i8** %7
%68 = call i32 @free(i8* %67)
%69 = load i8*, i8** %6
ret i8* %69
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/self/%s\00"
@.str.1 = [8 x i8] c"gid_map\00"
@.str.2 = [8 x i8] c"uid_map\00"
@.str.3 = [12 x i8] c"/proc/%d/%s\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [15 x i8] c"Cannot read %s\00"
@.str.6 = [10 x i8] c" %u %u %u\00"
@.str.7 = [23 x i8] c"Invalid map data in %s\00"
@.str.8 = [11 x i8] c"%s%u:%u:%u\00"
@.str.9 = [2 x i8] c",\00"
@.str.10 = [1 x i8] zeroinitializer
define i8* @getmap(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%10 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%11 = bitcast i64* %5 to i8*
%12 = bitcast i32* %6 to i8*
%13 = bitcast i32* %7 to i8*
%14 = bitcast i32* %8 to i8*
%15 = icmp eq i32 %0, -1
%16 = icmp eq i32 %1, 0
%17 = select i1 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0)
br i1 %15, label %18, label %20
18: ; preds = %2
%19 = tail call i8* (i8*, ...) @string(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %22
20: ; preds = %2
%21 = tail call i8* (i8*, ...) @string(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %0, i8* %17)
br label %22
22: ; preds = %20, %18
%23 = phi i8* [ %19, %18 ], [ %21, %20 ]
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @die(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %23)
br label %28
28: ; preds = %26, %22
%29 = call i64 @getline(i8** %3, i64* %5, i32* %24)
%30 = icmp sgt i64 %29, -1
br i1 %30, label %31, label %50
31: ; preds = %28, %37
%32 = load i8*, i8** %3
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32* %7, i32* %8, i32* %6)
%34 = icmp eq i32 %33, 3
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = call i32 @die(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %23)
br label %37
37: ; preds = %35, %31
%38 = load i8*, i8** %4
%39 = icmp eq i8* %38, null
%40 = select i1 %39, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0)
%41 = load i32, i32* %7
%42 = load i32, i32* %8
%43 = load i32, i32* %6
%44 = call i32 @append(i8** %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i8* %40, i32 %41, i32 %42, i32 %43)
%45 = call i64 @getline(i8** %3, i64* %5, i32* %24)
%46 = icmp sgt i64 %45, -1
br i1 %46, label %31, label %47
47: ; preds = %37
%48 = load i8*, i8** %4
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %28, %47
%51 = call i32 @die(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %23)
br label %52
52: ; preds = %50, %47
%53 = call i32 @fclose(i32* %24)
%54 = load i8*, i8** %3
%55 = call i32 @free(i8* %54)
%56 = call i32 @free(i8* %23)
%57 = load i8*, i8** %4
ret i8* %57
}