#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;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ filename2;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  DIR *dir;
  struct dirent *direntry;
  struct stat file_info;
  char filename[256], filename2[256];
  char *dirname = "/";
  if (argc > 1)
    dirname = argv[1];
  if (chdir(dirname)) {
    perror(dirname);
    exit(errno);
  };
  if (!(dir = opendir("./"))) {
    perror(dirname);
    exit(errno);
  };
  while ((direntry = readdir(dir))) {
    strcpy(filename, direntry->d_name);
    if (lstat(filename, &file_info))
      fprintf(stderr, "Error lstating %s\n", filename);
    else if (S_ISLNK(file_info.st_mode)) {
      memset(filename2, 0, sizeof(filename2));
      if (-1 == readlink(filename, filename2, sizeof(filename2))) {
        printf("failed using readlink\n");
      } else {
        if (lstat(filename2, &file_info))
          printf("%s is a broken link to %s\n", filename, filename2);
        else
          printf("%s is a link to %s\n", filename, filename2);
      }
    }
  }
  closedir(dir);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe11(%rip),%rax
mov    %rax,-0x238(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1210 <main+0x40>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rdi
call   1070 <chdir@plt>
cmp    $0x0,%rax
je     1240 <main+0x70>
mov    -0x238(%rbp),%rdi
call   10e0 <perror@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xdb9(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1274 <main+0xa4>
mov    -0x238(%rbp),%rdi
call   10e0 <perror@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
jmp    1279 <main+0xa9>
mov    -0x18(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13a7 <main+0x1d7>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
lea    -0x130(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10b0 <lstat@plt>
cmp    $0x0,%rax
je     12dd <main+0x10d>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    -0x130(%rbp),%rdx
lea    0xd30(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    13a2 <main+0x1d2>
mov    -0x28(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     139d <main+0x1cd>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1090 <memset@plt>
lea    -0x130(%rbp),%rdi
lea    -0x230(%rbp),%rsi
mov    $0x100,%edx
call   1050 <readlink@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    133c <main+0x16c>
lea    0xce6(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1398 <main+0x1c8>
lea    -0x230(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10b0 <lstat@plt>
cmp    $0x0,%rax
je     1377 <main+0x1a7>
lea    -0x130(%rbp),%rsi
lea    -0x230(%rbp),%rdx
lea    0xcc2(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1393 <main+0x1c3>
lea    -0x130(%rbp),%rsi
lea    -0x230(%rbp),%rdx
lea    0xcbc(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    1398 <main+0x1c8>
jmp    139d <main+0x1cd>
jmp    13a2 <main+0x1d2>
jmp    1279 <main+0xa9>
mov    -0x18(%rbp),%rdi
call   10a0 <closedir@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
cmp    $0x2,%edi
jl     11fc <main+0x1c>
mov    0x8(%rsi),%rbx
jmp    1203 <main+0x23>
lea    0xdfe(%rip),%rbx
mov    %rbx,%rdi
call   1080 <chdir@plt>
test   %rax,%rax
jne    1221 <main+0x41>
lea    0xde9(%rip),%rdi
call   1070 <opendir@plt>
test   %rax,%rax
jne    1237 <main+0x57>
mov    %rbx,%rdi
call   10f0 <perror@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
jne    1256 <main+0x76>
mov    %r14,%rdi
call   10b0 <closedir@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0x110(%rsp),%rbx
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2d71(%rip),%rbp
lea    0xd8d(%rip),%r13
jmp    129b <main+0xbb>
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10d0 <fprintf@plt>
mov    %r14,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     1247 <main+0x67>
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <lstat@plt>
test   %rax,%rax
jne    1280 <main+0xa0>
mov    0x8(%rsp),%edi
call   1040 <S_ISLNK@plt>
test   %rax,%rax
je     128e <main+0xae>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   10a0 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1060 <readlink@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x120>
mov    %r12,%rdi
mov    %r15,%rsi
call   10c0 <lstat@plt>
test   %rax,%rax
je     130e <main+0x12e>
lea    0xd18(%rip),%rdi
jmp    1315 <main+0x135>
lea    0xd3e(%rip),%rdi
call   1050 <puts@plt>
jmp    128e <main+0xae>
lea    0xd1c(%rip),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
jmp    128e <main+0xae>

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"./\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Error lstating %s\0A\00"
@.str.3 = [23 x i8] c"failed using readlink\0A\00"
@.str.4 = [27 x i8] c"%s is a broken link to %s\0A\00"
@.str.5 = [20 x i8] c"%s is a link to %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca %struct.stat
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %11
br label %18
18: ; preds = %14, %2
%19 = load i8*, i8** %11
%20 = call i64 @chdir(i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i8*, i8** %11
%24 = call i32 @perror(i8* %23)
%25 = load i32, i32* @errno
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %18
%28 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %6
%29 = icmp ne i32* %28, null
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = load i8*, i8** %11
%32 = call i32 @perror(i8* %31)
%33 = load i32, i32* @errno
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %27
br label %36
36: ; preds = %82, %35
%37 = load i32*, i32** %6
%38 = call %struct.dirent* @readdir(i32* %37)
store %struct.dirent* %38, %struct.dirent** %7
%39 = icmp ne %struct.dirent* %38, null
br i1 %39, label %40, label %83
40: ; preds = %36
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%42 = load %struct.dirent*, %struct.dirent** %7
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @strcpy(i8* %41, i32 %44)
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%47 = call i64 @lstat(i8* %46, %struct.stat* %8)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %40
%50 = load i32, i32* @stderr
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%52 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %51)
br label %82
53: ; preds = %40
%54 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i64 @S_ISLNK(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %81
58: ; preds = %53
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%60 = call i32 @memset(i8* %59, i32 0, i32 256)
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%63 = call i32 @readlink(i8* %61, i8* %62, i32 256)
%64 = icmp eq i32 -1, %63
br i1 %64, label %65, label %67
65: ; preds = %58
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %80
67: ; preds = %58
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%69 = call i64 @lstat(i8* %68, %struct.stat* %8)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %67
%72 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %72, i8* %73)
br label %79
75: ; preds = %67
%76 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %76, i8* %77)
br label %79
79: ; preds = %75, %71
br label %80
80: ; preds = %79, %65
br label %81
81: ; preds = %80, %53
br label %82
82: ; preds = %81, %49
br label %36
83: ; preds = %36
%84 = load i32*, i32** %6
%85 = call i32 @closedir(i32* %84)
%86 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"./\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Error lstating %s\0A\00"
@.str.4 = [27 x i8] c"%s is a broken link to %s\0A\00"
@.str.5 = [20 x i8] c"%s is a link to %s\0A\00"
@str = [22 x i8] c"failed using readlink\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = bitcast %struct.stat* %3 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
br label %13
13: ; preds = %10, %2
%14 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = tail call i64 @chdir(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* %14)
%19 = load i32, i32* @errno
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
%22 = tail call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = call %struct.dirent* @readdir(i32* %22)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %63, label %27
27: ; preds = %24
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %33
29: ; preds = %21
%30 = tail call i32 @perror(i8* %14)
%31 = load i32, i32* @errno
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %27, %60
%34 = phi %struct.dirent* [ %25, %27 ], [ %61, %60 ]
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @strcpy(i8* %7, i32 %36)
%38 = call i64 @lstat(i8* %7, %struct.stat* %3)
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %60
43: ; preds = %33
%44 = load i32, i32* %28
%45 = call i64 @S_ISLNK(i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %60, label %47
47: ; preds = %43
%48 = call i32 @memset(i8* %8, i32 0, i32 256)
%49 = call i32 @readlink(i8* %7, i8* %8, i32 256)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %60
53: ; preds = %47
%54 = call i64 @lstat(i8* %8, %struct.stat* %3)
%55 = icmp eq i64 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %7, i8* %8)
br label %60
58: ; preds = %53
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %7, i8* %8)
br label %60
60: ; preds = %43, %56, %58, %51, %40
%61 = call %struct.dirent* @readdir(i32* %22)
%62 = icmp eq %struct.dirent* %61, null
br i1 %62, label %63, label %33
63: ; preds = %60, %24
%64 = call i32 @closedir(i32* %22)
%65 = call i32 @exit(i32 0)
unreachable
}