#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*/ str;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ snprintf(char *, int, char *, int, int);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int set_idmap(const char *path, int new_id, int env_id) {
  int fd;
  ssize_t ret, len;
  char str[64];
  if (snprintf(str, sizeof(str), "%u %u 1", new_id, env_id) >=
      (ssize_t)sizeof(str)) {
    perror("set_idmap: buffer too small");
    return -1;
  }
  len = strlen(str);
  if ((fd = open(path, O_WRONLY)) == -1) {
    perror("set_idmap: open failed");
    return -1;
  }
  ret = write(fd, str, len);
  close(fd);
  if (ret < 0) {
    perror("set_idmap: write failed");
    return -1;
  } else if (ret != len) {
    perror("set_idmap: partial write");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_idmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    $0x40,%esi
lea    0xe77(%rip),%rdx
call   1050 <snprintf@plt>
cmp    $0x40,%rax
jl     11b0 <set_idmap+0x50>
lea    0xe69(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <set_idmap+0x104>
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11f3 <set_idmap+0x93>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <set_idmap+0x104>
mov    -0x1c(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,-0x28(%rbp)
jge    1232 <set_idmap+0xd2>
lea    0xe1a(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <set_idmap+0x104>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1258 <set_idmap+0xf8>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <set_idmap+0x104>
jmp    125d <set_idmap+0xfd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_idmap>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r8d
mov    %esi,%ecx
mov    %rdi,%r14
lea    0xe87(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
call   1050 <snprintf@plt>
cmp    $0x3f,%rax
jg     11db <set_idmap+0x7b>
mov    %rsp,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e4 <set_idmap+0x84>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    %r15,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
test   %rbx,%rbx
js     11ed <set_idmap+0x8d>
xor    %eax,%eax
cmp    %r15,%rbx
je     11fe <set_idmap+0x9e>
lea    0xe7a(%rip),%rdi
jmp    11f4 <set_idmap+0x94>
lea    0xe26(%rip),%rdi
jmp    11f4 <set_idmap+0x94>
lea    0xe39(%rip),%rdi
jmp    11f4 <set_idmap+0x94>
lea    0xe47(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%u %u 1\00"
@.str.1 = [28 x i8] c"set_idmap: buffer too small\00"
@O_WRONLY = global i32 0
@.str.2 = [23 x i8] c"set_idmap: open failed\00"
@.str.3 = [24 x i8] c"set_idmap: write failed\00"
@.str.4 = [25 x i8] c"set_idmap: partial write\00"
define i32 @set_idmap(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca [64 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%13 = load i32, i32* %6
%14 = load i32, i32* %7
%15 = call i64 @snprintf(i8* %12, i32 64, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = icmp sge i64 %15, 64
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
19: ; preds = %3
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %10
%22 = load i8*, i8** %5
%23 = load i32, i32* @O_WRONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
28: ; preds = %19
%29 = load i32, i32* %8
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%31 = load i64, i64* %10
%32 = call i64 @write(i32 %29, i8* %30, i64 %31)
store i64 %32, i64* %9
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
%35 = load i64, i64* %9
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
39: ; preds = %28
%40 = load i64, i64* %9
%41 = load i64, i64* %10
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
45: ; preds = %39
br label %46
46: ; preds = %45
store i32 0, i32* %4
br label %47
47: ; preds = %46, %43, %37, %26, %17
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [8 x i8] c"%u %u 1\00"
@.str.1 = [28 x i8] c"set_idmap: buffer too small\00"
@O_WRONLY = global i32 0
@.str.2 = [23 x i8] c"set_idmap: open failed\00"
@.str.3 = [24 x i8] c"set_idmap: write failed\00"
@.str.4 = [25 x i8] c"set_idmap: partial write\00"
define i32 @set_idmap(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%6 = call i64 @snprintf(i8* %5, i32 64, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%7 = icmp sgt i64 %6, 63
br i1 %7, label %19, label %8
8: ; preds = %3
%9 = call i64 @strlen(i8* %5)
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %19, label %13
13: ; preds = %8
%14 = call i64 @write(i32 %11, i8* %5, i64 %9)
%15 = call i32 @close(i32 %11)
%16 = icmp slt i64 %14, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = icmp eq i64 %14, %9
br i1 %18, label %22, label %19
19: ; preds = %17, %13, %8, %3
%20 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %17 ]
%21 = call i32 @perror(i8* %20)
br label %22
22: ; preds = %19, %17
%23 = phi i32 [ 0, %17 ], [ -1, %19 ]
ret i32 %23
}