#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 */
struct user_regs_struct {
  int orig_rax;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_GETREGS;
struct user_regs_struct *PTRACE_O_TRACESYSGOOD;
int /*<<< orphan*/ PTRACE_SETOPTIONS;
int /*<<< orphan*/ PTRACE_SYSCALL;
int /*<<< orphan*/ PTRACE_TRACEME;
int SIGTRAP;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int /*<<< orphan*/ __WALL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, ...);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t child = fork();
  if (child == -1) {
    printf("fork() error - %s", strerror(errno));
    exit(1);
  } else if (child == 0) {
    ptrace(PTRACE_TRACEME);
    execvp(argv[1], argv + 1);
  } else {
    bool first_stop = true;
    bool syscall_enter = false;
    while (1) {
      int wstatus;
      waitpid(child, &wstatus, __WALL);
      if (WIFEXITED(wstatus)) {
        break;
      } else if (WIFSTOPPED(wstatus)) {
        if (first_stop) {
          printf("Setting ptrace options\n");
          ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD);
          first_stop = false;
        }
        if (WSTOPSIG(wstatus) == (SIGTRAP | 0x80)) {
          if (syscall_enter) {
            struct user_regs_struct regs;
            ptrace(PTRACE_GETREGS, child, 0, &regs);
            printf("Syscall: %d\n", regs.orig_rax);
          }
          syscall_enter = !syscall_enter;
        }
        ptrace(PTRACE_SYSCALL, child, 0, 0);
      } else {
        printf("Something strange has happened...\n");
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f3 <main+0x53>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1227 <main+0x87>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    $0x0,%al
call   1050 <ptrace@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1070 <execvp@plt>
jmp    134e <main+0x1ae>
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rsi
call   1060 <waitpid@plt>
mov    -0x20(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1261 <main+0xc1>
jmp    1349 <main+0x1a9>
mov    -0x20(%rbp),%edi
call   1040 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1331 <main+0x191>
cmpl   $0x0,-0x18(%rbp)
je     12b1 <main+0x111>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    0x2d4a(%rip),%rax
mov    (%rax),%rcx
xor    %edx,%edx
mov    $0x0,%al
call   1050 <ptrace@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%edi
call   10b0 <WSTOPSIG@plt>
mov    0x2cf0(%rip),%rcx
mov    (%rcx),%ecx
or     $0x80,%ecx
cmp    %ecx,%eax
jne    1315 <main+0x175>
cmpl   $0x0,-0x1c(%rbp)
je     1304 <main+0x164>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
lea    -0x28(%rbp),%rcx
mov    $0x0,%al
call   1050 <ptrace@plt>
mov    -0x28(%rbp),%esi
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
mov    $0x0,%al
call   1050 <ptrace@plt>
jmp    133f <main+0x19f>
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1344 <main+0x1a4>
jmp    1235 <main+0x95>
jmp    134e <main+0x1ae>
jmp    1353 <main+0x1b3>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12f9 <main+0x149>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     132a <main+0x17a>
mov    0x2dd6(%rip),%r12
mov    (%r12),%edx
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    1319 <main+0x169>
mov    $0x1,%r14d
lea    0xc(%rsp),%r15
mov    0x2d8a(%rip),%rbp
xor    %r13d,%r13d
jmp    124c <main+0x9c>
nopl   0x0(%rax,%rax,1)
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    1319 <main+0x169>
mov    0xc(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     1220 <main+0x70>
test   %r14d,%r14d
je     1289 <main+0xd9>
lea    0xddb(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%rcx
mov    %ebx,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1060 <ptrace@plt>
mov    0xc(%rsp),%edi
call   10c0 <WSTOPSIG@plt>
mov    0x2d17(%rip),%rcx
mov    (%rcx),%ecx
mov    $0x80,%edx
or     %edx,%ecx
cmp    %ecx,%eax
jne    12e1 <main+0x131>
test   %r13d,%r13d
je     12d6 <main+0x126>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
lea    0x10(%rsp),%rcx
xor    %eax,%eax
call   1060 <ptrace@plt>
mov    0x10(%rsp),%esi
lea    0xd43(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
test   %r13d,%r13d
sete   %al
mov    %eax,%r13d
mov    0x0(%rbp),%edi
xor    %r14d,%r14d
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1060 <ptrace@plt>
jmp    122c <main+0x7c>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1060 <ptrace@plt>
mov    0x8(%rbp),%rdi
add    $0x8,%rbp
mov    %rbp,%rsi
call   1080 <execvp@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xcc1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@.str = [18 x i8] c"fork() error - %s\00"
@errno = global i32 0
@PTRACE_TRACEME = global i32 0
@__WALL = global i32 0
@.str.1 = [24 x i8] c"Setting ptrace options\0A\00"
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global %struct.user_regs_struct* null
@SIGTRAP = global i32 0
@PTRACE_GETREGS = global i32 0
@.str.2 = [13 x i8] c"Syscall: %d\0A\00"
@PTRACE_SYSCALL = global i32 0
@.str.3 = [35 x i8] c"Something strange has happened...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.user_regs_struct
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %19
%23 = load i32, i32* @PTRACE_TRACEME
%24 = call i32 (i32, ...) @ptrace(i32 %23)
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = call i32 @execvp(i8* %27, i8** %29)
br label %83
31: ; preds = %19
store i32 1, i32* %7
store i32 0, i32* %8
br label %32
32: ; preds = %31, %81
%33 = load i32, i32* %6
%34 = load i32, i32* @__WALL
%35 = call i32 @waitpid(i32 %33, i32* %9, i32 %34)
%36 = load i32, i32* %9
%37 = call i64 @WIFEXITED(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
br label %82
40: ; preds = %32
%41 = load i32, i32* %9
%42 = call i64 @WIFSTOPPED(i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %78
44: ; preds = %40
%45 = load i32, i32* %7
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* @PTRACE_SETOPTIONS
%50 = load i32, i32* %6
%51 = load %struct.user_regs_struct*, %struct.user_regs_struct** @PTRACE_O_TRACESYSGOOD
%52 = call i32 (i32, ...) @ptrace(i32 %49, i32 %50, i32 0, %struct.user_regs_struct* %51)
store i32 0, i32* %7
br label %53
53: ; preds = %47, %44
%54 = load i32, i32* %9
%55 = call i32 @WSTOPSIG(i32 %54)
%56 = load i32, i32* @SIGTRAP
%57 = or i32 %56, 128
%58 = icmp eq i32 %55, %57
br i1 %58, label %59, label %74
59: ; preds = %53
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %59
%63 = load i32, i32* @PTRACE_GETREGS
%64 = load i32, i32* %6
%65 = call i32 (i32, ...) @ptrace(i32 %63, i32 %64, i32 0, %struct.user_regs_struct* %10)
%66 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %10, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %67)
br label %69
69: ; preds = %62, %59
%70 = load i32, i32* %8
%71 = icmp ne i32 %70, 0
%72 = xor i1 %71, true
%73 = zext i1 %72 to i32
store i32 %73, i32* %8
br label %74
74: ; preds = %69, %53
%75 = load i32, i32* @PTRACE_SYSCALL
%76 = load i32, i32* %6
%77 = call i32 (i32, ...) @ptrace(i32 %75, i32 %76, i32 0, i32 0)
br label %80
78: ; preds = %40
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %80
80: ; preds = %78, %74
br label %81
81: ; preds = %80
br label %32
82: ; preds = %39
br label %83
83: ; preds = %82, %22
br label %84
84: ; preds = %83
ret i32 0
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32 }
@.str = [18 x i8] c"fork() error - %s\00"
@errno = global i32 0
@PTRACE_TRACEME = global i32 0
@__WALL = global i32 0
@PTRACE_SETOPTIONS = global i32 0
@PTRACE_O_TRACESYSGOOD = global %struct.user_regs_struct* null
@SIGTRAP = global i32 0
@PTRACE_GETREGS = global i32 0
@.str.2 = [13 x i8] c"Syscall: %d\0A\00"
@PTRACE_SYSCALL = global i32 0
@str = [34 x i8] c"Something strange has happened...\00"
@str.4 = [23 x i8] c"Setting ptrace options\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.user_regs_struct
%5 = tail call i32 (...) @fork()
switch i32 %5, label %6 [
i32 -1, label %16
i32 0, label %21
]
6: ; preds = %2
%7 = bitcast i32* %3 to i8*
%8 = load i32, i32* @__WALL
%9 = call i32 @waitpid(i32 %5, i32* %3, i32 %8)
%10 = load i32, i32* %3
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %6
%14 = bitcast %struct.user_regs_struct* %4 to i8*
%15 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 0
br label %28
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @PTRACE_TRACEME
%23 = tail call i32 (i32, ...) @ptrace(i32 %22)
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = tail call i32 @execvp(i8* %25, i8** %24)
br label %70
27: ; preds = %62, %6
br label %70
28: ; preds = %13, %62
%29 = phi i32 [ 0, %13 ], [ %64, %62 ]
%30 = phi i32 [ 1, %13 ], [ %63, %62 ]
%31 = load i32, i32* %3
%32 = call i64 @WIFSTOPPED(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %60, label %34
34: ; preds = %28
%35 = icmp eq i32 %30, 0
br i1 %35, label %41, label %36
36: ; preds = %34
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0))
%38 = load i32, i32* @PTRACE_SETOPTIONS
%39 = load %struct.user_regs_struct*, %struct.user_regs_struct** @PTRACE_O_TRACESYSGOOD
%40 = call i32 (i32, ...) @ptrace(i32 %38, i32 %5, i32 0, %struct.user_regs_struct* %39)
br label %41
41: ; preds = %36, %34
%42 = load i32, i32* %3
%43 = call i32 @WSTOPSIG(i32 %42)
%44 = load i32, i32* @SIGTRAP
%45 = or i32 %44, 128
%46 = icmp eq i32 %43, %45
br i1 %46, label %47, label %56
47: ; preds = %41
%48 = icmp eq i32 %29, 0
br i1 %48, label %54, label %49
49: ; preds = %47
%50 = load i32, i32* @PTRACE_GETREGS
%51 = call i32 (i32, ...) @ptrace(i32 %50, i32 %5, i32 0, %struct.user_regs_struct* %4)
%52 = load i32, i32* %15
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %49, %47
%55 = zext i1 %48 to i32
br label %56
56: ; preds = %54, %41
%57 = phi i32 [ %55, %54 ], [ %29, %41 ]
%58 = load i32, i32* @PTRACE_SYSCALL
%59 = call i32 (i32, ...) @ptrace(i32 %58, i32 %5, i32 0, i32 0)
br label %62
60: ; preds = %28
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
%63 = phi i32 [ 0, %56 ], [ %30, %60 ]
%64 = phi i32 [ %57, %56 ], [ %29, %60 ]
%65 = load i32, i32* @__WALL
%66 = call i32 @waitpid(i32 %5, i32* %3, i32 %65)
%67 = load i32, i32* %3
%68 = call i64 @WIFEXITED(i32 %67)
%69 = icmp eq i64 %68, 0
br i1 %69, label %28, label %27
70: ; preds = %27, %21
ret i32 0
}