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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long, ...);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write_file(char *, char *, int /*<<< orphan*/);
int map_ids(pid_t pid, unsigned long nsid, unsigned long hostid,
            unsigned long range) {
  char map[100], procfile[256];
  snprintf(procfile, sizeof(procfile), "/proc/%d/uid_map", pid);
  snprintf(map, sizeof(map), "%lu %lu %lu", nsid, hostid, range);
  if (write_file(procfile, map, strlen(map)))
    return -1;
  snprintf(procfile, sizeof(procfile), "/proc/%d/gid_map", pid);
  snprintf(map, sizeof(map), "%lu %lu %lu", nsid, hostid, range);
  if (write_file(procfile, map, strlen(map)))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<map_ids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x190(%rbp),%rdi
movslq -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe9f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x64,%esi
lea    0xe8a(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x190(%rbp),%rax
mov    %rax,-0x1a0(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x198(%rbp)
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1a0(%rbp),%rdi
mov    -0x198(%rbp),%rsi
mov    %eax,%edx
call   1040 <write_file@plt>
cmp    $0x0,%rax
je     11e1 <map_ids+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <map_ids+0x14f>
lea    -0x190(%rbp),%rdi
movslq -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe25(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    $0x64,%esi
lea    0xdf3(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x190(%rbp),%rax
mov    %rax,-0x1b0(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x1a8(%rbp)
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1b0(%rbp),%rdi
mov    -0x1a8(%rbp),%rsi
mov    %eax,%edx
call   1040 <write_file@plt>
cmp    $0x0,%rax
je     1278 <map_ids+0x148>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <map_ids+0x14f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_ids>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x178,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
movslq %edi,%rbx
lea    0xeac(%rip),%rdx
lea    0x70(%rsp),%r13
mov    %r13,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0xe9f(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14,%r9
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write_file@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1209 <map_ids+0xd9>
lea    0xe67(%rip),%rdx
lea    0x70(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0xe3d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14,%r9
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write_file@plt>
neg    %rax
sbb    %eax,%eax
add    $0x178,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/uid_map\00"
@.str.1 = [12 x i8] c"%lu %lu %lu\00"
@.str.2 = [17 x i8] c"/proc/%d/gid_map\00"
define i32 @map_ids(i32 %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca [100 x i8]
%11 = alloca [256 x i8]
store i32 %0, i32* %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %12, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %14)
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%17 = load i64, i64* %7
%18 = load i64, i64* %8
%19 = load i64, i64* %9
%20 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %16, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %17, i64 %18, i64 %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = call i64 @write_file(i8* %21, i8* %22, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %4
store i32 -1, i32* %5
br label %46
28: ; preds = %4
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %29, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %31)
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%34 = load i64, i64* %7
%35 = load i64, i64* %8
%36 = load i64, i64* %9
%37 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %33, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %34, i64 %35, i64 %36)
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = call i64 @write_file(i8* %38, i8* %39, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %28
store i32 -1, i32* %5
br label %46
45: ; preds = %28
store i32 0, i32* %5
br label %46
46: ; preds = %45, %44, %27
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/uid_map\00"
@.str.1 = [12 x i8] c"%lu %lu %lu\00"
@.str.2 = [17 x i8] c"/proc/%d/gid_map\00"
define i32 @map_ids(i32 %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca [100 x i8]
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = sext i32 %0 to i64
%10 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %9)
%11 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %7, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %1, i64 %2, i64 %3)
%12 = call i32 @strlen(i8* %7)
%13 = call i64 @write_file(i8* %8, i8* %7, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %9)
%17 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %7, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %1, i64 %2, i64 %3)
%18 = call i32 @strlen(i8* %7)
%19 = call i64 @write_file(i8* %8, i8* %7, i32 %18)
%20 = icmp ne i64 %19, 0
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %15, %4
%23 = phi i32 [ -1, %4 ], [ %21, %15 ]
ret i32 %23
}