#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*/ cwd;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ print_dir_details(char *);
int /*<<< orphan*/ print_file_details(struct stat, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  char cwd[1024];
  getcwd(cwd, sizeof(cwd));
  int len = strlen(cwd);
  char full_path[1024];
  if (argc > 1) {
    int arg_len = strlen(argv[1]);
    int full_len = 1 + len + arg_len;
    sprintf(full_path, "%s/%s", cwd, argv[1]);
    full_path[full_len] = '\0';
  } else {
    strncpy(full_path, cwd, len);
    full_path[len] = '\0';
  }
  struct stat file_stats;
  if (stat(full_path, &file_stats) < 0) {
    return 1;
  }
  if (S_ISDIR(file_stats.st_mode)) {
    print_dir_details(full_path);
  } else {
    print_file_details(file_stats, full_path);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1070 <getcwd@plt>
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1224 <main+0xa4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x824(%rbp)
mov    -0x414(%rbp),%eax
add    $0x1,%eax
add    -0x824(%rbp),%eax
mov    %eax,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
lea    -0x410(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdf5(%rip),%rsi
call   10a0 <sprintf@plt>
movslq -0x828(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
jmp    124c <main+0xcc>
lea    -0x820(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <strncpy@plt>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
lea    -0x820(%rbp),%rdi
lea    -0x830(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    1275 <main+0xf5>
movl   $0x1,-0x4(%rbp)
jmp    12b4 <main+0x134>
mov    -0x830(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     129b <main+0x11b>
lea    -0x820(%rbp),%rdi
call   1040 <print_dir_details@plt>
jmp    12ad <main+0x12d>
lea    -0x820(%rbp),%rsi
mov    -0x830(%rbp),%edi
call   1080 <print_file_details@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x810,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <getcwd@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
cmp    $0x2,%ebp
jl     11e1 <main+0x61>
mov    0x8(%r14),%rdi
call   1050 <strlen@plt>
add    %eax,%ebx
add    $0x1,%ebx
mov    0x8(%r14),%rcx
lea    0xe33(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x410(%rsp),%rdx
call   10a0 <sprintf@plt>
jmp    11f5 <main+0x75>
lea    0x10(%rsp),%rdi
lea    0x410(%rsp),%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
test   %rax,%rax
js     122b <main+0xab>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     1232 <main+0xb2>
lea    0x10(%rsp),%rdi
call   1040 <print_dir_details@plt>
jmp    1240 <main+0xc0>
mov    $0x1,%eax
jmp    1242 <main+0xc2>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
call   1080 <print_file_details@plt>
xor    %eax,%eax
add    $0x810,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = call i32 @getcwd(i8* %12, i32 1024)
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %7
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %36
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %9
%23 = load i32, i32* %7
%24 = add nsw i32 1, %23
%25 = load i32, i32* %9
%26 = add nsw i32 %24, %25
store i32 %26, i32* %10
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %28, i8* %31)
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %34
store i8 0, i8* %35
br label %44
36: ; preds = %2
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%39 = load i32, i32* %7
%40 = call i32 @strncpy(i8* %37, i8* %38, i32 %39)
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %36, %18
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%46 = call i64 @stat(i8* %45, %struct.stat* %11)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 1, i32* %3
br label %63
49: ; preds = %44
%50 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @S_ISDIR(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%56 = call i32 @print_dir_details(i8* %55)
br label %62
57: ; preds = %49
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%59 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @print_file_details(i32 %60, i8* %58)
br label %62
62: ; preds = %57, %54
store i32 0, i32* %3
br label %63
63: ; preds = %62, %48
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 @getcwd(i8* %6, i32 1024)
%8 = call i32 @strlen(i8* %6)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %8, 1
%16 = add nsw i32 %15, %14
%17 = load i8*, i8** %12
%18 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %17)
br label %21
19: ; preds = %2
%20 = call i32 @strncpy(i8* %9, i8* %6, i32 %8)
br label %21
21: ; preds = %19, %11
%22 = phi i32 [ %8, %19 ], [ %16, %11 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
%25 = bitcast %struct.stat* %5 to i8*
%26 = call i64 @stat(i8* %9, %struct.stat* %5)
%27 = icmp slt i64 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISDIR(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = call i32 @print_dir_details(i8* %9)
br label %38
35: ; preds = %28
%36 = load i32, i32* %29
%37 = call i32 @print_file_details(i32 %36, i8* %9)
br label %38
38: ; preds = %33, %35, %21
%39 = phi i32 [ 1, %21 ], [ 0, %35 ], [ 0, %33 ]
ret i32 %39
}