#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];
  char cwdir_parent[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_parent, 100) != NULL) {
      printf("The directory of the parent is : %s\n", cwdir_parent);
    } else {
      perror("getcwd parent error");
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%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    124c <main+0xec>
lea    -0xf0(%rbp),%rdi
mov    $0x64,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
je     123b <main+0xdb>
lea    -0xf0(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1040 <printf@plt>
jmp    1247 <main+0xe7>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
jmp    124c <main+0xec>
xor    %eax,%eax
add    $0xf0,%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]
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %10
%14 = call i64 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%20 = call i32* @getcwd(i8* %19, i32 100)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%24 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %27
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
br label %38
28: ; preds = %10
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%30 = call i32* @getcwd(i8* %29, i32 100)
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%34 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %33)
br label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
br label %38
38: ; preds = %37, %27
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 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %24
12: ; preds = %10
%13 = tail 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 = tail 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 = call i32* @getcwd(i8* %3, i32 100)
%19 = icmp eq i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %31
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %31
24: ; preds = %10
%25 = call i32* @getcwd(i8* %4, i32 100)
%26 = icmp eq i32* %25, null
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %4)
br label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %31
31: ; preds = %27, %29, %20, %22
ret i32 0
}