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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ S_ISLNK(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int lstat(char *, struct stat *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int read(int, char *, size_t);
struct dirent *readdir(int /*<<< orphan*/ *);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(int /*<<< orphan*/);
int strlen(char *);
int write(int, ...);
void savedir(int fd, char *dirname) {
  puts(dirname);
  DIR *dd = opendir(dirname);
  if (dd == NULL) {
    perror("opendir");
    exit(1);
  }
  struct dirent *r;
  while ((r = readdir(dd)) != NULL) {
    char *name = strdup(r->d_name);
    size_t plength = strlen(name) + strlen(dirname) + 2;
    char *path = malloc(plength);
    strcat(path, dirname);
    strcat(path, "\\");
    strcat(path, name);
    path[plength] = '\0';
    struct stat s;
    if (lstat(path, &s) == -1) {
      perror("lstat");
    } else if (!S_ISDIR(s.st_mode)) {
      int td = open(name, O_RDONLY);
      char len[2];
      ssize_t inr = read(td, len, 2);
      if (inr < 2) {
        perror("read length");
      } else {
        size_t length;
        char *data;
        int allright = 1;
        if (!S_ISLNK(s.st_mode)) {
          length = (size_t)len[0] + 256 * (size_t)len[1];
          data = malloc(length);
          ssize_t finr = read(td, data, length);
          if (finr < 0) {
            perror("read data");
            allright = 0;
          }
        } else {
          data = malloc(s.st_size + 1);
          length = readlink(name, data, s.st_size + 1);
          if (length < 0 || length > s.st_size) {
            perror("readlink");
            allright = 0;
          }
          data[s.st_size + 1] = '\0';
        }
        if (allright == 1) {
          int plen[2] = {plength % 256, plength / 256};
          ssize_t w = write(fd, plen, 2);
          w = write(fd, path, plength);
          w = write(fd, len, 2);
          w = write(fd, data, length);
        }
        free(data);
      }
      close(td);
    } else {
      savedir(fd, path);
    }
    free(name);
    free(path);
  }
}


x86 assembly (clang, -O0):
<savedir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1080 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1279 <savedir+0x49>
lea    0xd96(%rip),%rdi
call   1120 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>
jmp    127e <savedir+0x4e>
mov    -0x18(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1517 <savedir+0x2e7>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1150 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <strlen@plt>
mov    %eax,-0x78(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,%edi
call   10f0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1130 <strcat@plt>
mov    -0x38(%rbp),%rdi
lea    0xd18(%rip),%rsi
call   1130 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1130 <strcat@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10d0 <lstat@plt>
cmp    $0xffffffff,%eax
jne    1335 <savedir+0x105>
lea    0xcdf(%rip),%rdi
call   1120 <perror@plt>
jmp    1500 <savedir+0x2d0>
mov    -0x3c(%rbp),%edi
call   10a0 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    14ef <savedir+0x2bf>
mov    -0x28(%rbp),%rdi
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
call   1110 <open@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x46(%rbp),%rsi
mov    $0x2,%edx
call   10c0 <read@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x2,-0x4c(%rbp)
jge    138a <savedir+0x15a>
lea    0xc90(%rip),%rdi
call   1120 <perror@plt>
jmp    14e2 <savedir+0x2b2>
movl   $0x1,-0x64(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    13fb <savedir+0x1cb>
movsbq -0x46(%rbp),%rax
movsbq -0x45(%rbp),%rcx
shl    $0x8,%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %eax,%edi
call   10f0 <malloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   10c0 <read@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jge    13f6 <savedir+0x1c6>
lea    0xc32(%rip),%rdi
call   1120 <perror@plt>
movl   $0x0,-0x64(%rbp)
jmp    1461 <savedir+0x231>
mov    -0x40(%rbp),%edi
add    $0x1,%edi
call   10f0 <malloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x40(%rbp),%edx
add    $0x1,%edx
call   1060 <readlink@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jb     143d <savedir+0x20d>
mov    -0x58(%rbp),%rax
movslq -0x40(%rbp),%rcx
cmp    %rcx,%rax
jbe    1450 <savedir+0x220>
lea    0xbe2(%rip),%rdi
call   1120 <perror@plt>
movl   $0x0,-0x64(%rbp)
mov    -0x60(%rbp),%rax
mov    -0x40(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x1,-0x64(%rbp)
jne    14d9 <savedir+0x2a9>
mov    -0x30(%rbp),%rax
and    $0xff,%rax
mov    %eax,-0x70(%rbp)
mov    -0x30(%rbp),%rax
shr    $0x8,%rax
mov    %eax,-0x6c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1070 <write@plt>
mov    %eax,-0x74(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1070 <write@plt>
mov    %eax,-0x74(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x46(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1070 <write@plt>
mov    %eax,-0x74(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdx
mov    $0x0,%al
call   1070 <write@plt>
mov    %eax,-0x74(%rbp)
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x44(%rbp),%edi
call   10b0 <close@plt>
jmp    14fb <savedir+0x2cb>
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1100 <savedir@plt>
jmp    1500 <savedir+0x2d0>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    127e <savedir+0x4e>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<savedir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,0x14(%rsp)
mov    %rsi,%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1080 <opendir@plt>
test   %rax,%rax
je     14bd <savedir+0x28d>
mov    %rax,%rbx
mov    %rax,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     14ae <savedir+0x27e>
mov    %r14,0x20(%rsp)
mov    %rbx,0x18(%rsp)
jmp    12b2 <savedir+0x82>
xchg   %ax,%ax
mov    0x14(%rsp),%edi
mov    %r15,%rsi
call   1100 <savedir@plt>
mov    0x18(%rsp),%rbx
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     14ae <savedir+0x27e>
mov    (%rax),%edi
call   1150 <strdup@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1090 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%r12
mov    %r12d,%edi
call   10f0 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1130 <strcat@plt>
mov    %r15,%rdi
lea    0xd12(%rip),%rsi
call   1130 <strcat@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1130 <strcat@plt>
movb   $0x0,(%r15,%r12,1)
mov    %r15,%rdi
lea    0x28(%rsp),%rsi
call   10d0 <lstat@plt>
cmp    $0xffffffff,%eax
je     145f <savedir+0x22f>
mov    0x2c(%rsp),%ebx
mov    %ebx,%edi
call   10a0 <S_ISDIR@plt>
test   %eax,%eax
jne    1280 <savedir+0x50>
mov    0x2cad(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1110 <open@plt>
mov    $0x2,%edx
mov    %eax,0x10(%rsp)
mov    %eax,%edi
lea    0x6(%rsp),%rsi
call   10c0 <read@plt>
cmp    $0x1,%eax
jle    1470 <savedir+0x240>
mov    %ebx,%edi
call   1040 <S_ISLNK@plt>
test   %eax,%eax
je     13af <savedir+0x17f>
movslq 0x28(%rsp),%rbp
lea    0x1(%rbp),%r14
mov    %r14d,%edi
call   10f0 <malloc@plt>
mov    %r13,%rdi
mov    %rax,0x8(%rsp)
mov    %rax,%rsi
mov    %r14d,%edx
call   1060 <readlink@plt>
cmp    %rbp,%rax
ja     147e <savedir+0x24e>
mov    %rax,%rbx
mov    0x8(%rsp),%rax
movb   $0x0,(%rax,%r14,1)
mov    0x20(%rsp),%r14
jmp    13e5 <savedir+0x1b5>
movsbq 0x6(%rsp),%rax
movsbq 0x7(%rsp),%rbx
shl    $0x8,%rbx
add    %rax,%rbx
mov    %ebx,%edi
call   10f0 <malloc@plt>
mov    0x10(%rsp),%edi
mov    %rax,0x8(%rsp)
mov    %rax,%rsi
mov    %rbx,%rdx
call   10c0 <read@plt>
test   %eax,%eax
js     149b <savedir+0x26b>
movzbl %r12b,%eax
mov    %eax,0x30(%rsp)
mov    %r12d,%eax
sar    $0x8,%eax
mov    %eax,0x34(%rsp)
mov    0x14(%rsp),%ebp
mov    %ebp,%edi
lea    0x30(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1070 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <write@plt>
mov    %ebp,%edi
lea    0x6(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1070 <write@plt>
mov    %ebp,%edi
mov    0x8(%rsp),%rbp
mov    %rbp,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <write@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rbx
mov    0x10(%rsp),%edi
call   10b0 <close@plt>
jmp    1291 <savedir+0x61>
lea    0xba4(%rip),%rdi
call   1120 <perror@plt>
jmp    128c <savedir+0x5c>
lea    0xb99(%rip),%rdi
call   1120 <perror@plt>
jmp    144c <savedir+0x21c>
lea    0xba1(%rip),%rdi
call   1120 <perror@plt>
mov    0x8(%rsp),%rdi
movb   $0x0,(%rdi,%r14,1)
mov    0x20(%rsp),%r14
jmp    1447 <savedir+0x217>
lea    0xb7a(%rip),%rdi
call   1120 <perror@plt>
mov    0x8(%rsp),%rdi
jmp    1447 <savedir+0x217>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb3c(%rip),%rdi
call   1120 <perror@plt>
mov    $0x1,%edi
call   1140 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32, i32 }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [2 x i8] c"\\\00"
@.str.2 = [6 x i8] c"lstat\00"
@O_RDONLY = global i32 0
@.str.3 = [12 x i8] c"read length\00"
@.str.4 = [10 x i8] c"read data\00"
@.str.5 = [9 x i8] c"readlink\00"
define void @savedir(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca [2 x i8]
%13 = alloca i32
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca [2 x i32]
%19 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%20 = load i8*, i8** %4
%21 = call i32 @puts(i8* %20)
%22 = load i8*, i8** %4
%23 = call i32* @opendir(i8* %22)
store i32* %23, i32** %5
%24 = load i32*, i32** %5
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %2
br label %30
30: ; preds = %171, %29
%31 = load i32*, i32** %5
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %6
%33 = icmp ne %struct.dirent* %32, null
br i1 %33, label %34, label %176
34: ; preds = %30
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i8* @strdup(i32 %37)
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
%41 = load i8*, i8** %4
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %40, %42
%44 = add nsw i32 %43, 2
%45 = sext i32 %44 to i64
store i64 %45, i64* %8
%46 = load i64, i64* %8
%47 = trunc i64 %46 to i32
%48 = call i8* @malloc(i32 %47)
store i8* %48, i8** %9
%49 = load i8*, i8** %9
%50 = load i8*, i8** %4
%51 = call i32 @strcat(i8* %49, i8* %50)
%52 = load i8*, i8** %9
%53 = call i32 @strcat(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%54 = load i8*, i8** %9
%55 = load i8*, i8** %7
%56 = call i32 @strcat(i8* %54, i8* %55)
%57 = load i8*, i8** %9
%58 = load i64, i64* %8
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %9
%61 = call i32 @lstat(i8* %60, %struct.stat* %10)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %34
%64 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %171
65: ; preds = %34
%66 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%67 = load i32, i32* %66
%68 = call i32 @S_ISDIR(i32 %67)
%69 = icmp ne i32 %68, 0
br i1 %69, label %167, label %70
70: ; preds = %65
%71 = load i8*, i8** %7
%72 = load i32, i32* @O_RDONLY
%73 = call i32 @open(i8* %71, i32 %72)
store i32 %73, i32* %11
%74 = load i32, i32* %11
%75 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%76 = call i32 @read(i32 %74, i8* %75, i64 2)
store i32 %76, i32* %13
%77 = load i32, i32* %13
%78 = icmp slt i32 %77, 2
br i1 %78, label %79, label %81
79: ; preds = %70
%80 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %164
81: ; preds = %70
store i32 1, i32* %16
%82 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%83 = load i32, i32* %82
%84 = call i32 @S_ISLNK(i32 %83)
%85 = icmp ne i32 %84, 0
br i1 %85, label %107, label %86
86: ; preds = %81
%87 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%88 = load i8, i8* %87
%89 = sext i8 %88 to i64
%90 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 1
%91 = load i8, i8* %90
%92 = sext i8 %91 to i64
%93 = mul i64 256, %92
%94 = add i64 %89, %93
store i64 %94, i64* %14
%95 = load i64, i64* %14
%96 = trunc i64 %95 to i32
%97 = call i8* @malloc(i32 %96)
store i8* %97, i8** %15
%98 = load i32, i32* %11
%99 = load i8*, i8** %15
%100 = load i64, i64* %14
%101 = call i32 @read(i32 %98, i8* %99, i64 %100)
store i32 %101, i32* %17
%102 = load i32, i32* %17
%103 = icmp slt i32 %102, 0
br i1 %103, label %104, label %106
104: ; preds = %86
%105 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %16
br label %106
106: ; preds = %104, %86
br label %135
107: ; preds = %81
%108 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%109 = load i32, i32* %108
%110 = add nsw i32 %109, 1
%111 = call i8* @malloc(i32 %110)
store i8* %111, i8** %15
%112 = load i8*, i8** %7
%113 = load i8*, i8** %15
%114 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%115 = load i32, i32* %114
%116 = add nsw i32 %115, 1
%117 = call i64 @readlink(i8* %112, i8* %113, i32 %116)
store i64 %117, i64* %14
%118 = load i64, i64* %14
%119 = icmp ult i64 %118, 0
br i1 %119, label %126, label %120
120: ; preds = %107
%121 = load i64, i64* %14
%122 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%123 = load i32, i32* %122
%124 = sext i32 %123 to i64
%125 = icmp ugt i64 %121, %124
br i1 %125, label %126, label %128
126: ; preds = %120, %107
%127 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %16
br label %128
128: ; preds = %126, %120
%129 = load i8*, i8** %15
%130 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%131 = load i32, i32* %130
%132 = add nsw i32 %131, 1
%133 = sext i32 %132 to i64
%134 = getelementptr inbounds i8, i8* %129, i64 %133
store i8 0, i8* %134
br label %135
135: ; preds = %128, %106
%136 = load i32, i32* %16
%137 = icmp eq i32 %136, 1
br i1 %137, label %138, label %161
138: ; preds = %135
%139 = getelementptr inbounds [2 x i32], [2 x i32]* %18, i64 0, i64 0
%140 = load i64, i64* %8
%141 = urem i64 %140, 256
%142 = trunc i64 %141 to i32
store i32 %142, i32* %139
%143 = getelementptr inbounds i32, i32* %139, i64 1
%144 = load i64, i64* %8
%145 = udiv i64 %144, 256
%146 = trunc i64 %145 to i32
store i32 %146, i32* %143
%147 = load i32, i32* %3
%148 = getelementptr inbounds [2 x i32], [2 x i32]* %18, i64 0, i64 0
%149 = call i32 (i32, ...) @write(i32 %147, i32* %148, i32 2)
store i32 %149, i32* %19
%150 = load i32, i32* %3
%151 = load i8*, i8** %9
%152 = load i64, i64* %8
%153 = call i32 (i32, ...) @write(i32 %150, i8* %151, i64 %152)
store i32 %153, i32* %19
%154 = load i32, i32* %3
%155 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%156 = call i32 (i32, ...) @write(i32 %154, i8* %155, i32 2)
store i32 %156, i32* %19
%157 = load i32, i32* %3
%158 = load i8*, i8** %15
%159 = load i64, i64* %14
%160 = call i32 (i32, ...) @write(i32 %157, i8* %158, i64 %159)
store i32 %160, i32* %19
br label %161
161: ; preds = %138, %135
%162 = load i8*, i8** %15
%163 = call i32 @free(i8* %162)
br label %164
164: ; preds = %161, %79
%165 = load i32, i32* %11
%166 = call i32 @close(i32 %165)
br label %170
167: ; preds = %65
%168 = load i32, i32* %3
%169 = load i8*, i8** %9
br label %170
170: ; preds = %167, %164
br label %171
171: ; preds = %170, %63
%172 = load i8*, i8** %7
%173 = call i32 @free(i8* %172)
%174 = load i8*, i8** %9
%175 = call i32 @free(i8* %174)
br label %30
176: ; preds = %30
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i32 }
@.str = [8 x i8] c"opendir\00"
@.str.1 = [2 x i8] c"\\\00"
@.str.2 = [6 x i8] c"lstat\00"
@O_RDONLY = global i32 0
@.str.3 = [12 x i8] c"read length\00"
@.str.4 = [10 x i8] c"read data\00"
@.str.5 = [9 x i8] c"readlink\00"
define void @savedir(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [2 x i8]
%5 = alloca [2 x i32]
%6 = tail call i32 @puts(i8* dereferenceable(1) %1)
%7 = tail call i32* @opendir(i8* %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %21, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %104, label %12
12: ; preds = %9
%13 = bitcast %struct.stat* %3 to i8*
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%18 = bitcast [2 x i32]* %5 to i8*
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
br label %24
21: ; preds = %2
%22 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %12, %99
%25 = phi %struct.dirent* [ %10, %12 ], [ %102, %99 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i8* @strdup(i32 %27)
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @strlen(i8* %1)
%31 = add i32 %29, 2
%32 = add i32 %31, %30
%33 = sext i32 %32 to i64
%34 = call i8* @malloc(i32 %32)
%35 = call i32 @strcat(i8* %34, i8* %1)
%36 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @strcat(i8* %34, i8* %28)
%38 = getelementptr inbounds i8, i8* %34, i64 %33
store i8 0, i8* %38
%39 = call i32 @lstat(i8* %34, %struct.stat* %3)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %24
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %99
43: ; preds = %24
%44 = load i32, i32* %14
%45 = call i32 @S_ISDIR(i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %98
47: ; preds = %43
%48 = load i32, i32* @O_RDONLY
%49 = call i32 @open(i8* %28, i32 %48)
%50 = call i32 @read(i32 %49, i8* %15, i64 2)
%51 = icmp slt i32 %50, 2
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %96
54: ; preds = %47
%55 = call i32 @S_ISLNK(i32 %44)
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %70
57: ; preds = %54
%58 = load i8, i8* %15
%59 = sext i8 %58 to i64
%60 = load i8, i8* %17
%61 = sext i8 %60 to i64
%62 = shl nsw i64 %61, 8
%63 = add nsw i64 %62, %59
%64 = trunc i64 %63 to i32
%65 = call i8* @malloc(i32 %64)
%66 = call i32 @read(i32 %49, i8* %65, i64 %63)
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %84
68: ; preds = %57
%69 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %93
70: ; preds = %54
%71 = load i32, i32* %16
%72 = add nsw i32 %71, 1
%73 = call i8* @malloc(i32 %72)
%74 = call i64 @readlink(i8* %28, i8* %73, i32 %72)
%75 = sext i32 %71 to i64
%76 = icmp ugt i64 %74, %75
br i1 %76, label %80, label %77
77: ; preds = %70
%78 = sext i32 %72 to i64
%79 = getelementptr inbounds i8, i8* %73, i64 %78
store i8 0, i8* %79
br label %84
80: ; preds = %70
%81 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%82 = sext i32 %72 to i64
%83 = getelementptr inbounds i8, i8* %73, i64 %82
store i8 0, i8* %83
br label %93
84: ; preds = %57, %77
%85 = phi i64 [ %74, %77 ], [ %63, %57 ]
%86 = phi i8* [ %73, %77 ], [ %65, %57 ]
%87 = and i32 %32, 255
store i32 %87, i32* %19
%88 = ashr i32 %32, 8
store i32 %88, i32* %20
%89 = call i32 (i32, ...) @write(i32 %0, i32* %19, i32 2)
%90 = call i32 (i32, ...) @write(i32 %0, i8* %34, i64 %33)
%91 = call i32 (i32, ...) @write(i32 %0, i8* %15, i32 2)
%92 = call i32 (i32, ...) @write(i32 %0, i8* %86, i64 %85)
br label %93
93: ; preds = %80, %68, %84
%94 = phi i8* [ %86, %84 ], [ %73, %80 ], [ %65, %68 ]
%95 = call i32 @free(i8* %94)
br label %96
96: ; preds = %93, %52
%97 = call i32 @close(i32 %49)
br label %99
98: ; preds = %43
br label %99
99: ; preds = %96, %98, %41
%100 = call i32 @free(i8* %28)
%101 = call i32 @free(i8* %34)
%102 = call %struct.dirent* @readdir(i32* %7)
%103 = icmp eq %struct.dirent* %102, null
br i1 %103, label %104, label %24
104: ; preds = %99, %9
ret void
}