#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 *, char *);
int wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid;
  char buf[1024];
  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 parent : %s\n", buf);
  pid = fork();
  if (pid == -1) {
    perror("fail to fork");
    exit(1);
  } else if (pid == 0) {
    if (getcwd(buf, 1024) == NULL) {
      perror("fail to get pwd");
      exit(1);
    }
    printf("the child : %s\n", buf);
  } else {
    if (wait(NULL) == -1) {
      perror("fail to wait");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11ad <main+0x3d>
lea    0xe6d(%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    11de <main+0x6e>
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121b <main+0xab>
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    126e <main+0xfe>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    1256 <main+0xe6>
lea    0xdd7(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
jmp    129b <main+0x12b>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    1296 <main+0x126>
lea    0xdd5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    129b <main+0x12b>
jmp    12a0 <main+0x130>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x408,%rsp
lea    0xe82(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     1202 <main+0x92>
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11f9 <main+0x89>
lea    0xe8d(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11ce <main+0x5e>
cmp    $0xffffffff,%eax
je     120b <main+0x9b>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    11ef <main+0x7f>
lea    0xe90(%rip),%rdi
jmp    1212 <main+0xa2>
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11f9 <main+0x89>
lea    0xe65(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x408,%rsp
ret
lea    0xe1e(%rip),%rdi
jmp    1212 <main+0xa2>
lea    0xe02(%rip),%rdi
jmp    1212 <main+0xa2>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

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