#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int uid;
  int cuid;
  int mode;
};
struct shmid_ds {
  int shm_segsz;
  int shm_cpid;
  int shm_nattch;
  TYPE_1__ shm_perm;
};
typedef scalar_t__ key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ exit(int);
scalar_t__ ftok(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int shmget(scalar_t__, int, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("error args!\n");
    exit(0);
  }
  key_t skey = ftok(argv[1], 1);
  if (skey < 0) {
    perror("ftok");
    exit(-1);
  }
  printf("skey: %#x\n", skey);
  int shmid = shmget(skey, 4096, IPC_CREAT | 0600);
  if (shmid < 0) {
    perror("shmget");
    exit(-1);
  }
  printf("shmid: %d\n", shmid);
  struct shmid_ds buf;
  int ret = shmctl(shmid, IPC_STAT, &buf);
  if (ret < 0) {
    perror("shmctl");
    exit(-1);
  }
  printf("euid: %d, cuid: %d, mode: %#o\n", buf.shm_perm.uid, buf.shm_perm.cuid,
         buf.shm_perm.mode);
  printf("size: %d, cpid: %d, nattch: %lu\n", buf.shm_segsz, buf.shm_cpid,
         buf.shm_nattch);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1195 <main+0x35>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1,%esi
call   1080 <ftok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11cc <main+0x6c>
lea    0xe50(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
mov    $0x1000,%esi
call   1060 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    121e <main+0xbe>
lea    0xe0e(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1267 <main+0x107>
lea    0xdd7(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%ecx
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jne    1233 <main+0xc3>
mov    0x8(%rsi),%rdi
mov    $0x1,%esi
call   1090 <ftok@plt>
test   %rax,%rax
js     1246 <main+0xd6>
mov    %rax,%rbx
lea    0xe66(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e28(%rip),%rax
mov    $0x180,%edx
or     (%rax),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <shmget@plt>
test   %eax,%eax
js     124f <main+0xdf>
mov    %eax,%ebx
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <shmctl@plt>
test   %eax,%eax
js     1258 <main+0xe8>
mov    0x14(%rsp),%esi
mov    0x18(%rsp),%edx
mov    0x1c(%rsp),%ecx
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%ecx
lea    0xe24(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xdb3(%rip),%rdi
jmp    125f <main+0xef>
lea    0xdba(%rip),%rdi
jmp    125f <main+0xef>
lea    0xdc3(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { i32, i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@.str = [13 x i8] c"error args!\0A\00"
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [11 x i8] c"skey: %#x\0A\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [11 x i8] c"shmid: %d\0A\00"
@IPC_STAT = global i32 0
@.str.5 = [7 x i8] c"shmctl\00"
@.str.6 = [31 x i8] c"euid: %d, cuid: %d, mode: %#o\0A\00"
@.str.7 = [33 x i8] c"size: %d, cpid: %d, nattch: %lu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.shmid_ds
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i64 @ftok(i8* %18, i32 1)
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %15
%26 = load i64, i64* %6
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %26)
%28 = load i64, i64* %6
%29 = load i32, i32* @IPC_CREAT
%30 = or i32 %29, 384
%31 = call i32 @shmget(i64 %28, i32 4096, i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %25
%38 = load i32, i32* %7
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %7
%41 = load i32, i32* @IPC_STAT
%42 = call i32 @shmctl(i32 %40, i32 %41, %struct.shmid_ds* %8)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %37
%49 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %8, i32 0, i32 3
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %8, i32 0, i32 3
%53 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %52, i32 0, i32 1
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %8, i32 0, i32 3
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 2
%57 = load i32, i32* %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %51, i32 %54, i32 %57)
%59 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %8, i32 0, i32 0
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %8, i32 0, i32 1
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %8, i32 0, i32 2
%64 = load i32, i32* %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i32 %60, i32 %62, i32 %64)
ret i32 0
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32, i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [11 x i8] c"skey: %#x\0A\00"
@IPC_CREAT = global i32 0
@.str.3 = [7 x i8] c"shmget\00"
@.str.4 = [11 x i8] c"shmid: %d\0A\00"
@IPC_STAT = global i32 0
@.str.5 = [7 x i8] c"shmctl\00"
@.str.6 = [31 x i8] c"euid: %d, cuid: %d, mode: %#o\0A\00"
@.str.7 = [33 x i8] c"size: %d, cpid: %d, nattch: %lu\0A\00"
@str = [12 x i8] c"error args!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.shmid_ds
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @ftok(i8* %10, i32 1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %8
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %11)
%18 = load i32, i32* @IPC_CREAT
%19 = or i32 %18, 384
%20 = tail call i32 @shmget(i64 %11, i32 4096, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %16
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%27 = bitcast %struct.shmid_ds* %3 to i8*
%28 = load i32, i32* @IPC_STAT
%29 = call i32 @shmctl(i32 %20, i32 %28, %struct.shmid_ds* %3)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %25
%35 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 3, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 3, i32 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 3, i32 2
%40 = load i32, i32* %39
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %36, i32 %38, i32 %40)
%42 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 1
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 2
%47 = load i32, i32* %46
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i32 %43, i32 %45, i32 %47)
ret i32 0
}