#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 stat {
  int st_rdev;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fstat(int, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char buf[255];
  ssize_t bytes;
  int fd, ret;
  struct stat sb;
  if (argc != 2) {
    fprintf(stderr, "usage:  %s fd\n", argv[0]);
    exit(-1);
  }
  fd = atoi(argv[1]);
  ret = fstat(fd, &sb);
  if (ret < 0) {
    perror("fstat");
    exit(2);
  }
  if (sb.st_rdev == 0x103) {
    fprintf(stderr, "%s:  null device\n", argv[0]);
    exit(1);
  }
  strcpy(buf, "hello world!\n");
  bytes = write(fd, buf, strlen(buf));
  if (bytes < 0) {
    perror("write");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c9 <main+0x49>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%edi
lea    -0x128(%rbp),%rsi
call   10a0 <fstat@plt>
mov    %eax,-0x120(%rbp)
cmpl   $0x0,-0x120(%rbp)
jge    1217 <main+0x97>
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
cmpl   $0x103,-0x128(%rbp)
jne    124d <main+0xcd>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x110(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x11c(%rbp),%eax
mov    %eax,-0x134(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jge    12c5 <main+0x145>
lea    0xd7f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11c2 <main+0x42>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %eax,%eax
jns    11e4 <main+0x64>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe2b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpl   $0x103,0x8(%rsp)
jne    1210 <main+0x90>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe14(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe10(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    1253 <main+0xd3>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [15 x i8] c"usage: %s fd\0A\00"
@.str.1 = [6 x i8] c"fstat\00"
@.str.2 = [18 x i8] c"%s: null device\0A\00"
@.str.3 = [14 x i8] c"hello world!\0A\00"
@.str.4 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [255 x i8]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = call i32 @fstat(i32 %25, %struct.stat* %10)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %20
%33 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, 259
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 0
%40 = load i8*, i8** %39
%41 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%45 = call i32 @strcpy(i8* %44, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %8
%47 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%48 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
%50 = call i64 @write(i32 %46, i8* %47, i32 %49)
store i64 %50, i64* %7
%51 = load i64, i64* %7
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %43
%57 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [15 x i8] c"usage: %s fd\0A\00"
@.str.1 = [6 x i8] c"fstat\00"
@.str.2 = [18 x i8] c"%s: null device\0A\00"
@.str.3 = [14 x i8] c"hello world!\0A\00"
@.str.4 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [255 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = call i32 @fstat(i32 %16, %struct.stat* %4)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %13
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 259
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %1
%29 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %22
%32 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @strlen(i8* %5)
%34 = call i64 @write(i32 %16, i8* %5, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = call i32 @exit(i32 0)
unreachable
}