#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int execv(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ forkpty(int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int start_forkpty(int *fd, pid_t *cpid, char *debugger, char **args) {
  char *argv[] = {"sh", "-c", NULL, NULL};
  int i;
  int ret;
  char *debugger_cmd;
  int len;
  *cpid = forkpty(fd, NULL, NULL, NULL);
  if (*cpid == 0) {
    i = 0;
    while (args[i] != NULL) {
      i++;
    }
    i = 0;
    len = strlen(debugger);
    while (args[i] != 0) {
      len += strlen(args[i]) + 1;
      i++;
    }
    debugger_cmd = (char *)malloc(len + 1);
    if (debugger_cmd == NULL) {
      fprintf(stderr, "Could not create command");
      exit(EXIT_FAILURE);
    }
    sprintf(debugger_cmd, "%s", debugger);
    i = 0;
    while (args[i] != 0) {
      strcat(debugger_cmd, " ");
      strcat(debugger_cmd, args[i]);
      i++;
    }
    argv[2] = debugger_cmd;
    ret = execv("/bin/sh", argv);
    if (ret < 0) {
      fprintf(stderr, "Could not call execv: %m");
      exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
  } else if (*cpid < 0) {
    fprintf(stderr, "Could not fork: %m");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<start_forkpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2c41(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c3e(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2c3b(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c38(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1060 <forkpty@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1359 <start_forkpty+0x1d9>
movl   $0x0,-0x54(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x54(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1217 <start_forkpty+0x97>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11f6 <start_forkpty+0x76>
movl   $0x0,-0x54(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x54(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1265 <start_forkpty+0xe5>
mov    -0x28(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
add    -0x64(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    122a <start_forkpty+0xaa>
mov    -0x64(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    12a2 <start_forkpty+0x122>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd6b(%rip),%rsi
call   1080 <sprintf@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x54(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1303 <start_forkpty+0x183>
mov    -0x60(%rbp),%rdi
lea    0xd44(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x54(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <strcat@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12bd <start_forkpty+0x13d>
mov    -0x60(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x50(%rbp),%rsi
lea    0xd0b(%rip),%rdi
call   10a0 <execv@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    134b <start_forkpty+0x1cb>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
lea    0xcf1(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jge    1388 <start_forkpty+0x208>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
lea    0xccb(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1394 <start_forkpty+0x214>
jmp    138d <start_forkpty+0x20d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_forkpty>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
movaps 0x2c55(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0x2c39(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %ebx,%ebx
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <forkpty@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     11eb <start_forkpty+0x6b>
jns    11dd <start_forkpty+0x5d>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    (%r15),%rdi
test   %rdi,%rdi
je     1228 <start_forkpty+0xa8>
lea    0x8(%r15),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1040 <strlen@plt>
add    %eax,%r12d
add    $0x1,%r12d
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    1210 <start_forkpty+0x90>
add    $0x1,%r12d
mov    %r12d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    124b <start_forkpty+0xcb>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
jmp    12b9 <start_forkpty+0x139>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
cmpq   $0x0,(%r15)
je     1291 <start_forkpty+0x111>
add    $0x8,%r15
lea    0xdb2(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    -0x8(%r15),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
cmpq   $0x0,(%r15)
lea    0x8(%r15),%r15
jne    1270 <start_forkpty+0xf0>
mov    %rbx,0x10(%rsp)
lea    0xd81(%rip),%rdi
mov    %rsp,%rsi
call   10a0 <execv@plt>
test   %eax,%eax
jns    12cc <start_forkpty+0x14c>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@__const.start_forkpty.argv = [4 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* null, i8* null]
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not create command\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [25 x i8] c"Could not call execv: %m\00"
@EXIT_SUCCESS = global i32 0
@.str.7 = [19 x i8] c"Could not fork: %m\00"
define i32 @start_forkpty(i32* %0, i64* %1, i8* %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca [4 x i8*]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i32* %0, i32** %6
store i64* %1, i64** %7
store i8* %2, i8** %8
store i8** %3, i8*** %9
%15 = bitcast [4 x i8*]* %10 to i8*
%16 = load i32*, i32** %6
%17 = call i64 @forkpty(i32* %16, i32* null, i32* null, i32* null)
%18 = load i64*, i64** %7
store i64 %17, i64* %18
%19 = load i64*, i64** %7
%20 = load i64, i64* %19
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %105
22: ; preds = %4
store i32 0, i32* %11
br label %23
23: ; preds = %30, %22
%24 = load i8**, i8*** %9
%25 = load i32, i32* %11
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i32, i32* %11
%32 = add nsw i32 %31, 1
store i32 %32, i32* %11
br label %23
33: ; preds = %23
store i32 0, i32* %11
%34 = load i8*, i8** %8
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %14
br label %36
36: ; preds = %43, %33
%37 = load i8**, i8*** %9
%38 = load i32, i32* %11
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %37, i64 %39
%41 = load i8*, i8** %40
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %55
43: ; preds = %36
%44 = load i8**, i8*** %9
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
%48 = load i8*, i8** %47
%49 = call i32 @strlen(i8* %48)
%50 = add nsw i32 %49, 1
%51 = load i32, i32* %14
%52 = add nsw i32 %51, %50
store i32 %52, i32* %14
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
br label %36
55: ; preds = %36
%56 = load i32, i32* %14
%57 = add nsw i32 %56, 1
%58 = call i64 @malloc(i32 %57)
%59 = inttoptr i64 %58 to i8*
store i8* %59, i8** %13
%60 = load i8*, i8** %13
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %67
62: ; preds = %55
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %55
%68 = load i8*, i8** %13
%69 = load i8*, i8** %8
%70 = call i32 @sprintf(i8* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %69)
store i32 0, i32* %11
br label %71
71: ; preds = %78, %67
%72 = load i8**, i8*** %9
%73 = load i32, i32* %11
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8*, i8** %72, i64 %74
%76 = load i8*, i8** %75
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %90
78: ; preds = %71
%79 = load i8*, i8** %13
%80 = call i32 @strcat(i8* %79, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%81 = load i8*, i8** %13
%82 = load i8**, i8*** %9
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8*, i8** %82, i64 %84
%86 = load i8*, i8** %85
%87 = call i32 @strcat(i8* %81, i8* %86)
%88 = load i32, i32* %11
%89 = add nsw i32 %88, 1
store i32 %89, i32* %11
br label %71
90: ; preds = %71
%91 = load i8*, i8** %13
%92 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 2
store i8* %91, i8** %92
%93 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
%94 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %93)
store i32 %94, i32* %12
%95 = load i32, i32* %12
%96 = icmp slt i32 %95, 0
br i1 %96, label %97, label %102
97: ; preds = %90
%98 = load i32, i32* @stderr
%99 = call i32 @fprintf(i32 %98, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%100 = load i32, i32* @EXIT_FAILURE
%101 = call i32 @exit(i32 %100)
unreachable
102: ; preds = %90
%103 = load i32, i32* @EXIT_SUCCESS
%104 = call i32 @exit(i32 %103)
unreachable
105: ; preds = %4
%106 = load i64*, i64** %7
%107 = load i64, i64* %106
%108 = icmp slt i64 %107, 0
br i1 %108, label %109, label %112
109: ; preds = %105
%110 = load i32, i32* @stderr
%111 = call i32 @fprintf(i32 %110, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %5
br label %114
112: ; preds = %105
br label %113
113: ; preds = %112
store i32 0, i32* %5
br label %114
114: ; preds = %113, %109
%115 = load i32, i32* %5
ret i32 %115
}

LLVM IR (-O2):
@.str = [3 x i8] c"sh\00"
@.str.1 = [3 x i8] c"-c\00"
@__const.start_forkpty.argv = [4 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* null, i8* null]
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not create command\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [2 x i8] c" \00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [25 x i8] c"Could not call execv: %m\00"
@EXIT_SUCCESS = global i32 0
@.str.7 = [19 x i8] c"Could not fork: %m\00"
define i32 @start_forkpty(i32* %0, i64* nocapture writeonly %1, i8* %2, i8** nocapture %3) {
%5 = alloca [4 x i8*]
%6 = bitcast [4 x i8*]* %5 to i8*
%7 = tail call i64 @forkpty(i32* %0, i32* null, i32* null, i32* null)
store i64 %7, i64* %1
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %62
9: ; preds = %4
%10 = tail call i32 @strlen(i8* %2)
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %24, label %13
13: ; preds = %9, %13
%14 = phi i64 [ %20, %13 ], [ 0, %9 ]
%15 = phi i8* [ %22, %13 ], [ %11, %9 ]
%16 = phi i32 [ %19, %13 ], [ %10, %9 ]
%17 = tail call i32 @strlen(i8* %15)
%18 = add i32 %16, 1
%19 = add i32 %18, %17
%20 = add nuw i64 %14, 1
%21 = getelementptr inbounds i8*, i8** %3, i64 %20
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %13
24: ; preds = %13, %9
%25 = phi i32 [ %10, %9 ], [ %19, %13 ]
%26 = add nsw i32 %25, 1
%27 = tail call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %24
%36 = tail call i8* @strcpy(i8* %28, i8* dereferenceable(1) %2)
%37 = load i8*, i8** %3
%38 = icmp eq i8* %37, null
br i1 %38, label %49, label %39
39: ; preds = %35, %39
%40 = phi i64 [ %45, %39 ], [ 0, %35 ]
%41 = phi i8** [ %46, %39 ], [ %3, %35 ]
%42 = tail call i32 @strcat(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%43 = load i8*, i8** %41
%44 = tail call i32 @strcat(i8* %28, i8* %43)
%45 = add nuw i64 %40, 1
%46 = getelementptr inbounds i8*, i8** %3, i64 %45
%47 = load i8*, i8** %46
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %39
49: ; preds = %39, %35
%50 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 2
store i8* %28, i8** %50
%51 = getelementptr inbounds [4 x i8*], [4 x i8*]* %5, i64 0, i64 0
%52 = call i32 @execv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %51)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %49
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %49
%60 = load i32, i32* @EXIT_SUCCESS
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %4
%63 = icmp slt i64 %7, 0
br i1 %63, label %64, label %67
64: ; preds = %62
%65 = load i32, i32* @stderr
%66 = tail call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %67
67: ; preds = %62, %64
%68 = phi i32 [ -1, %64 ], [ 0, %62 ]
ret i32 %68
}