#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int system(char *);
int main(void) {
  char buf[1024];
  if (getcwd(buf, 1024) == NULL) {
    perror("fail to get pwd");
    exit(1);
  }
  printf("%s\n", buf);
  if (system("cd ./test-dir") == -1) {
    perror("fail to exec");
    exit(1);
  }
  if (getcwd(buf, 1024) == NULL) {
    perror("fail to get pwd");
    exit(1);
  }
  printf("%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    1193 <main+0x43>
lea    0xe7c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xe6f(%rip),%rdi
call   1040 <printf@plt>
lea    0xe67(%rip),%rdi
call   1030 <system@plt>
cmp    $0xffffffff,%eax
jne    11d1 <main+0x81>
lea    0xe60(%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    1202 <main+0xb2>
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xe00(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x408,%rsp
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11b4 <main+0x64>
lea    0xea0(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
lea    0xe95(%rip),%rdi
call   1030 <system@plt>
cmp    $0xffffffff,%eax
je     11bd <main+0x6d>
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11b4 <main+0x64>
lea    0xe6e(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x408,%rsp
ret
lea    0xe45(%rip),%rdi
jmp    11c4 <main+0x74>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"fail to get pwd\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [14 x i8] c"cd ./test-dir\00"
@.str.3 = [13 x i8] c"fail to exec\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
store i32 0, i32* %1
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32* @getcwd(i8* %3, i32 1024)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%11 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%19 = call i32* @getcwd(i8* %18, i32 1024)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %25)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"fail to get pwd\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [14 x i8] c"cd ./test-dir\00"
@.str.3 = [13 x i8] c"fail to exec\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 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%10 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = call i32* @getcwd(i8* %2, i32 1024)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %2)
ret i32 0
}