#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*/ key_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int init_shm_key(key_t *k, int num) {
  int i;
  char fn_r[] = "/dev/shm/qpitestxxxxx";
  char fname[256];
  for (i = 0; i < num; i++) {
    sprintf(fname, "%s%d", fn_r, i);
    FILE *fp = fopen(fname, "w");
    fclose(fp);
    k[i] = ftok(fname, 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<init_shm_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0xea7(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xea4(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xea2(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xe9c(%rip),%ax
mov    %ax,-0x1c(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f8 <init_shm_key+0xb8>
lea    -0x130(%rbp),%rdi
lea    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%ecx
lea    0xe72(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x130(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
call   1060 <ftok@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1183 <init_shm_key+0x43>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_shm_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
movaps 0xea8(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x78787878787473,%rax
mov    %rax,0xe(%rsp)
test   %esi,%esi
jle    11ce <init_shm_key+0x8e>
mov    %rdi,%r14
mov    %esi,%r15d
xor    %ebx,%ebx
lea    0x20(%rsp),%r12
mov    %rsp,%r13
lea    0xe95(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
lea    0xe7c(%rip),%rsi
mov    %r13,%rdx
mov    %ebx,%ecx
call   1050 <sprintf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1040 <fopen@plt>
mov    %rax,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1060 <ftok@plt>
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1190 <init_shm_key+0x50>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.init_shm_key.fn_r = [22 x i8] c"/dev/shm/qpitestxxxxx\00"
@.str = [5 x i8] c"%s%d\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @init_shm_key(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [22 x i8]
%7 = alloca [256 x i8]
%8 = alloca i32*
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = bitcast [22 x i8]* %6 to i8*
store i32 0, i32* %5
br label %10
10: ; preds = %29, %2
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %32
14: ; preds = %10
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%16 = getelementptr inbounds [22 x i8], [22 x i8]* %6, i64 0, i64 0
%17 = load i32, i32* %5
%18 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i32*, i32** %8
%22 = call i32 @fclose(i32* %21)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%24 = call i32 @ftok(i8* %23, i32 0)
%25 = load i32*, i32** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
store i32 %24, i32* %28
br label %29
29: ; preds = %14
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %10
32: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@__const.init_shm_key.fn_r = [22 x i8] c"/dev/shm/qpitestxxxxx\00"
@.str = [5 x i8] c"%s%d\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @init_shm_key(i32* nocapture writeonly %0, i32 %1) {
%3 = alloca [22 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [22 x i8], [22 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %20
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %18, %10 ]
%12 = trunc i64 %11 to i32
%13 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %12)
%14 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @fclose(i32* %14)
%16 = call i32 @ftok(i8* %6, i32 0)
%17 = getelementptr inbounds i32, i32* %0, i64 %11
store i32 %16, i32* %17
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %9
br i1 %19, label %20, label %10
20: ; preds = %10, %2
ret i32 0
}