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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ assert_perror(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int, ...);
int write(int, char *, int);
void write_id_map(pid_t pid, const char *file, int id) {
  char id_map_file[100];
  snprintf(id_map_file, sizeof id_map_file, "/proc/%d/%s", pid, file);
  char id_map[100];
  int len = snprintf(id_map, sizeof id_map, "%d %d 1\n", id, id);
  int fd = open(id_map_file, O_WRONLY);
  if (fd < 0)
    assert_perror(errno);
  int n = write(fd, id_map, len);
  if (n < 0)
    assert_perror(errno);
  close(fd);
}


x86 assembly (clang, -O0):
<write_id_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x64,%esi
lea    0xe84(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0xf0(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    $0x64,%esi
lea    0xe6f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0xf4(%rbp)
lea    -0x80(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xf8(%rbp)
cmpl   $0x0,-0xf8(%rbp)
jge    11dd <write_id_map+0x8d>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
mov    -0xf8(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    -0xf4(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0xfc(%rbp)
cmpl   $0x0,-0xfc(%rbp)
jge    1216 <write_id_map+0xc6>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
mov    -0xf8(%rbp),%edi
call   1060 <close@plt>
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_id_map>:
push   %rbp
push   %r14
push   %rbx
sub    $0xe0,%rsp
mov    %edx,%ebx
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xe97(%rip),%rdx
lea    0x70(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0xe88(%rip),%rdx
mov    %rsp,%rdi
mov    $0x64,%esi
mov    %ebx,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %eax,%ebp
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11bf <write_id_map+0x6f>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11dd <write_id_map+0x8d>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0xe0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/proc/%d/%s\00"
@.str.1 = [9 x i8] c"%d %d 1\0A\00"
@O_WRONLY = global i32 0
@errno = global i32 0
define void @write_id_map(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [100 x i8]
%8 = alloca [100 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %4
%14 = load i8*, i8** %5
%15 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %12, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14)
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%17 = load i32, i32* %6
%18 = load i32, i32* %6
%19 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %16, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
store i32 %19, i32* %9
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %3
%26 = load i32, i32* @errno
%27 = call i32 @assert_perror(i32 %26)
br label %28
28: ; preds = %25, %3
%29 = load i32, i32* %10
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%31 = load i32, i32* %9
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i32, i32* @errno
%37 = call i32 @assert_perror(i32 %36)
br label %38
38: ; preds = %35, %28
%39 = load i32, i32* %10
%40 = call i32 @close(i32 %39)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"/proc/%d/%s\00"
@.str.1 = [9 x i8] c"%d %d 1\0A\00"
@O_WRONLY = global i32 0
@errno = global i32 0
define void @write_id_map(i32 %0, i8* %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%9 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %8, i32 100, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %2)
%10 = load i32, i32* @O_WRONLY
%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 @assert_perror(i32 %14)
br label %16
16: ; preds = %13, %3
%17 = call i32 @write(i32 %11, i8* %8, i32 %9)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = call i32 @assert_perror(i32 %20)
br label %22
22: ; preds = %19, %16
%23 = call i32 @close(i32 %11)
ret void
}