#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 EXIT_SUCCESS;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int close(int);
int /*<<< orphan*/ exit(int);
int ftruncate(int, size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int shm_open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
size_t strlen(char *);
int main(int argc, char *argv[]) {
  int fd;
  size_t len;
  char *addr;
  fd = shm_open("/myshm", O_RDWR, 0);
  if (fd == -1) {
    perror("shm_open");
    exit(1);
  }
  len = strlen(argv[1]);
  if (ftruncate(fd, len) == -1) {
    perror("ftruncate");
    exit(1);
  }
  printf("Resized to %ld bytes\n", (long)len);
  addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED)
    perror("mmap");
  if (close(fd) == -1)
    perror("close");
  printf("copying %ld bytes\n", (long)len);
  memcpy(addr, argv[1], len);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
xor    %edx,%edx
call   10b0 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e0 <main+0x50>
lea    0xe36(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    121c <main+0x8c>
lea    0xe03(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
or     (%rax),%edx
mov    0x2d9f(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d4e(%rip),%rcx
cmp    (%rcx),%rax
jne    127f <main+0xef>
lea    0xdb6(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    129c <main+0x10c>
lea    0xd9e(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%rsi
lea    0xd94(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <memcpy@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
xor    %edx,%edx
call   10b0 <shm_open@plt>
cmp    $0xffffffff,%eax
jne    11bf <main+0x2f>
lea    0xe4a(%rip),%rdi
jmp    11e3 <main+0x53>
mov    %eax,%ebp
mov    0x8(%r14),%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
call   1060 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11f2 <main+0x62>
lea    0xe2d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe21(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x2df0(%rip),%rax
mov    0x2db9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dce(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r15
mov    0x2d82(%rip),%rax
cmp    (%rax),%r15
jne    1247 <main+0xb7>
lea    0xdee(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    125f <main+0xcf>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
lea    0xdd5(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1080 <memcpy@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"/myshm\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"shm_open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@.str.3 = [22 x i8] c"Resized to %ld bytes\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [6 x i8] c"close\00"
@.str.6 = [19 x i8] c"copying %ld bytes\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %7
%21 = load i32, i32* %6
%22 = load i64, i64* %7
%23 = call i32 @ftruncate(i32 %21, i64 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = load i64, i64* %7
%30 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %29)
%31 = load i64, i64* %7
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %32, %33
%35 = load i32, i32* @MAP_SHARED
%36 = load i32, i32* %6
%37 = call i8* @mmap(i32* null, i64 %31, i32 %34, i32 %35, i32 %36, i32 0)
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = load i8*, i8** @MAP_FAILED
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %43
41: ; preds = %28
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %28
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = load i64, i64* %7
%51 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %50)
%52 = load i8*, i8** %8
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = load i64, i64* %7
%57 = call i32 @memcpy(i8* %52, i8* %55, i64 %56)
%58 = load i32, i32* @EXIT_SUCCESS
%59 = call i32 @exit(i32 %58)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"/myshm\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"shm_open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@.str.3 = [22 x i8] c"Resized to %ld bytes\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [6 x i8] c"close\00"
@.str.6 = [19 x i8] c"copying %ld bytes\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @shm_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%13 = tail call i32 @ftruncate(i32 %4, i64 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %12)
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i8* @mmap(i32* null, i64 %12, i32 %22, i32 %23, i32 %4, i32 0)
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %18
%30 = tail call i32 @close(i32 %4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i64 %12)
%36 = load i8*, i8** %10
%37 = tail call i32 @memcpy(i8* %24, i8* %36, i64 %12)
%38 = load i32, i32* @EXIT_SUCCESS
%39 = tail call i32 @exit(i32 %38)
unreachable
}