#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 scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(void) {
  pid_t pid;
  char cwdir_child[100];
  if ((pid = fork()) < 0) {
    perror("fork error");
    exit(1);
  }
  if (pid == 0) {
    if (chdir("/tmp") < 0) {
      perror("chdir error");
    }
    if (getcwd(cwdir_child, 100) != NULL) {
      printf("The directory of the child is : %s\n", cwdir_child);
    } else {
      perror("getcwd client error");
    }
  } else {
    if (getcwd(cwdir_child, 100) != NULL) {
      printf("The directory of the parent is : %s\n", cwdir_child);
    } else {
      perror("getcwd parent error");
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    119d <main+0x3d>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1208 <main+0xa8>
lea    0xe5c(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    11ca <main+0x6a>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
je     11f7 <main+0x97>
lea    -0x80(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
jmp    1203 <main+0xa3>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
jmp    1246 <main+0xe6>
lea    -0x80(%rbp),%rdi
mov    $0x64,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
je     1235 <main+0xd5>
lea    -0x80(%rbp),%rsi
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
jmp    1241 <main+0xe1>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
jmp    1246 <main+0xe6>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x68,%rsp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11eb <main+0x8b>
je     118d <main+0x2d>
mov    %rsp,%rdi
mov    $0x64,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11c6 <main+0x66>
lea    0xec9(%rip),%rdi
jmp    11b7 <main+0x57>
lea    0xe77(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11cf <main+0x6f>
mov    %rsp,%rdi
mov    $0x64,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11dd <main+0x7d>
lea    0xe65(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x68,%rsp
ret
lea    0xeac(%rip),%rdi
jmp    11e4 <main+0x84>
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
jmp    119e <main+0x3e>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
jmp    11bf <main+0x5f>
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [12 x i8] c"chdir error\00"
@.str.3 = [36 x i8] c"The directory of the child is : %s\0A\00"
@.str.4 = [20 x i8] c"getcwd client error\00"
@.str.5 = [37 x i8] c"The directory of the parent is : %s\0A\00"
@.str.6 = [20 x i8] c"getcwd parent error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %9
%13 = call i64 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%19 = call i32* @getcwd(i8* %18, i32 100)
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%23 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
br label %37
27: ; preds = %9
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%29 = call i32* @getcwd(i8* %28, i32 100)
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %32)
br label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
br label %37
37: ; preds = %36, %26
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [12 x i8] c"chdir error\00"
@.str.3 = [36 x i8] c"The directory of the child is : %s\0A\00"
@.str.4 = [20 x i8] c"getcwd client error\00"
@.str.5 = [37 x i8] c"The directory of the parent is : %s\0A\00"
@.str.6 = [20 x i8] c"getcwd parent error\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
br i1 %9, label %10, label %22
10: ; preds = %8
%11 = tail call i64 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = call i32* @getcwd(i8* %2, i32 100)
%17 = icmp eq i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %29
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %29
22: ; preds = %8
%23 = call i32* @getcwd(i8* %2, i32 100)
%24 = icmp eq i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %2)
br label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %25, %27, %18, %20
ret i32 0
}