#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 */
struct passwd {
  char *pw_dir;
};
typedef int key_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ abort();
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int ftok(char const *, int);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char const *);
key_t vam_ipc_get_key(const char *ipcf, int proj_id) {
  char *malloced = NULL;
  key_t result = -1;
  if (!ipcf) {
    struct passwd *pwent = getpwuid(getuid());
    if (!pwent) {
      fprintf(stderr, "Could not get pw entry\n");
      goto bail;
    }
    const char *homedir = pwent->pw_dir;
    malloced = malloc(strlen(homedir) + strlen(".vamipc_key_file") + 2);
    if (!malloced)
      goto bail;
    sprintf(malloced, "%s/%s", homedir, ".vamipc_key_file");
    ipcf = malloced;
  }
  FILE *f = NULL;
  if ((f = fopen(ipcf, "r")) || (f = fopen(ipcf, "w+"))) {
    {
      if (!(fclose(f) == 0)) {
        fprintf(stderr, "FAILED ASSERTION\n");
        fprintf(stderr, "Could not close file");
        fprintf(stderr, "\n");
        abort();
      }
    };
  } else {
    fprintf(stderr, "Could not open %s\n", ipcf);
    goto bail;
  }
  result = ftok(ipcf, proj_id);
bail:
  if (malloced)
    free(malloced);
  return result;
}


x86 assembly (clang, -O0):
<vam_ipc_get_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    127d <vam_ipc_get_key+0xcd>
mov    $0x0,%al
call   1080 <getuid@plt>
mov    %eax,%edi
call   1060 <getpwuid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1212 <vam_ipc_get_key+0x62>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1354 <vam_ipc_get_key+0x1a4>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x40(%rbp)
lea    0xde7(%rip),%rdi
call   1070 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125a <vam_ipc_get_key+0xaa>
jmp    1354 <vam_ipc_get_key+0x1a4>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xdc0(%rip),%rsi
lea    0xda8(%rip),%rcx
call   10c0 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xd9f(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    12c1 <vam_ipc_get_key+0x111>
mov    -0x8(%rbp),%rdi
lea    0xd83(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1325 <vam_ipc_get_key+0x175>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     1320 <vam_ipc_get_key+0x170>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
lea    0xd4b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    1345 <vam_ipc_get_key+0x195>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd22(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1354 <vam_ipc_get_key+0x1a4>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10d0 <ftok@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1368 <vam_ipc_get_key+0x1b8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vam_ipc_get_key>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
test   %rdi,%rdi
je     121a <vam_ipc_get_key+0x6a>
mov    %rdi,%rbx
xor    %ebp,%ebp
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
jne    11ed <vam_ipc_get_key+0x3d>
lea    0xe55(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     128d <vam_ipc_get_key+0xdd>
mov    %rax,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
jne    12b7 <vam_ipc_get_key+0x107>
mov    %rbx,%rdi
mov    %r14d,%esi
call   10d0 <ftok@plt>
mov    %eax,%ebx
test   %rbp,%rbp
je     1286 <vam_ipc_get_key+0xd6>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1286 <vam_ipc_get_key+0xd6>
xor    %eax,%eax
call   1080 <getuid@plt>
mov    %eax,%edi
call   1060 <getpwuid@plt>
test   %rax,%rax
je     126a <vam_ipc_get_key+0xba>
mov    (%rax),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    0x12(%rax),%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1281 <vam_ipc_get_key+0xd1>
mov    %rax,%rbp
lea    0xdd9(%rip),%rsi
lea    0xdc1(%rip),%rcx
mov    %rax,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %rbp,%rbx
jmp    11c1 <vam_ipc_get_key+0x11>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%ebx
test   %rbp,%rbp
jne    1210 <vam_ipc_get_key+0x60>
jmp    1286 <vam_ipc_get_key+0xd6>
mov    0x2d22(%rip),%rbx
mov    (%rbx),%edi
lea    0xd6d(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd6f(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd5d(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@stderr = global i32 0
@.str = [24 x i8] c"Could not get pw entry\0A\00"
@.str.1 = [17 x i8] c".vamipc_key_file\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [18 x i8] c"FAILED ASSERTION\0A\00"
@.str.6 = [21 x i8] c"Could not close file\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [19 x i8] c"Could not open %s\0A\00"
define i32 @vam_ipc_get_key(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.passwd*
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* null, i8** %5
store i32 -1, i32* %6
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %38, label %12
12: ; preds = %2
%13 = call i32 (...) @getuid()
%14 = call %struct.passwd* @getpwuid(i32 %13)
store %struct.passwd* %14, %struct.passwd** %7
%15 = load %struct.passwd*, %struct.passwd** %7
%16 = icmp ne %struct.passwd* %15, null
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %67
20: ; preds = %12
%21 = load %struct.passwd*, %struct.passwd** %7
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 0
%23 = load i8*, i8** %22
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = call i64 @strlen(i8* %24)
%26 = call i64 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%27 = add nsw i64 %25, %26
%28 = add nsw i64 %27, 2
%29 = call i8* @malloc(i64 %28)
store i8* %29, i8** %5
%30 = load i8*, i8** %5
%31 = icmp ne i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %20
br label %67
33: ; preds = %20
%34 = load i8*, i8** %5
%35 = load i8*, i8** %8
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%37 = load i8*, i8** %5
store i8* %37, i8** %3
br label %38
38: ; preds = %33, %2
store i32* null, i32** %9
%39 = load i8*, i8** %3
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %40, i32** %9
%41 = icmp ne i32* %40, null
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i8*, i8** %3
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %44, i32** %9
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %59
46: ; preds = %42, %38
%47 = load i32*, i32** %9
%48 = call i64 @fclose(i32* %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %58, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%57 = call i32 (...) @abort()
unreachable
58: ; preds = %46
br label %63
59: ; preds = %42
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %3
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %61)
br label %67
63: ; preds = %58
%64 = load i8*, i8** %3
%65 = load i32, i32* %4
%66 = call i32 @ftok(i8* %64, i32 %65)
store i32 %66, i32* %6
br label %67
67: ; preds = %63, %59, %32, %17
%68 = load i8*, i8** %5
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i8*, i8** %5
%72 = call i32 @free(i8* %71)
br label %73
73: ; preds = %70, %67
%74 = load i32, i32* %6
ret i32 %74
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@stderr = global i32 0
@.str = [24 x i8] c"Could not get pw entry\0A\00"
@.str.1 = [17 x i8] c".vamipc_key_file\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [18 x i8] c"FAILED ASSERTION\0A\00"
@.str.6 = [21 x i8] c"Could not close file\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [19 x i8] c"Could not open %s\0A\00"
define i32 @vam_ipc_get_key(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %20
4: ; preds = %2
%5 = tail call i32 (...) @getuid()
%6 = tail call %struct.passwd* @getpwuid(i32 %5)
%7 = icmp eq %struct.passwd* %6, null
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %50
11: ; preds = %4
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i64 @strlen(i8* dereferenceable(1) %13)
%15 = add nsw i64 %14, 18
%16 = tail call i8* @malloc(i64 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %50, label %18
18: ; preds = %11
%19 = tail call i32 @sprintf(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = phi i8* [ null, %2 ], [ %16, %18 ]
%22 = phi i8* [ %0, %2 ], [ %16, %18 ]
%23 = tail call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = tail call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %40, label %28
28: ; preds = %25, %20
%29 = phi i32* [ %23, %20 ], [ %26, %25 ]
%30 = tail call i64 @fclose(i32* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %43, label %32
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%39 = tail call i32 (...) @abort()
unreachable
40: ; preds = %25
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %22)
br label %45
43: ; preds = %28
%44 = tail call i32 @ftok(i8* %22, i32 %1)
br label %45
45: ; preds = %43, %40
%46 = phi i32 [ %44, %43 ], [ -1, %40 ]
%47 = icmp eq i8* %21, null
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = tail call i32 @free(i8* %21)
br label %50
50: ; preds = %8, %11, %45, %48
%51 = phi i32 [ %46, %45 ], [ %46, %48 ], [ -1, %11 ], [ -1, %8 ]
ret i32 %51
}