#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 dummy;
};
typedef int /*<<< orphan*/ s;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ rmdir(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ symlink(char *, char *);
int main() {
  FILE *fp;
  char s[] = "system_calls";
  char buffer[100];
  struct stat state;
  fp = fopen("system_calls.tmp", "w");
  if (fp == NULL) {
    printf("Can not create system_calls.tmp\n");
    exit(0);
  }
  fwrite(s, sizeof(s), 1, fp);
  fseek(fp, 0, SEEK_CUR);
  fread(buffer, sizeof(s), 1, fp);
  fclose(fp);
  getcwd(buffer, 100);
  chdir(".");
  symlink("system_calls.tmp", "system_calls.link");
  remove("system_calls.link");
  rename("system_calls.tmp", "system_calls.tmp1");
  stat("system_calls.tmp", &state);
  access("system_calls.tmp", R_OK);
  remove("system_calls.tmp1");
  mkdir("system_call_mkdir", 0777);
  rmdir("system_call_mkdir");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xde7(%rip),%rax
mov    %rax,-0x1d(%rbp)
mov    0xde5(%rip),%eax
mov    %eax,-0x15(%rbp)
mov    0xde0(%rip),%al
mov    %al,-0x11(%rbp)
lea    0xdd7(%rip),%rdi
lea    0xde1(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1264 <main+0x64>
lea    0xdc8(%rip),%rdi
call   1090 <printf@plt>
xor    %edi,%edi
call   1110 <exit@plt>
lea    -0x1d(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xd,%esi
mov    $0x1,%edx
call   1120 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xd,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
lea    -0x90(%rbp),%rdi
mov    $0x64,%esi
call   10b0 <getcwd@plt>
lea    0xd77(%rip),%rdi
call   1080 <chdir@plt>
lea    0xd37(%rip),%rdi
lea    0xd66(%rip),%rsi
call   10a0 <symlink@plt>
lea    0xd5a(%rip),%rdi
call   1030 <remove@plt>
lea    0xd18(%rip),%rdi
lea    0xd59(%rip),%rsi
call   1100 <rename@plt>
lea    0xd05(%rip),%rdi
lea    -0x98(%rbp),%rsi
call   10c0 <stat@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
lea    0xce9(%rip),%rdi
call   10e0 <access@plt>
lea    0xd25(%rip),%rdi
call   1030 <remove@plt>
lea    0xd2b(%rip),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
lea    0xd1a(%rip),%rdi
call   1070 <rmdir@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x88,%rsp
movabs $0x736c6c61635f6d,%rax
mov    %rax,0x15(%rsp)
movabs $0x635f6d6574737973,%rax
6d 5f 63
mov    %rax,0x10(%rsp)
lea    0xe27(%rip),%rdi
lea    0xdd7(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     132e <main+0x12e>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1120 <fwrite@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fread@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    %r14,%rdi
mov    $0x64,%esi
call   10b0 <getcwd@plt>
lea    0xd68(%rip),%rdi
call   1090 <chdir@plt>
lea    0xda3(%rip),%rbx
lea    0xd57(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <symlink@plt>
mov    %r14,%rdi
call   1030 <remove@plt>
lea    0xd4f(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1100 <rename@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10c0 <stat@plt>
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10e0 <access@plt>
mov    %r14,%rdi
call   1030 <remove@plt>
lea    0xd29(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
mov    %rbx,%rdi
call   1080 <rmdir@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd12(%rip),%rdi
call   1050 <puts@plt>
xor    %edi,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.main.s = [13 x i8] c"system_calls\00"
@.str = [17 x i8] c"system_calls.tmp\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [33 x i8] c"Can not create system_calls.tmp\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [18 x i8] c"system_calls.link\00"
@.str.5 = [18 x i8] c"system_calls.tmp1\00"
@R_OK = global i32 0
@.str.6 = [18 x i8] c"system_call_mkdir\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [13 x i8]
%4 = alloca [100 x i8]
%5 = alloca %struct.stat
store i32 0, i32* %1
%6 = bitcast [13 x i8]* %3 to i8*
%7 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %0
%14 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%15 = load i32*, i32** %2
%16 = call i32 @fwrite(i8* %14, i32 13, i32 1, i32* %15)
%17 = load i32*, i32** %2
%18 = load i32, i32* @SEEK_CUR
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%21 = load i32*, i32** %2
%22 = call i32 @fread(i8* %20, i32 13, i32 1, i32* %21)
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%26 = call i32 @getcwd(i8* %25, i32 100)
%27 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @symlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @rename(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%31 = call i32 @stat(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %struct.stat* %5)
%32 = load i32, i32* @R_OK
%33 = call i32 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @mkdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 511)
%36 = call i32 @rmdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.main.s = [13 x i8] c"system_calls\00"
@.str = [17 x i8] c"system_calls.tmp\00"
@.str.1 = [2 x i8] c"w\00"
@SEEK_CUR = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [18 x i8] c"system_calls.link\00"
@.str.5 = [18 x i8] c"system_calls.tmp1\00"
@R_OK = global i32 0
@.str.6 = [18 x i8] c"system_call_mkdir\00"
@str = [32 x i8] c"Can not create system_calls.tmp\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = alloca [100 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.stat* %3 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %0
%13 = call i32 @fwrite(i8* %4, i32 13, i32 1, i32* %7)
%14 = load i32, i32* @SEEK_CUR
%15 = tail call i32 @fseek(i32* %7, i32 0, i32 %14)
%16 = call i32 @fread(i8* %5, i32 13, i32 1, i32* %7)
%17 = tail call i32 @fclose(i32* %7)
%18 = call i32 @getcwd(i8* %5, i32 100)
%19 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @symlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @rename(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%23 = call i32 @stat(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%24 = load i32, i32* @R_OK
%25 = call i32 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%27 = call i32 @mkdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 511)
%28 = call i32 @rmdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}