#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 st_nlink;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int lstat(char *, struct stat *);
int /*<<< orphan*/ printf(char *, int, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
char *strerror(int /*<<< orphan*/);
int check_regular_file(char *base, int nb, int nlinks, int line) {
  struct stat stats;
  int ret;
  char file[128];
  sprintf(file, "%s%d", base, nb);
  ret = lstat(file, &stats);
  if (ret < 0) {
    printf("%d lstat(%s) %s\n", line, file, strerror(errno));
    return -1;
  }
  if (!S_ISREG(stats.st_mode)) {
    printf("%d lstat(%s) not a regular file\n", line, file);
    return -1;
  }
  if (nlinks != stats.st_nlink) {
    printf("%d file %s has %d links while expecting %d\n", line, file,
           (int)stats.st_nlink, nlinks);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_regular_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe83(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0xb0(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ee <check_regular_file+0x9e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xbc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0xbc(%rbp),%esi
mov    -0xb8(%rbp),%rdx
mov    %rax,%rcx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <check_regular_file+0x111>
mov    -0x24(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1223 <check_regular_file+0xd3>
mov    -0x1c(%rbp),%esi
lea    -0xb0(%rbp),%rdx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <check_regular_file+0x111>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     125a <check_regular_file+0x10a>
mov    -0x1c(%rbp),%esi
lea    -0xb0(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <check_regular_file+0x111>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_regular_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%ecx
mov    %rdi,%rdx
lea    0xe94(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <lstat@plt>
test   %eax,%eax
js     11bc <check_regular_file+0x6c>
mov    0xc(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %eax,%eax
je     11e5 <check_regular_file+0x95>
mov    0x8(%rsp),%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
je     1200 <check_regular_file+0xb0>
lea    0xe8f(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11fb <check_regular_file+0xab>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe34(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %r14d,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11fb <check_regular_file+0xab>
lea    0xe2a(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [5 x i8] c"%s%d\00"
@.str.1 = [17 x i8] c"%d lstat(%s) %s\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"%d lstat(%s) not a regular file\0A\00"
@.str.3 = [44 x i8] c"%d file %s has %d links while expecting %d\0A\00"
define i32 @check_regular_file(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca [128 x i8]
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%18 = call i32 @lstat(i8* %17, %struct.stat* %10)
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %4
%22 = load i32, i32* %9
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %22, i8* %23, i8* %25)
store i32 -1, i32* %5
br label %49
27: ; preds = %4
%28 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%29 = load i32, i32* %28
%30 = call i32 @S_ISREG(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = load i32, i32* %9
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%35 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %33, i8* %34)
store i32 -1, i32* %5
br label %49
36: ; preds = %27
%37 = load i32, i32* %8
%38 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%39 = load i32, i32* %38
%40 = icmp ne i32 %37, %39
br i1 %40, label %41, label %48
41: ; preds = %36
%42 = load i32, i32* %9
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%44 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%45 = load i32, i32* %44
%46 = load i32, i32* %8
%47 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %42, i8* %43, i32 %45, i32 %46)
store i32 -1, i32* %5
br label %49
48: ; preds = %36
store i32 0, i32* %5
br label %49
49: ; preds = %48, %41, %32, %21
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [5 x i8] c"%s%d\00"
@.str.1 = [17 x i8] c"%d lstat(%s) %s\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"%d lstat(%s) not a regular file\0A\00"
@.str.3 = [44 x i8] c"%d file %s has %d links while expecting %d\0A\00"
define i32 @check_regular_file(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.stat
%6 = alloca [128 x i8]
%7 = bitcast %struct.stat* %5 to i8*
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%10 = call i32 @lstat(i8* %8, %struct.stat* %5)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %4
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %3, i8* %8, i8* %14)
br label %29
16: ; preds = %4
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i32 @S_ISREG(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %3, i8* %8)
br label %29
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, %2
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %3, i8* %8, i32 %25, i32 %2)
br label %29
29: ; preds = %23, %27, %21, %12
%30 = phi i32 [ -1, %12 ], [ -1, %27 ], [ -1, %21 ], [ 0, %23 ]
ret i32 %30
}