#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 */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ open(char *, int, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ realpath(char *, char *);
int /*<<< orphan*/ rmdir(char *);
int /*<<< orphan*/ symlink(char *, char *);
int /*<<< orphan*/ unlink(char *);
main() {
  char buf[1024];
  realpath("fnord", buf);
  puts(buf);
  mkdir("foo", 0755);
  symlink("foo", "bar");
  close(open("foo/blah", O_WRONLY | O_CREAT, 0700));
  realpath("bar/blah", buf);
  puts(buf);
  symlink("..", "foo/blub");
  realpath("bar/blub/foo/blub", buf);
  puts(buf);
  unlink("foo/blub");
  unlink("foo/blah");
  rmdir("foo");
  unlink("bar");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
lea    -0x400(%rbp),%rsi
lea    0xe67(%rip),%rdi
call   1090 <realpath@plt>
lea    -0x400(%rbp),%rdi
call   1050 <puts@plt>
lea    0xe55(%rip),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
lea    0xe44(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1070 <symlink@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
lea    0xe27(%rip),%rdi
mov    $0x1c0,%edx
call   10a0 <open@plt>
mov    %eax,%edi
call   1080 <close@plt>
lea    -0x400(%rbp),%rsi
lea    0xe11(%rip),%rdi
call   1090 <realpath@plt>
lea    -0x400(%rbp),%rdi
call   1050 <puts@plt>
lea    0xe02(%rip),%rdi
lea    0xe07(%rip),%rsi
call   1070 <symlink@plt>
lea    -0x400(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1090 <realpath@plt>
lea    -0x400(%rbp),%rdi
call   1050 <puts@plt>
lea    0xddc(%rip),%rdi
call   1030 <unlink@plt>
lea    0xdb2(%rip),%rdi
call   1030 <unlink@plt>
lea    0xd9e(%rip),%rdi
call   1060 <rmdir@plt>
lea    0xd96(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
lea    0xe69(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1090 <realpath@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
lea    0xe55(%rip),%r14
mov    %r14,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
lea    0xe45(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <symlink@plt>
mov    0x2df9(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe25(%rip),%r12
mov    %r12,%rdi
mov    $0x1c0,%edx
call   10a0 <open@plt>
mov    %eax,%edi
call   1080 <close@plt>
lea    0xe13(%rip),%rdi
mov    %rbx,%rsi
call   1090 <realpath@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
lea    0xe05(%rip),%rdi
lea    0xe0a(%rip),%r13
mov    %r13,%rsi
call   1070 <symlink@plt>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
call   1090 <realpath@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %r13,%rdi
call   1030 <unlink@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
mov    %r14,%rdi
call   1060 <rmdir@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"fnord\00"
@.str.1 = [4 x i8] c"foo\00"
@.str.2 = [4 x i8] c"bar\00"
@.str.3 = [9 x i8] c"foo/blah\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.4 = [9 x i8] c"bar/blah\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [9 x i8] c"foo/blub\00"
@.str.7 = [18 x i8] c"bar/blub/foo/blub\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @realpath(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %2)
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = call i32 @puts(i8* %4)
%6 = call i32 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 493)
%7 = call i32 @symlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %10, i32 448)
%12 = call i32 @close(i32 %11)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%14 = call i32 @realpath(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%16 = call i32 @puts(i8* %15)
%17 = call i32 @symlink(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%19 = call i32 @realpath(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%21 = call i32 @puts(i8* %20)
%22 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%23 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @rmdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"fnord\00"
@.str.1 = [4 x i8] c"foo\00"
@.str.2 = [4 x i8] c"bar\00"
@.str.3 = [9 x i8] c"foo/blah\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.4 = [9 x i8] c"bar/blah\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [9 x i8] c"foo/blub\00"
@.str.7 = [18 x i8] c"bar/blub/foo/blub\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @realpath(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %2)
%4 = call i32 @puts(i8* %2)
%5 = call i32 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 493)
%6 = call i32 @symlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %9, i32 448)
%11 = call i32 @close(i32 %10)
%12 = call i32 @realpath(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%13 = call i32 @puts(i8* %2)
%14 = call i32 @symlink(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%15 = call i32 @realpath(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%16 = call i32 @puts(i8* %2)
%17 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%18 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @rmdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}