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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ show_long(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  DIR *dfp;
  struct dirent *d;
  if (argc > 1) {
    if (chdir(argv[1]))
      err(1, "chdir `%s'", argv[1]);
  }
  if (argc > 2)
    fprintf(stderr, "extra args ignored\n");
  dfp = opendir(".");
  if (dfp == NULL)
    err(EXIT_FAILURE, "opendir");
  while ((d = readdir(dfp)) != NULL)
    show_long(d->d_name);
  closedir(dfp);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c7 <main+0x57>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11c2 <main+0x52>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
jmp    11c7 <main+0x57>
cmpl   $0x2,-0x8(%rbp)
jle    11e6 <main+0x76>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe32(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1218 <main+0xa8>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
jmp    121d <main+0xad>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1244 <main+0xd4>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <show_long@plt>
jmp    121d <main+0xad>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     11bd <main+0x4d>
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     11a3 <main+0x33>
mov    0x8(%r14),%rdx
lea    0xe69(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1090 <err@plt>
cmp    $0x3,%ebx
jb     11bd <main+0x4d>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe5b(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f7 <main+0x87>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
xor    %eax,%eax
call   1090 <err@plt>
jmp    11f7 <main+0x87>
nopw   0x0(%rax,%rax,1)
mov    (%rax),%edi
call   1070 <show_long@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11f0 <main+0x80>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [11 x i8] c"chdir `%s'\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"extra args ignored\0A\00"
@.str.2 = [2 x i8] c".\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [8 x i8] c"opendir\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %22
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @chdir(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %16, %10
br label %22
22: ; preds = %21, %2
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 2
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %25, %22
%29 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %29, i32** %6
%30 = load i32*, i32** %6
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 (i32, i8*, ...) @err(i32 %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %32, %28
br label %36
36: ; preds = %40, %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 %45
40: ; preds = %36
%41 = load %struct.dirent*, %struct.dirent** %7
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @show_long(i32 %43)
br label %36
45: ; preds = %36
%46 = load i32*, i32** %6
%47 = call i32 @closedir(i32* %46)
%48 = load i32, i32* @EXIT_SUCCESS
ret i32 %48
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [11 x i8] c"chdir `%s'\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"extra args ignored\0A\00"
@.str.2 = [2 x i8] c".\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [8 x i8] c"opendir\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %17
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @chdir(i8* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %4
%10 = load i8*, i8** %5
%11 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %4, %9
%13 = icmp ugt i32 %0, 2
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %2, %14, %12
%18 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 (i32, i8*, ...) @err(i32 %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %20, %17
%24 = tail call %struct.dirent* @readdir(i32* %18)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %33, label %26
26: ; preds = %23, %26
%27 = phi %struct.dirent* [ %31, %26 ], [ %24, %23 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i32, i32* %28
%30 = tail call i32 @show_long(i32 %29)
%31 = tail call %struct.dirent* @readdir(i32* %18)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %33, label %26
33: ; preds = %26, %23
%34 = tail call i32 @closedir(i32* %18)
%35 = load i32, i32* @EXIT_SUCCESS
ret i32 %35
}