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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int write(int, char *, int);
void save_maps(void) {
  int ret;
  char buf[128];
  sprintf(buf, "/proc/%d/maps", getpid());
  int maps_fd = open(buf, O_RDONLY);
  int dest_fd = open("maps.snapshot", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  while ((ret = read(maps_fd, buf, 128)) > 0) {
    ret = write(dest_fd, buf, ret);
    if (ret < 0)
      break;
  }
  close(maps_fd);
  close(dest_fd);
}


x86 assembly (clang, -O0):
<save_maps>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xa0(%rbp),%rdi
mov    %eax,%edx
lea    0xe70(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x94(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x98(%rbp)
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jle    1239 <save_maps+0xd9>
mov    -0x98(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x4(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1234 <save_maps+0xd4>
jmp    1239 <save_maps+0xd9>
jmp    11ea <save_maps+0x8a>
mov    -0x94(%rbp),%edi
call   1050 <close@plt>
mov    -0x98(%rbp),%edi
call   1050 <close@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_maps>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe87(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebp
mov    0x2e16(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e14(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe48(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
nop
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11f3 <save_maps+0x93>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11d0 <save_maps+0x70>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"maps.snapshot\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @save_maps() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%6 = call i32 (...) @getpid()
%7 = call i32 @sprintf(i8* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%9 = load i32, i32* @O_RDONLY
%10 = call i32 (i8*, i32, ...) @open(i8* %8, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %16)
store i32 %17, i32* %4
br label %18
18: ; preds = %31, %0
%19 = load i32, i32* %3
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 128)
store i32 %21, i32* %1
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %18
%24 = load i32, i32* %4
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%26 = load i32, i32* %1
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %1
%28 = load i32, i32* %1
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
br label %32
31: ; preds = %23
br label %18
32: ; preds = %30, %18
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"maps.snapshot\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @save_maps() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 (i8*, i32, ...) @open(i8* %2, i32 %5)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %12)
br label %14
14: ; preds = %17, %0
%15 = call i32 @read(i32 %6, i8* %2, i32 128)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @write(i32 %13, i8* %2, i32 %15)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %14
20: ; preds = %17, %14
%21 = tail call i32 @close(i32 %6)
%22 = tail call i32 @close(i32 %13)
ret void
}