#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 */
scalar_t__ chdir(char *);
scalar_t__ creat(char *, int);
int /*<<< orphan*/ err_sys(char *, ...);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rmdir(char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  if (mkdir(argv[1], 0755) < 0)
    err_sys("%s: mkdir error", argv[1]);
  printf("mkdir %s\n", argv[1]);
  int ret = fork();
  if (ret < 0)
    err_sys("fork error");
  else if (ret == 0) {
    if (chdir(argv[1]) < 0)
      err_sys("chdir error");
    printf("chdir OK\n");
    sleep(5);
    if (creat("xyz.txt", 0755) < 0)
      printf("create xyz.txt failed, errno = %d\n", errno);
    else
      printf("create xyz.txt\n");
    sleep(3);
    printf("child quit\n");
    exit(0);
  } else {
    sleep(3);
  }
  if (rmdir(argv[1]) < 0)
    err_sys("%s: rmdir error", argv[1]);
  printf("rmdir %s\n", argv[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    11d8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <err_sys@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1215 <main+0x85>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <err_sys@plt>
jmp    12cf <main+0x13f>
cmpl   $0x0,-0x14(%rbp)
jne    12c0 <main+0x130>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jge    1244 <main+0xb4>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <err_sys@plt>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
lea    0xdd8(%rip),%rdi
mov    $0x1ed,%esi
call   1080 <creat@plt>
cmp    $0x0,%rax
jge    1293 <main+0x103>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12a1 <main+0x111>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
jmp    12cf <main+0x13f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <rmdir@plt>
cmp    $0x0,%rax
jge    12fc <main+0x16c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <err_sys@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
jns    11c9 <main+0x29>
mov    0x8(%rbx),%rsi
lea    0xe3e(%rip),%rdi
xor    %eax,%eax
call   1050 <err_sys@plt>
mov    0x8(%rbx),%rsi
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     11f4 <main+0x54>
je     1238 <main+0x98>
mov    $0x3,%edi
call   10b0 <sleep@plt>
jmp    1202 <main+0x62>
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1050 <err_sys@plt>
mov    0x8(%rbx),%rdi
call   1060 <rmdir@plt>
test   %rax,%rax
jns    1222 <main+0x82>
mov    0x8(%rbx),%rsi
lea    0xe39(%rip),%rdi
xor    %eax,%eax
call   1050 <err_sys@plt>
mov    0x8(%rbx),%rsi
lea    0xe37(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x8(%rbx),%rdi
call   1070 <chdir@plt>
test   %rax,%rax
jns    1254 <main+0xb4>
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1050 <err_sys@plt>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xe0d(%rip),%rdi
mov    $0x1ed,%esi
call   1090 <creat@plt>
test   %rax,%rax
jns    1299 <main+0xf9>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    0xda1(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    12a5 <main+0x105>
lea    0xdd7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
lea    0xdd0(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"%s: mkdir error\00"
@.str.1 = [10 x i8] c"mkdir %s\0A\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [12 x i8] c"chdir error\00"
@.str.4 = [10 x i8] c"chdir OK\0A\00"
@.str.5 = [8 x i8] c"xyz.txt\00"
@.str.6 = [35 x i8] c"create xyz.txt failed, errno = %d\0A\00"
@errno = global i32 0
@.str.7 = [16 x i8] c"create xyz.txt\0A\00"
@.str.8 = [12 x i8] c"child quit\0A\00"
@.str.9 = [16 x i8] c"%s: rmdir error\00"
@.str.10 = [10 x i8] c"rmdir %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i8**, i8*** %5
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = call i64 @mkdir(i8* %9, i32 493)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %12, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 (...) @fork()
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %55
27: ; preds = %17
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %52
30: ; preds = %27
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i64 @chdir(i8* %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %30
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @sleep(i32 5)
%41 = call i64 @creat(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 493)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* @errno
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %44)
br label %48
46: ; preds = %38
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = call i32 @sleep(i32 3)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %27
%53 = call i32 @sleep(i32 3)
br label %54
54: ; preds = %52
br label %55
55: ; preds = %54, %25
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = call i64 @rmdir(i8* %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %61, %55
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 1
%69 = load i8*, i8** %68
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %69)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"%s: mkdir error\00"
@.str.1 = [10 x i8] c"mkdir %s\0A\00"
@.str.2 = [11 x i8] c"fork error\00"
@.str.3 = [12 x i8] c"chdir error\00"
@.str.5 = [8 x i8] c"xyz.txt\00"
@.str.6 = [35 x i8] c"create xyz.txt failed, errno = %d\0A\00"
@errno = global i32 0
@.str.9 = [16 x i8] c"%s: rmdir error\00"
@.str.10 = [10 x i8] c"rmdir %s\0A\00"
@str = [9 x i8] c"chdir OK\00"
@str.11 = [15 x i8] c"create xyz.txt\00"
@str.12 = [11 x i8] c"child quit\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i64 @mkdir(i8* %4, i32 493)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %7, %2
%11 = load i8*, i8** %3
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = tail call i32 (...) @fork()
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %41
17: ; preds = %10
%18 = icmp eq i32 %13, 0
br i1 %18, label %19, label %39
19: ; preds = %17
%20 = load i8*, i8** %3
%21 = tail call i64 @chdir(i8* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 @sleep(i32 5)
%28 = tail call i64 @creat(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 493)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* @errno
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %31)
br label %35
33: ; preds = %25
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = tail call i32 @sleep(i32 3)
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.12, i64 0, i64 0))
%38 = tail call i32 @exit(i32 0)
unreachable
39: ; preds = %17
%40 = tail call i32 @sleep(i32 3)
br label %41
41: ; preds = %39, %15
%42 = load i8*, i8** %3
%43 = tail call i64 @rmdir(i8* %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i8*, i8** %3
%47 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %45, %41
%49 = load i8*, i8** %3
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %49)
ret i32 0
}