#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 /*<<< orphan*/ progname;

/* Variables and functions */
scalar_t__ EPERM;
int PATH_MAX;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int main(int argc, char **argv) {
  int p;
  char progname[PATH_MAX];
  snprintf(progname, sizeof(progname), "%s_2", argv[0]);
  p = fork();
  if (!p)
    _exit(0);
  if (p < 0 && errno != EPERM) {
    fprintf(stderr, "failed: unexpected fork() error: %s\n", strerror(errno));
    return 1;
  }
  if (p > 0) {
    fprintf(stderr, "failed: fork() succeeded\n");
    return 1;
  }
  execl(progname, progname, NULL);
  if (errno != EPERM) {
    fprintf(stderr, "failed: unexpected execl() error: %s\n", strerror(errno));
    return 1;
  }
  fprintf(stderr, "ok\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe4e(%rip),%rdx
call   1040 <snprintf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11d2 <main+0x72>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x14(%rbp)
jge    1238 <main+0xd8>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     1238 <main+0xd8>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12fd <main+0x19d>
cmpl   $0x0,-0x14(%rbp)
jle    126c <main+0x10c>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12fd <main+0x19d>
mov    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1070 <execl@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    0x2d33(%rip),%rcx
cmp    (%rcx),%rax
je     12d8 <main+0x178>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12fd <main+0x19d>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rsi),%rcx
lea    0xe6e(%rip),%rdx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <snprintf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1256 <main+0xf6>
js     11c6 <main+0x66>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1240 <main+0xe0>
mov    0x2e03(%rip),%r12
mov    (%r12),%rdi
mov    0x2de8(%rip),%r15
cmp    (%r15),%rdi
jne    1211 <main+0xb1>
xor    %r14d,%r14d
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <execl@plt>
mov    (%r12),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%ebx
cmp    (%r15),%rdi
jne    1228 <main+0xc8>
lea    0xe64(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1246 <main+0xe6>
mov    0x2dc0(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xddf(%rip),%rsi
jmp    1234 <main+0xd4>
call   1060 <strerror@plt>
lea    0xe10(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"%s_2\00"
@errno = global i64 0
@EPERM = global i64 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"failed: unexpected fork() error: %s\0A\00"
@.str.2 = [26 x i8] c"failed: fork() succeeded\0A\00"
@.str.3 = [38 x i8] c"failed: unexpected execl() error: %s\0A\00"
@.str.4 = [4 x i8] c"ok\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = trunc i64 %11 to i32
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 (...) @fork()
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %2
%23 = call i32 @_exit(i32 0)
unreachable
24: ; preds = %2
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EPERM
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = load i64, i64* @errno
%34 = call i8* @strerror(i64 %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
store i32 1, i32* %9
br label %55
36: ; preds = %27, %24
%37 = load i32, i32* %6
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %9
br label %55
42: ; preds = %36
%43 = call i32 @execl(i8* %13, i8* %13, i32* null)
%44 = load i64, i64* @errno
%45 = load i64, i64* @EPERM
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = load i64, i64* @errno
%50 = call i8* @strerror(i64 %49)
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %50)
store i32 1, i32* %3
store i32 1, i32* %9
br label %55
52: ; preds = %42
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
store i32 1, i32* %9
br label %55
55: ; preds = %52, %47, %39, %31
%56 = load i8*, i8** %7
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"%s_2\00"
@errno = global i64 0
@EPERM = global i64 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"failed: unexpected fork() error: %s\0A\00"
@.str.2 = [26 x i8] c"failed: fork() succeeded\0A\00"
@.str.3 = [38 x i8] c"failed: unexpected execl() error: %s\0A\00"
@.str.4 = [4 x i8] c"ok\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i8*, i8** %1
%7 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @_exit(i32 0)
unreachable
12: ; preds = %2
%13 = icmp slt i32 %8, 0
br i1 %13, label %14, label %22
14: ; preds = %12
%15 = load i64, i64* @errno
%16 = load i64, i64* @EPERM
%17 = icmp eq i64 %15, %16
br i1 %17, label %25, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i8* @strerror(i64 %15)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %36
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %36
25: ; preds = %14
%26 = call i32 @execl(i8* %5, i8* %5, i32* null)
%27 = load i64, i64* @errno
%28 = load i64, i64* @EPERM
%29 = icmp eq i64 %27, %28
%30 = load i32, i32* @stderr
br i1 %29, label %34, label %31
31: ; preds = %25
%32 = call i8* @strerror(i64 %27)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %36
34: ; preds = %25
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31, %22, %18
%37 = phi i32 [ 1, %18 ], [ 1, %22 ], [ 1, %31 ], [ 0, %34 ]
ret i32 %37
}