#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 /*<<< orphan*/ IPC_PRIVATE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, int);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  int shmid;
  char *p_addr, *c_addr;
  if (argc != 2) {
    fprintf(stderr, "Usage：%s need start arg! \n\a", argv[0]);
    exit(1);
  }
  if ((shmid = shmget(IPC_PRIVATE, 1024, S_IRUSR | S_IWUSR)) == -1) {
    fprintf(stderr, "Create Share Memory Error：%s\n\a", strerror(errno));
    exit(1);
  }
  p_addr = shmat(shmid, 0, 0);
  memset(p_addr, '\0', 1024);
  strncpy(p_addr, argv[1], 1024);
  printf("shmid:%d\n", shmid);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11bf <main+0x3f>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    $0x400,%esi
call   1080 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1226 <main+0xa6>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe07(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%esi
lea    0xdd9(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
cmp    $0x2,%edi
jne    11fb <main+0x7b>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e24(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    $0x400,%esi
call   1080 <shmget@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0x90>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x8(%r14),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    0xe52(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdf2(%rip),%rsi
jmp    1233 <main+0xb3>
mov    0x2db9(%rip),%rax
mov    (%rax),%ebx
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdf0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Usage\EF\BC\9A%s need start arg! \0A\07\00"
@IPC_PRIVATE = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [33 x i8] c"Create Share Memory Error\EF\BC\9A%s\0A\07\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"shmid:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @IPC_PRIVATE
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = call i32 @shmget(i32 %18, i32 1024, i32 %21)
store i32 %22, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %17
%31 = load i32, i32* %5
%32 = call i8* @shmat(i32 %31, i32 0, i32 0)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = call i32 @memset(i8* %33, i8 signext 0, i32 1024)
%35 = load i8*, i8** %6
%36 = load i8**, i8*** %4
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @strncpy(i8* %35, i8* %38, i32 1024)
%40 = load i32, i32* %5
%41 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %40)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Usage\EF\BC\9A%s need start arg! \0A\07\00"
@IPC_PRIVATE = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [33 x i8] c"Create Share Memory Error\EF\BC\9A%s\0A\07\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"shmid:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @IPC_PRIVATE
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = tail call i32 @shmget(i32 %10, i32 1024, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %9
%23 = tail call i8* @shmat(i32 %14, i32 0, i32 0)
%24 = tail call i32 @memset(i8* %23, i8 signext 0, i32 1024)
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32 @strncpy(i8* %23, i8* %26, i32 1024)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %14)
ret i32 0
}