#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 void *uint16_t;
struct winsize {
  void *ws_row;
  void *ws_col;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCSWINSZ;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct winsize *);
int /*<<< orphan*/ perror(char *);
int set_terminal_size(int fd, uint16_t width, uint16_t height) {
  struct winsize ws;
  ws.ws_col = width;
  ws.ws_row = height;
  if (ioctl(fd, TIOCSWINSZ, &ws) != 0) {
    perror("TIOCSWINSZ");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_terminal_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     117a <set_terminal_size+0x5a>
lea    0xe97(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1181 <set_terminal_size+0x61>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_terminal_size>:
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdx,0x8(%rsp)
mov    0x2eb3(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1150 <set_terminal_size+0x30>
add    $0x18,%rsp
ret
lea    0xea9(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.winsize = type { i8*, i8* }
@TIOCSWINSZ = global i32 0
@.str = [11 x i8] c"TIOCSWINSZ\00"
define i32 @set_terminal_size(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.winsize
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %6
%10 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 1
store i8* %9, i8** %10
%11 = load i8*, i8** %7
%12 = getelementptr inbounds %struct.winsize, %struct.winsize* %8, i32 0, i32 0
store i8* %11, i8** %12
%13 = load i32, i32* %5
%14 = load i32, i32* @TIOCSWINSZ
%15 = call i64 @ioctl(i32 %13, i32 %14, %struct.winsize* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %20
19: ; preds = %3
store i32 0, i32* %4
br label %20
20: ; preds = %19, %17
%21 = load i32, i32* %4
ret i32 %21
}

LLVM IR (-O2):
%struct.winsize = type { i8*, i8* }
@TIOCSWINSZ = global i32 0
@.str = [11 x i8] c"TIOCSWINSZ\00"
define i32 @set_terminal_size(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.winsize
%5 = bitcast %struct.winsize* %4 to i8*
%6 = getelementptr inbounds %struct.winsize, %struct.winsize* %4, i64 0, i32 1
store i8* %1, i8** %6
%7 = getelementptr inbounds %struct.winsize, %struct.winsize* %4, i64 0, i32 0
store i8* %2, i8** %7
%8 = load i32, i32* @TIOCSWINSZ
%9 = call i64 @ioctl(i32 %0, i32 %8, %struct.winsize* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %3, %11
%14 = phi i32 [ -1, %11 ], [ 0, %3 ]
ret i32 %14
}