#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;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISLNK(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int lstat(char *, struct stat *);
int /*<<< orphan*/ printf(char *, int, char *, ...);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int check_symlink_file(char *base, int nb, int line) {
  struct stat stats;
  int ret;
  char file[128];
  char target[128];
  char name[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_ISLNK(stats.st_mode)) {
    printf("%d lstat(%s) not a symbolic link\n", line, file);
    return -1;
  }
  ret = readlink(file, target, 128);
  if (ret < 0) {
    printf("%d readlink(%s) %s\n", line, file, strerror(errno));
    return -1;
  }
  target[ret] = 0;
  sprintf(name, "%s%d", base, nb - 16);
  if (strcmp(target, name) != 0) {
    printf("%d %s is a link to %s while expecting %s\n", line, file, target,
           name);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_symlink_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe66(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0xb0(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <lstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    120b <check_symlink_file+0x9b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1bc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x1b8(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1bc(%rbp),%esi
mov    -0x1b8(%rbp),%rdx
mov    %rax,%rcx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <check_symlink_file+0x1c3>
mov    -0x20(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    1240 <check_symlink_file+0xd0>
mov    -0x18(%rbp),%esi
lea    -0xb0(%rbp),%rdx
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <check_symlink_file+0x1c3>
lea    -0xb0(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    $0x80,%edx
call   1040 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12b4 <check_symlink_file+0x144>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1cc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x1c8(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1cc(%rbp),%esi
mov    -0x1c8(%rbp),%rdx
mov    %rax,%rcx
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <check_symlink_file+0x1c3>
movslq -0x24(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x1b0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
sub    $0x10,%ecx
lea    0xd28(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x130(%rbp),%rdi
lea    -0x1b0(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     132c <check_symlink_file+0x1bc>
mov    -0x18(%rbp),%esi
lea    -0xb0(%rbp),%rdx
lea    -0x130(%rbp),%rcx
lea    -0x1b0(%rbp),%r8
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <check_symlink_file+0x1c3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_symlink_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x198,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xe74(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %ebp,%ecx
call   1080 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %eax,%eax
js     124d <check_symlink_file+0xdd>
mov    0x8(%rsp),%edi
call   1030 <S_ISLNK@plt>
test   %eax,%eax
je     1264 <check_symlink_file+0xf4>
lea    0x10(%rsp),%rdi
lea    0x90(%rsp),%rsi
mov    $0x80,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     127c <check_symlink_file+0x10c>
mov    %eax,%eax
movb   $0x0,0x90(%rsp,%rax,1)
add    $0xfffffff0,%ebp
lea    0xe09(%rip),%rsi
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %ebp,%ecx
call   1080 <sprintf@plt>
lea    0x90(%rsp),%rdi
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12b6 <check_symlink_file+0x146>
lea    0xe20(%rip),%rdi
lea    0x10(%rsp),%rdx
lea    0x90(%rsp),%rcx
lea    0x110(%rsp),%r8
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a3 <check_symlink_file+0x133>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda3(%rip),%rdi
jmp    1291 <check_symlink_file+0x121>
lea    0xdab(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a3 <check_symlink_file+0x133>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda7(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %r14d,%esi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12a8 <check_symlink_file+0x138>

LLVM IR (-O0):
%struct.stat = type { 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 = [34 x i8] c"%d lstat(%s) not a symbolic link\0A\00"
@.str.3 = [20 x i8] c"%d readlink(%s) %s\0A\00"
@.str.4 = [42 x i8] c"%d %s is a link to %s while expecting %s\0A\00"
define i32 @check_symlink_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca [128 x i8]
%11 = alloca [128 x i8]
%12 = alloca [128 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%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]* %10, i64 0, i64 0
%18 = call i32 @lstat(i8* %17, %struct.stat* %8)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %3
%22 = load i32, i32* %7
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %10, 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* %4
br label %68
27: ; preds = %3
%28 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @S_ISLNK(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = load i32, i32* %7
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%35 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %33, i8* %34)
store i32 -1, i32* %4
br label %68
36: ; preds = %27
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%39 = call i32 @readlink(i8* %37, i8* %38, i32 128)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %36
%43 = load i32, i32* %7
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %43, i8* %44, i8* %46)
store i32 -1, i32* %4
br label %68
48: ; preds = %36
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 %50
store i8 0, i8* %51
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%53 = load i8*, i8** %5
%54 = load i32, i32* %6
%55 = sub nsw i32 %54, 16
%56 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %53, i32 %55)
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%59 = call i64 @strcmp(i8* %57, i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %48
%62 = load i32, i32* %7
%63 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%64 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%65 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%66 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %62, i8* %63, i8* %64, i8* %65)
store i32 -1, i32* %4
br label %68
67: ; preds = %48
store i32 0, i32* %4
br label %68
68: ; preds = %67, %61, %42, %32, %21
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
%struct.stat = type { 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 = [34 x i8] c"%d lstat(%s) not a symbolic link\0A\00"
@.str.3 = [20 x i8] c"%d readlink(%s) %s\0A\00"
@.str.4 = [42 x i8] c"%d %s is a link to %s while expecting %s\0A\00"
define i32 @check_symlink_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca [128 x i8]
%6 = alloca [128 x i8]
%7 = alloca [128 x i8]
%8 = bitcast %struct.stat* %4 to i8*
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%12 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%13 = call i32 @lstat(i8* %9, %struct.stat* %4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %2, i8* %9, i8* %17)
br label %42
19: ; preds = %3
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @S_ISLNK(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %2, i8* %9)
br label %42
26: ; preds = %19
%27 = call i32 @readlink(i8* %9, i8* %10, i32 128)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %2, i8* %9, i8* %31)
br label %42
33: ; preds = %26
%34 = zext i32 %27 to i64
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %34
store i8 0, i8* %35
%36 = add nsw i32 %1, -16
%37 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %36)
%38 = call i64 @strcmp(i8* %10, i8* %11)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %33
%41 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %2, i8* %9, i8* %10, i8* %11)
br label %42
42: ; preds = %33, %40, %29, %24, %15
%43 = phi i32 [ -1, %15 ], [ -1, %29 ], [ -1, %40 ], [ -1, %24 ], [ 0, %33 ]
ret i32 %43
}