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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *getmID();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int strlen(char *);
char *getRandContents(void) {
  char *mid = getmID();
  if (!mid)
    return NULL;
  char file[80] = "";
  struct passwd *pw = getpwuid(getuid());
  const char *homedir = pw->pw_dir;
  sprintf(file, "%s/.%s-chk", homedir, mid);
  free(mid);
  FILE *pFile = fopen(file, "r");
  if (!pFile)
    return NULL;
  char *output = malloc(121);
  fgets(output, 121, pFile);
  fclose(pFile);
  if (strlen(output) != 120)
    return NULL;
  return output;
}


x86 assembly (clang, -O0):
<getRandContents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    $0x0,%al
call   1040 <getmID@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11de <getRandContents+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    12a8 <getRandContents+0xf8>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1090 <memset@plt>
mov    $0x0,%al
call   1080 <getuid@plt>
mov    %eax,%edi
call   1060 <getpwuid@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x70(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x70(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xde2(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    -0x60(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    1258 <getRandContents+0xa8>
movq   $0x0,-0x8(%rbp)
jmp    12a8 <getRandContents+0xf8>
mov    $0x79,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%rdx
mov    $0x79,%esi
call   10a0 <fgets@plt>
mov    -0x78(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x80(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x78,%eax
je     12a0 <getRandContents+0xf0>
movq   $0x0,-0x8(%rbp)
jmp    12a8 <getRandContents+0xf8>
mov    -0x80(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRandContents>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getmID@plt>
test   %rax,%rax
je     1259 <getRandContents+0xb9>
mov    %rax,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <getuid@plt>
mov    %eax,%edi
call   1060 <getpwuid@plt>
mov    (%rax),%rdx
lea    0xe0b(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbx,%rcx
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdf9(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1259 <getRandContents+0xb9>
mov    %rax,%rbx
mov    $0x79,%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x79,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
xor    %r14d,%r14d
cmp    $0x78,%eax
cmove  %r15,%r14
mov    %r14,%rax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [11 x i8] c"%s/.%s-chk\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @getRandContents() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca [80 x i8]
%4 = alloca %struct.passwd*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = call i8* (...) @getmID()
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %0
store i8* null, i8** %1
br label %43
12: ; preds = %0
%13 = bitcast [80 x i8]* %3 to i8*
%14 = call i32 (...) @getuid()
%15 = call %struct.passwd* @getpwuid(i32 %14)
store %struct.passwd* %15, %struct.passwd** %4
%16 = load %struct.passwd*, %struct.passwd** %4
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i8*, i8** %17
store i8* %18, i8** %5
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%20 = load i8*, i8** %5
%21 = load i8*, i8** %2
%22 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %21)
%23 = load i8*, i8** %2
%24 = call i32 @free(i8* %23)
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %6
%27 = load i32*, i32** %6
%28 = icmp ne i32* %27, null
br i1 %28, label %30, label %29
29: ; preds = %12
store i8* null, i8** %1
br label %43
30: ; preds = %12
%31 = call i8* @malloc(i32 121)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = load i32*, i32** %6
%34 = call i32 @fgets(i8* %32, i32 121, i32* %33)
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %7
%38 = call i32 @strlen(i8* %37)
%39 = icmp ne i32 %38, 120
br i1 %39, label %40, label %41
40: ; preds = %30
store i8* null, i8** %1
br label %43
41: ; preds = %30
%42 = load i8*, i8** %7
store i8* %42, i8** %1
br label %43
43: ; preds = %41, %40, %29, %11
%44 = load i8*, i8** %1
ret i8* %44
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [11 x i8] c"%s/.%s-chk\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @getRandContents() {
%1 = alloca [80 x i8]
%2 = tail call i8* (...) @getmID()
%3 = icmp eq i8* %2, null
br i1 %3, label %23, label %4
4: ; preds = %0
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%6 = tail call i32 (...) @getuid()
%7 = tail call %struct.passwd* @getpwuid(i32 %6)
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %2)
%11 = tail call i32 @free(i8* %2)
%12 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %21, label %14
14: ; preds = %4
%15 = tail call dereferenceable_or_null(121) i8* @malloc(i32 121)
%16 = tail call i32 @fgets(i8* %15, i32 121, i32* %12)
%17 = tail call i32 @fclose(i32* %12)
%18 = tail call i32 @strlen(i8* %15)
%19 = icmp eq i32 %18, 120
%20 = select i1 %19, i8* %15, i8* null
br label %21
21: ; preds = %4, %14
%22 = phi i8* [ %20, %14 ], [ null, %4 ]
br label %23
23: ; preds = %0, %21
%24 = phi i8* [ %22, %21 ], [ null, %0 ]
ret i8* %24
}