#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ paths;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int stat(char const *, struct stat *);
const char *get_lockdir(void) {
  static const char *paths[] = {"/var/lock", "/tmp"};
  for (size_t i = 0; i < (sizeof(paths) / sizeof(paths[0])); i++) {
    struct stat s;
    const char *path = paths[i];
    int result = stat(path, &s);
    if (result != 0) {
      perror("stat");
      return NULL;
    }
    if (S_ISDIR(s.st_mode)) {
      return path;
    }
  }
  return ".";
}


x86 assembly (clang, -O0):
<get_lockdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x10(%rbp)
cmpq   $0x2,-0x10(%rbp)
jae    11c6 <get_lockdir+0x96>
mov    -0x10(%rbp),%rcx
lea    0x2eea(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1191 <get_lockdir+0x61>
lea    0xe90(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d1 <get_lockdir+0xa1>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11b0 <get_lockdir+0x80>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d1 <get_lockdir+0xa1>
jmp    11b5 <get_lockdir+0x85>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1140 <get_lockdir+0x10>
lea    0xe47(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_lockdir>:
push   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
jne    118c <get_lockdir+0x5c>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
jne    119a <get_lockdir+0x6a>
lea    0xea8(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
jne    118c <get_lockdir+0x5c>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
lea    0xe8e(%rip),%rax
cmove  %rax,%rbx
jmp    119a <get_lockdir+0x6a>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@get_lockdir.paths = internal global [2 x i8*] [i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0)]
@.str = [10 x i8] c"/var/lock\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [5 x i8] c"stat\00"
@.str.3 = [2 x i8] c".\00"
define i8* @get_lockdir() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i32
store i64 0, i64* %2
br label %6
6: ; preds = %27, %0
%7 = load i64, i64* %2
%8 = icmp ult i64 %7, 2
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = load i64, i64* %2
%11 = getelementptr inbounds [2 x i8*], [2 x i8*]* @get_lockdir.paths, i64 0, i64 %10
%12 = load i8*, i8** %11
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = call i32 @stat(i8* %13, %struct.stat* %3)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %31
19: ; preds = %9
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @S_ISDIR(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i8*, i8** %4
store i8* %25, i8** %1
br label %31
26: ; preds = %19
br label %27
27: ; preds = %26
%28 = load i64, i64* %2
%29 = add i64 %28, 1
store i64 %29, i64* %2
br label %6
30: ; preds = %6
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %31
31: ; preds = %30, %24, %17
%32 = load i8*, i8** %1
ret i8* %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"/var/lock\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [5 x i8] c"stat\00"
@.str.3 = [2 x i8] c".\00"
define i8* @get_lockdir() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%4 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %16, label %14
6: ; preds = %16
%7 = call i32 @stat(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = load i32, i32* %3
%11 = tail call i64 @S_ISDIR(i32 %10)
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0)
br label %20
14: ; preds = %6, %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %20
16: ; preds = %0
%17 = load i32, i32* %3
%18 = tail call i64 @S_ISDIR(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %6, label %20
20: ; preds = %9, %16, %14
%21 = phi i8* [ null, %14 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %16 ], [ %13, %9 ]
ret i8* %21
}