#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*/ pid_t;

/* Variables and functions */
int O_CLOEXEC;
int O_WRONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
scalar_t__ dprintf(int, char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ progress(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char const *, int /*<<< orphan*/);
void writemap(const char *fmt, pid_t kid, int id) {
  char file[PATH_MAX];
  snprintf(file, sizeof file, fmt, kid);
  int fd = open(file, O_CLOEXEC | O_WRONLY);
  if (fd < 0)
    error(1, errno, "can't open %s", file);
  if (dprintf(fd, "0 %d 1\n", id) < 0)
    error(1, errno, "i/o error while writing to %s", file);
  close(fd);
  progress("parent: remapped UID/GID %d to 0 for child namespace.\n", id);
}


x86 assembly (clang, -O0):
<writemap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1040 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f2 <writemap+0x92>
mov    -0x30(%rbp),%rcx
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe13(%rip),%rdx
call   1070 <error@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%edx
lea    0xe0f(%rip),%rsi
call   1060 <dprintf@plt>
cmp    $0x0,%rax
jge    122c <writemap+0xcc>
mov    -0x30(%rbp),%rcx
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdef(%rip),%rdx
call   1070 <error@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%esi
lea    0xdf6(%rip),%rdi
call   1030 <progress@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writemap>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ecx
mov    %rdi,%rdx
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1040 <snprintf@plt>
mov    0x2e22(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11d5 <writemap+0x75>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe38(%rip),%rdx
mov    $0x1,%edi
mov    %rbx,%rcx
call   1070 <error@plt>
lea    0xe32(%rip),%rsi
mov    %r15d,%edi
mov    %r14d,%edx
call   1060 <dprintf@plt>
test   %rax,%rax
jns    1209 <writemap+0xa9>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdx
mov    $0x1,%edi
mov    %rbx,%rcx
call   1070 <error@plt>
mov    %r15d,%edi
call   1050 <close@plt>
lea    0xe1c(%rip),%rdi
mov    %r14d,%esi
call   1030 <progress@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@O_CLOEXEC = global i32 0
@O_WRONLY = global i32 0
@errno = global i32 0
@.str = [14 x i8] c"can't open %s\00"
@.str.1 = [8 x i8] c"0 %d 1\0A\00"
@.str.2 = [30 x i8] c"i/o error while writing to %s\00"
@.str.3 = [55 x i8] c"parent: remapped UID/GID %d to 0 for child namespace.\0A\00"
define void @writemap(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = trunc i64 %11 to i32
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i32 @snprintf(i8* %13, i32 %14, i8* %15, i32 %16)
%18 = load i32, i32* @O_CLOEXEC
%19 = load i32, i32* @O_WRONLY
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %13, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %3
%25 = load i32, i32* @errno
%26 = call i32 @error(i32 1, i32 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %27
27: ; preds = %24, %3
%28 = load i32, i32* %9
%29 = load i32, i32* %6
%30 = call i64 @dprintf(i32 %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* @errno
%34 = call i32 @error(i32 1, i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %35
35: ; preds = %32, %27
%36 = load i32, i32* %9
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %6
%39 = call i32 @progress(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@O_CLOEXEC = global i32 0
@O_WRONLY = global i32 0
@errno = global i32 0
@.str = [14 x i8] c"can't open %s\00"
@.str.1 = [8 x i8] c"0 %d 1\0A\00"
@.str.2 = [30 x i8] c"i/o error while writing to %s\00"
@.str.3 = [55 x i8] c"parent: remapped UID/GID %d to 0 for child namespace.\0A\00"
define void @writemap(i8* %0, i32 %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @snprintf(i8* %6, i32 %4, i8* %0, i32 %1)
%8 = load i32, i32* @O_CLOEXEC
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %9, %8
%11 = call i32 @open(i8* %6, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @errno
%15 = call i32 @error(i32 1, i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %16
16: ; preds = %13, %3
%17 = call i64 @dprintf(i32 %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = call i32 @error(i32 1, i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %22
22: ; preds = %19, %16
%23 = call i32 @close(i32 %11)
%24 = call i32 @progress(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i32 %2)
ret void
}