#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 st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
scalar_t__ fchdir(int);
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(void) {
  int fd;
  struct stat sbuf;
  fd = open(".", O_RDONLY);
  fstat(fd, &sbuf);
  chdir("..");
  fchmod(fd, 0);
  if (fchdir(fd) < 0)
    perror("fchdir back");
  fchmod(fd, sbuf.st_mode & 07777);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1090 <fstat@plt>
lea    0xe56(%rip),%rdi
call   1030 <chdir@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <fchmod@plt>
mov    -0x8(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0x0,%rax
jge    11d7 <main+0x67>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
and    $0xfff,%esi
call   1060 <fchmod@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
lea    0xe61(%rip),%rdi
call   1030 <chdir@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <fchmod@plt>
mov    %ebx,%edi
call   1050 <fchdir@plt>
test   %rax,%rax
js     11d8 <main+0x68>
mov    $0xfff,%esi
and    0x8(%rsp),%esi
mov    %ebx,%edi
call   1060 <fchmod@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
jmp    11b9 <main+0x49>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [12 x i8] c"fchdir back\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @fstat(i32 %6, %struct.stat* %3)
%8 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* %2
%10 = call i32 @fchmod(i32 %9, i32 0)
%11 = load i32, i32* %2
%12 = call i64 @fchdir(i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, 4095
%21 = call i32 @fchmod(i32 %17, i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [12 x i8] c"fchdir back\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @fstat(i32 %4, %struct.stat* %1)
%6 = tail call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @fchmod(i32 %4, i32 0)
%8 = tail call i64 @fchdir(i32 %4)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%14 = load i32, i32* %13
%15 = and i32 %14, 4095
%16 = tail call i32 @fchmod(i32 %4, i32 %15)
%17 = tail call i32 @close(i32 %4)
ret i32 0
}