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

/* Variables and functions */
int chdir(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid;
  char buf[1024];
  if (getcwd(buf, 1024) == NULL) {
    perror("fail to get pwd");
    exit(1);
  }
  printf("the parent’s pwd is : %s\n", buf);
  pid = fork();
  if (pid == -1) {
    perror("fail to fork");
    exit(1);
  } else if (pid == 0) {
    printf("the child\n");
    if (chdir("./test-dir") == -1) {
      perror("fail to change dir");
      exit(1);
    }
    if (getcwd(buf, 1024) == NULL) {
      perror("fail to get pwd");
      exit(1);
    }
    printf("the child’s pwd is : %s\n", buf);
  } else {
    if (wait(NULL) == -1) {
      perror("fail to wait");
      exit(1);
    }
    if (getcwd(buf, 1024) == NULL) {
      perror("fail to get pwd");
      exit(1);
    }
    printf("the parent’s pwd is : %s\n", buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    11b3 <main+0x43>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f2 <main+0x82>
lea    0xe49(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1280 <main+0x110>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe33(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1235 <main+0xc5>
lea    0xe29(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    1266 <main+0xf6>
lea    0xda9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ee <main+0x17e>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    12a8 <main+0x138>
lea    0xde4(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    12d9 <main+0x169>
lea    0xd36(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f3 <main+0x183>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     1230 <main+0xb0>
lea    0xe6b(%rip),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11e2 <main+0x62>
cmp    $0xffffffff,%eax
je     1239 <main+0xb9>
xor    %edi,%edi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xc2>
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
jne    1218 <main+0x98>
jmp    1230 <main+0xb0>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
lea    0xe44(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     1230 <main+0xb0>
lea    0xe3f(%rip),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
ret
lea    0xdc9(%rip),%rdi
jmp    1252 <main+0xd2>
lea    0xdec(%rip),%rdi
jmp    1252 <main+0xd2>
lea    0xe29(%rip),%rdi
jmp    1252 <main+0xd2>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"fail to get pwd\00"
@.str.1 = [28 x i8] c"the parent\E2\80\99s pwd is : %s\0A\00"
@.str.2 = [13 x i8] c"fail to fork\00"
@.str.3 = [11 x i8] c"the child\0A\00"
@.str.4 = [11 x i8] c"./test-dir\00"
@.str.5 = [19 x i8] c"fail to change dir\00"
@.str.6 = [27 x i8] c"the child\E2\80\99s pwd is : %s\0A\00"
@.str.7 = [13 x i8] c"fail to wait\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
store i32 0, i32* %1
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32* @getcwd(i8* %4, i32 1024)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 (...) @fork()
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %39
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @chdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%31 = call i32* @getcwd(i8* %30, i32 1024)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* %37)
br label %55
39: ; preds = %19
%40 = call i32 @wait(i32* null)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %39
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%47 = call i32* @getcwd(i8* %46, i32 1024)
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %45
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %52, %36
br label %56
56: ; preds = %55
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"fail to get pwd\00"
@.str.1 = [28 x i8] c"the parent\E2\80\99s pwd is : %s\0A\00"
@.str.2 = [13 x i8] c"fail to fork\00"
@.str.4 = [11 x i8] c"./test-dir\00"
@.str.5 = [19 x i8] c"fail to change dir\00"
@.str.6 = [27 x i8] c"the child\E2\80\99s pwd is : %s\0A\00"
@.str.7 = [13 x i8] c"fail to wait\00"
@str = [10 x i8] c"the child\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32* @getcwd(i8* %2, i32 1024)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%10 = call i32 (...) @fork()
switch i32 %10, label %27 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %8
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%16 = call i32 @chdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32* @getcwd(i8* %2, i32 1024)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %39
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %8
%28 = call i32 @wait(i32* null)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = call i32* @getcwd(i8* %2, i32 1024)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33, %21
%40 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), %21 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %33 ]
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %40, i8* %2)
ret i32 0
}