#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ link(char *, char *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ readlink(char *, char *, int);
int /*<<< orphan*/ symlink(char *, char *);
int main() {
  int fd;
  char buf[128];
  fd = open("foobar", O_WRONLY | O_CREAT, S_IRWXU);
  close(fd);
  link("foobar", "foobar2");
  link("foobar", "foobar");
  link("nonexist", "foo");
  link((char *)-1, "foo");
  link("nonexist", (char *)-1);
  symlink("foobar", "Sfoobar");
  symlink((char *)-1, "Sfoobar");
  symlink("foobar", (char *)-1);
  readlink("Sfoobar", buf, sizeof(buf));
  readlink((char *)-1, buf, sizeof(buf));
  readlink("Sfoobar", (char *)-1, sizeof(buf));
  readlink("Sfoobar", buf, -1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
or     (%rax),%esi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
lea    0xe92(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xe7b(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <link@plt>
lea    0xe68(%rip),%rsi
mov    %rsi,%rdi
call   1070 <link@plt>
lea    0xe4b(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <link@plt>
mov    $0xffffffffffffffff,%rdi
lea    0xe3a(%rip),%rsi
call   1070 <link@plt>
lea    0xe25(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
call   1070 <link@plt>
lea    0xe20(%rip),%rdi
lea    0xe18(%rip),%rsi
call   1040 <symlink@plt>
mov    $0xffffffffffffffff,%rdi
lea    0xe05(%rip),%rsi
call   1040 <symlink@plt>
lea    0xdfa(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
call   1040 <symlink@plt>
lea    -0x90(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x80,%edx
call   1030 <readlink@plt>
lea    -0x90(%rbp),%rsi
mov    $0xffffffffffffffff,%rdi
mov    $0x80,%edx
call   1030 <readlink@plt>
lea    0xdb6(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
mov    $0x80,%edx
call   1030 <readlink@plt>
lea    -0x90(%rbp),%rsi
lea    0xd97(%rip),%rdi
mov    $0xffffffff,%edx
call   1030 <readlink@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    0x2e65(%rip),%rax
mov    0x2e8e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
lea    0xe98(%rip),%r14
mov    %r14,%rdi
call   1060 <open@plt>
mov    %eax,%edi
call   1050 <close@plt>
lea    0xe6c(%rip),%rsi
mov    %r14,%rdi
call   1070 <link@plt>
mov    %r14,%rdi
mov    %r14,%rsi
call   1070 <link@plt>
lea    0xe5a(%rip),%r15
lea    0xe5c(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <link@plt>
mov    $0xffffffffffffffff,%rdi
mov    %rbx,%rsi
call   1070 <link@plt>
mov    %r15,%rdi
mov    $0xffffffffffffffff,%rsi
call   1070 <link@plt>
lea    0xe30(%rip),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <symlink@plt>
mov    $0xffffffffffffffff,%rdi
mov    %rbx,%rsi
call   1040 <symlink@plt>
mov    %r14,%rdi
mov    $0xffffffffffffffff,%rsi
call   1040 <symlink@plt>
mov    %rsp,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x80,%edx
call   1030 <readlink@plt>
mov    $0xffffffffffffffff,%rdi
mov    %r14,%rsi
mov    $0x80,%edx
call   1030 <readlink@plt>
mov    %rbx,%rdi
mov    $0xffffffffffffffff,%rsi
mov    $0x80,%edx
call   1030 <readlink@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xffffffff,%edx
call   1030 <readlink@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"foobar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [8 x i8] c"foobar2\00"
@.str.2 = [9 x i8] c"nonexist\00"
@.str.3 = [4 x i8] c"foo\00"
@.str.4 = [8 x i8] c"Sfoobar\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [128 x i8]
store i32 0, i32* %1
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRWXU
%8 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = call i32 @close(i32 %9)
%11 = call i32 @link(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @link(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @link(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 @link(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @link(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
%16 = call i32 @symlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @symlink(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @symlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%20 = call i32 @readlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %19, i32 128)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%22 = call i32 @readlink(i8* inttoptr (i64 -1 to i8*), i8* %21, i32 128)
%23 = call i32 @readlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*), i32 128)
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%25 = call i32 @readlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %24, i32 -1)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"foobar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [8 x i8] c"foobar2\00"
@.str.2 = [9 x i8] c"nonexist\00"
@.str.3 = [4 x i8] c"foo\00"
@.str.4 = [8 x i8] c"Sfoobar\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = tail call i32 @close(i32 %7)
%9 = tail call i32 @link(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @link(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @link(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @link(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 @link(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
%14 = tail call i32 @symlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @symlink(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%16 = tail call i32 @symlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
%17 = call i32 @readlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %2, i32 128)
%18 = call i32 @readlink(i8* inttoptr (i64 -1 to i8*), i8* %2, i32 128)
%19 = tail call i32 @readlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*), i32 128)
%20 = call i32 @readlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %2, i32 -1)
ret i32 0
}