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

/* Variables and functions */
scalar_t__ ENOENT;
char *JoinPaths(char *, char *);
int /*<<< orphan*/ PrintError(char const **);
char *basename(char *);
char *dirname(char *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *realpath(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strdup(char const *);
scalar_t__ strlen(char const *);
int main(int argc, const char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "realpath <path>\n");
    return 1;
  }
  const char *path = argv[1];
  char *goodPath = realpath(path, NULL);
  if (goodPath == NULL) {
    if ((errno != ENOENT) || (strlen(path) == 0)) {
      PrintError(argv);
    }
    char *dirCopy = strdup(path);
    char *baseCopy = strdup(path);
    if (dirCopy == NULL || baseCopy == NULL) {
      PrintError(argv);
    }
    char *dir = dirname(dirCopy);
    if (dir == NULL) {
      PrintError(argv);
    }
    char *base = basename(baseCopy);
    if (base == NULL) {
      PrintError(argv);
    }
    char *realdir = realpath(dir, NULL);
    if (realdir == NULL) {
      PrintError(argv);
    }
    goodPath = JoinPaths(realdir, base);
    if (goodPath == NULL) {
      PrintError(argv);
    }
  }
  fprintf(stdout, "%s\n", goodPath);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c3 <main+0x43>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130d <main+0x18d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <realpath@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12eb <main+0x16b>
mov    0x2de6(%rip),%rax
mov    (%rax),%rax
mov    0x2dc4(%rip),%rcx
cmp    (%rcx),%rax
jne    1218 <main+0x98>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1221 <main+0xa1>
mov    -0x10(%rbp),%rdi
call   1090 <PrintError@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1251 <main+0xd1>
cmpq   $0x0,-0x30(%rbp)
jne    125a <main+0xda>
mov    -0x10(%rbp),%rdi
call   1090 <PrintError@plt>
mov    -0x28(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    127b <main+0xfb>
mov    -0x10(%rbp),%rdi
call   1090 <PrintError@plt>
mov    -0x30(%rbp),%rdi
call   1070 <basename@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    129c <main+0x11c>
mov    -0x10(%rbp),%rdi
call   1090 <PrintError@plt>
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <realpath@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12c1 <main+0x141>
mov    -0x10(%rbp),%rdi
call   1090 <PrintError@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1030 <JoinPaths@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12e6 <main+0x166>
mov    -0x10(%rbp),%rdi
call   1090 <PrintError@plt>
jmp    12eb <main+0x16b>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd12(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
cmp    $0x2,%edi
jne    126a <main+0xfa>
mov    %rsi,%r15
mov    0x8(%rsi),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <realpath@plt>
test   %rax,%rax
jne    124e <main+0xde>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
jne    11ba <main+0x4a>
cmpb   $0x0,(%rbx)
jne    11c2 <main+0x52>
mov    %r15,%rdi
call   1080 <PrintError@plt>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%r12
test   %r13,%r13
je     11e2 <main+0x72>
test   %r12,%r12
jne    11ea <main+0x7a>
mov    %r15,%rdi
call   1080 <PrintError@plt>
mov    %r13,%rdi
call   1040 <dirname@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1202 <main+0x92>
mov    %r15,%rdi
call   1080 <PrintError@plt>
mov    %r12,%rdi
call   1060 <basename@plt>
mov    %rax,%r12
test   %rax,%rax
jne    121a <main+0xaa>
mov    %r15,%rdi
call   1080 <PrintError@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <realpath@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1234 <main+0xc4>
mov    %r15,%rdi
call   1080 <PrintError@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <JoinPaths@plt>
test   %rax,%rax
jne    124e <main+0xde>
mov    %r15,%rdi
call   1080 <PrintError@plt>
xor    %eax,%eax
mov    0x2d7b(%rip),%rcx
mov    (%rcx),%edi
lea    0xdb3(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1287 <main+0x117>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"realpath <path>\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@stdout = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %88
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = call i8* @realpath(i8* %22, i32* null)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %84
26: ; preds = %18
%27 = load i64, i64* @errno
%28 = load i64, i64* @ENOENT
%29 = icmp ne i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = call i64 @strlen(i8* %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30, %26
%35 = load i8**, i8*** %5
%36 = call i32 @PrintError(i8** %35)
br label %37
37: ; preds = %34, %30
%38 = load i8*, i8** %6
%39 = call i8* @strdup(i8* %38)
store i8* %39, i8** %8
%40 = load i8*, i8** %6
%41 = call i8* @strdup(i8* %40)
store i8* %41, i8** %9
%42 = load i8*, i8** %8
%43 = icmp eq i8* %42, null
br i1 %43, label %47, label %44
44: ; preds = %37
%45 = load i8*, i8** %9
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %50
47: ; preds = %44, %37
%48 = load i8**, i8*** %5
%49 = call i32 @PrintError(i8** %48)
br label %50
50: ; preds = %47, %44
%51 = load i8*, i8** %8
%52 = call i8* @dirname(i8* %51)
store i8* %52, i8** %10
%53 = load i8*, i8** %10
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = load i8**, i8*** %5
%57 = call i32 @PrintError(i8** %56)
br label %58
58: ; preds = %55, %50
%59 = load i8*, i8** %9
%60 = call i8* @basename(i8* %59)
store i8* %60, i8** %11
%61 = load i8*, i8** %11
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = load i8**, i8*** %5
%65 = call i32 @PrintError(i8** %64)
br label %66
66: ; preds = %63, %58
%67 = load i8*, i8** %10
%68 = call i8* @realpath(i8* %67, i32* null)
store i8* %68, i8** %12
%69 = load i8*, i8** %12
%70 = icmp eq i8* %69, null
br i1 %70, label %71, label %74
71: ; preds = %66
%72 = load i8**, i8*** %5
%73 = call i32 @PrintError(i8** %72)
br label %74
74: ; preds = %71, %66
%75 = load i8*, i8** %12
%76 = load i8*, i8** %11
%77 = call i8* @JoinPaths(i8* %75, i8* %76)
store i8* %77, i8** %7
%78 = load i8*, i8** %7
%79 = icmp eq i8* %78, null
br i1 %79, label %80, label %83
80: ; preds = %74
%81 = load i8**, i8*** %5
%82 = call i32 @PrintError(i8** %81)
br label %83
83: ; preds = %80, %74
br label %84
84: ; preds = %83, %18
%85 = load i32, i32* @stdout
%86 = load i8*, i8** %7
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %86)
store i32 0, i32* %3
br label %88
88: ; preds = %84, %15
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"realpath <path>\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@stdout = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %53
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i8* @realpath(i8* %9, i32* null)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %49
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8, i8* %9
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16, %12
%20 = tail call i32 @PrintError(i8** %1)
br label %21
21: ; preds = %19, %16
%22 = tail call i8* @strdup(i8* %9)
%23 = tail call i8* @strdup(i8* %9)
%24 = icmp eq i8* %22, null
%25 = icmp eq i8* %23, null
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = tail call i32 @PrintError(i8** %1)
br label %29
29: ; preds = %21, %27
%30 = tail call i8* @dirname(i8* %22)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 @PrintError(i8** %1)
br label %34
34: ; preds = %32, %29
%35 = tail call i8* @basename(i8* %23)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 @PrintError(i8** %1)
br label %39
39: ; preds = %37, %34
%40 = tail call i8* @realpath(i8* %30, i32* null)
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = tail call i32 @PrintError(i8** %1)
br label %44
44: ; preds = %42, %39
%45 = tail call i8* @JoinPaths(i8* %40, i8* %35)
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = tail call i32 @PrintError(i8** %1)
br label %49
49: ; preds = %44, %47, %7
%50 = phi i8* [ %10, %7 ], [ null, %47 ], [ %45, %44 ]
%51 = load i32, i32* @stdout
%52 = tail call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %50)
br label %53
53: ; preds = %49, %4
%54 = phi i32 [ 1, %4 ], [ 0, %49 ]
ret i32 %54
}