#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 winsize {
  int ws_col;
  int ws_row;
  int ws_xpixel;
  int ws_ypixel;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ forkpty(int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   struct winsize *);
int /*<<< orphan*/ perror(char *);
int fork_pty_child(int *master_fd_pointer, char **argv) {
  struct winsize win = {
      .ws_col = 80,
      .ws_row = 24,
      .ws_xpixel = 480,
      .ws_ypixel = 192,
  };
  pid_t child_pid = forkpty(master_fd_pointer, NULL, NULL, &win);
  if (0 != child_pid)
    return child_pid;
  execvp(argv[1], argv + 1);
  perror("exec failed");
  exit(EX_OSERR);
  return -1;
}


x86 assembly (clang, -O0):
<fork_pty_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xea9(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xea6(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
mov    %rdx,%rsi
call   1030 <forkpty@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
je     1194 <fork_pty_child+0x54>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1050 <execvp@plt>
lea    0xe60(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<fork_pty_child>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movups 0xeb1(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1030 <forkpty@plt>
test   %rax,%rax
je     116a <fork_pty_child+0x2a>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1050 <execvp@plt>
lea    0xe8f(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32, i32, i32, i32 }
@__const.fork_pty_child.win = %struct.winsize { i32 80, i32 24, i32 480, i32 192 }
@.str = [12 x i8] c"exec failed\00"
@EX_OSERR = global i32 0
define i32 @fork_pty_child(i32* %0, i8** %1) {
%3 = alloca i32*
%4 = alloca i8**
%5 = alloca %struct.winsize
%6 = alloca i64
store i32* %0, i32** %3
store i8** %1, i8*** %4
%7 = bitcast %struct.winsize* %5 to i8*
%8 = load i32*, i32** %3
%9 = call i64 @forkpty(i32* %8, i32* null, i32* null, %struct.winsize* %5)
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp ne i64 0, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i64, i64* %6
%14 = trunc i64 %13 to i32
ret i32 %14
15: ; preds = %2
%16 = load i8**, i8*** %4
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = call i32 @execvp(i8* %18, i8** %20)
%22 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EX_OSERR
%24 = call i32 @exit(i32 %23)
unreachable
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32, i32, i32 }
@__const.fork_pty_child.win = %struct.winsize { i32 80, i32 24, i32 480, i32 192 }
@.str = [12 x i8] c"exec failed\00"
@EX_OSERR = global i32 0
define i32 @fork_pty_child(i32* %0, i8** %1) {
%3 = alloca %struct.winsize
%4 = bitcast %struct.winsize* %3 to i8*
%5 = call i64 @forkpty(i32* %0, i32* null, i32* null, %struct.winsize* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = trunc i64 %5 to i32
ret i32 %8
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @execvp(i8* %11, i8** %10)
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EX_OSERR
%15 = call i32 @exit(i32 %14)
unreachable
}