#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 */
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ errorf(char *, ...);
scalar_t__ execlp(char *, char *, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
scalar_t__ wait(int *);
int fstty(int fd, char *stty_args) {
  int child_status;
  int pid;
  char *stty_cmd;
  stty_cmd = malloc(strlen(stty_args) + 1 + strlen("stty "));
  if (!stty_cmd)
    errorf("Couldn't malloc for stty_cmd: %s\n", strerror(errno));
  strcpy(stty_cmd, "stty ");
  strcat(stty_cmd, stty_args);
  if ((pid = fork()) == 0) {
    if (dup2(fd, STDIN_FILENO) < 0)
      errorf("Couldn't dup2(%d,STDIN_FILENO=%d): %s\n", fd, STDIN_FILENO,
             strerror(errno));
    if (execlp("sh", "sh", "-c", stty_cmd, (0)) < 0)
      errorf("Couldn't exec stty command: %s\n", strerror(errno));
    exit(-1);
  } else if (pid == -1) {
    errorf("Couldn't fork: %s\n", strerror(errno));
  }
  free(stty_cmd);
  if (wait(&child_status) <= 0)
    errorf("Error waiting for forked stty process: '%s'\n", strerror(errno));
  if (!(WIFEXITED(child_status) && WEXITSTATUS(child_status) == 0))
    errorf("stty %s failed\n", stty_args);
  return 0;
}


x86 assembly (clang, -O0):
<fstty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
lea    0xdf9(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1246 <fstty+0x66>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1070 <errorf@plt>
mov    -0x20(%rbp),%rdi
lea    0xdaf(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcat@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    1318 <fstty+0x138>
mov    -0x4(%rbp),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
cmp    $0x0,%rax
jge    12c8 <fstty+0xe8>
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1070 <errorf@plt>
mov    -0x20(%rbp),%rcx
lea    0xd7c(%rip),%rsi
lea    0xd78(%rip),%rdx
xor    %r8d,%r8d
mov    %rsi,%rdi
call   10d0 <execlp@plt>
cmp    $0x0,%rax
jge    130e <fstty+0x12e>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1070 <errorf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1341 <fstty+0x161>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1070 <errorf@plt>
jmp    1346 <fstty+0x166>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    -0x14(%rbp),%rdi
call   10f0 <wait@plt>
cmp    $0x0,%rax
jg     1381 <fstty+0x1a1>
mov    0x2c77(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1070 <errorf@plt>
mov    -0x14(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     13a5 <fstty+0x1c5>
mov    -0x14(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     13b7 <fstty+0x1d7>
mov    -0x10(%rbp),%rsi
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1070 <errorf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fstty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsi,%rdi
call   1050 <strlen@plt>
lea    0x6(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1225 <fstty+0x45>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xdeb(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <errorf@plt>
lea    0xdd4(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     12de <fstty+0xfe>
test   %eax,%eax
jne    12fd <fstty+0x11d>
mov    0x2d7a(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1060 <dup2@plt>
test   %rax,%rax
jns    1294 <fstty+0xb4>
mov    (%r14),%r14d
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xda3(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <errorf@plt>
lea    0xdb4(%rip),%rdi
lea    0xdb0(%rip),%rdx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   10d0 <execlp@plt>
test   %rax,%rax
jns    12d4 <fstty+0xf4>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd8b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <errorf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd82(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <errorf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xc(%rsp),%rdi
call   10f0 <wait@plt>
test   %rax,%rax
jg     1333 <fstty+0x153>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd5f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <errorf@plt>
mov    0xc(%rsp),%edi
call   10b0 <WIFEXITED@plt>
test   %rax,%rax
je     134f <fstty+0x16f>
mov    0xc(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
test   %rax,%rax
je     1360 <fstty+0x180>
lea    0xd5f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <errorf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"stty \00"
@.str.1 = [34 x i8] c"Couldn't malloc for stty_cmd: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [39 x i8] c"Couldn't dup2(%d,STDIN_FILENO=%d): %s\0A\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [32 x i8] c"Couldn't exec stty command: %s\0A\00"
@.str.6 = [19 x i8] c"Couldn't fork: %s\0A\00"
@.str.7 = [45 x i8] c"Error waiting for forked stty process: '%s'\0A\00"
@.str.8 = [16 x i8] c"stty %s failed\0A\00"
define i32 @fstty(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
%10 = add nsw i64 %9, 1
%11 = call i64 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%12 = add nsw i64 %10, %11
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %16, %2
%21 = load i8*, i8** %7
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = load i8*, i8** %7
%24 = load i8*, i8** %4
%25 = call i32 @strcat(i8* %23, i8* %24)
%26 = call i32 (...) @fork()
store i32 %26, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %49
28: ; preds = %20
%29 = load i32, i32* %3
%30 = load i32, i32* @STDIN_FILENO
%31 = call i64 @dup2(i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i32, i32* %3
%35 = load i32, i32* @STDIN_FILENO
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 %35, i8* %37)
br label %39
39: ; preds = %33, %28
%40 = load i8*, i8** %7
%41 = call i64 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %40, i32 0)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %43, %39
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %20
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %52, %49
br label %57
57: ; preds = %56
%58 = load i8*, i8** %7
%59 = call i32 @free(i8* %58)
%60 = call i64 @wait(i32* %5)
%61 = icmp sle i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %57
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.7, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %62, %57
%67 = load i32, i32* %5
%68 = call i64 @WIFEXITED(i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i32, i32* %5
%72 = call i64 @WEXITSTATUS(i32 %71)
%73 = icmp eq i64 %72, 0
br i1 %73, label %77, label %74
74: ; preds = %70, %66
%75 = load i8*, i8** %4
%76 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %75)
br label %77
77: ; preds = %74, %70
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"stty \00"
@.str.1 = [34 x i8] c"Couldn't malloc for stty_cmd: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [39 x i8] c"Couldn't dup2(%d,STDIN_FILENO=%d): %s\0A\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [32 x i8] c"Couldn't exec stty command: %s\0A\00"
@.str.6 = [19 x i8] c"Couldn't fork: %s\0A\00"
@.str.7 = [45 x i8] c"Error waiting for forked stty process: '%s'\0A\00"
@.str.8 = [16 x i8] c"stty %s failed\0A\00"
define i32 @fstty(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = add nsw i64 %5, 6
%7 = tail call i8* @malloc(i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %9, %2
%14 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @strcat(i8* %7, i8* %1)
%16 = tail call i32 (...) @fork()
switch i32 %16, label %39 [
i32 0, label %17
i32 -1, label %35
]
17: ; preds = %13
%18 = load i32, i32* @STDIN_FILENO
%19 = tail call i64 @dup2(i32 %0, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %22, i8* %24)
br label %26
26: ; preds = %21, %17
%27 = tail call i64 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %7, i32 0)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %29, %26
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %13
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %13, %35
%40 = tail call i32 @free(i8* %7)
%41 = call i64 @wait(i32* %3)
%42 = icmp slt i64 %41, 1
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.7, i64 0, i64 0), i8* %45)
br label %47
47: ; preds = %43, %39
%48 = load i32, i32* %3
%49 = call i64 @WIFEXITED(i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = load i32, i32* %3
%53 = call i64 @WEXITSTATUS(i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %51, %47
%56 = call i32 (i8*, ...) @errorf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i8* %1)
br label %57
57: ; preds = %55, %51
ret i32 0
}