#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 atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int open(char *, int);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ stderr;
scalar_t__ writefd(int, char *, int, int);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    fprintf(stderr, "usage: openfile sockfd pathname flags\n");
    exit(1);
  }
  int fd;
  fd = open(argv[2], atoi(argv[3]));
  if (fd == -1) {
    fprintf(stderr, "open: %m\n");
    exit(1);
  }
  printf("pid %d fd = %d\n", getpid(), fd);
  if (writefd(atoi(argv[1]), "", 1, fd) < 0) {
    fprintf(stderr, "writefd error\n");
    exit(1);
  }
  close(fd);
  close(atoi(argv[1]));
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11bf <main+0x3f>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120f <main+0x8f>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%ecx
lea    0xe00(%rip),%rsi
mov    $0x1,%edx
call   10a0 <writefd@plt>
cmp    $0x0,%rax
jge    1273 <main+0xf3>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x3,%edi
jg     119a <main+0x1a>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
jmp    1217 <main+0x97>
mov    %rsi,%rbx
mov    0x10(%rsi),%rbp
mov    0x18(%rsi),%rdi
call   1080 <atoi@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11cb <main+0x4b>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
jmp    1217 <main+0x97>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe56(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
lea    0xe4c(%rip),%rsi
mov    %eax,%edi
mov    $0x1,%edx
mov    %ebp,%ecx
call   10a0 <writefd@plt>
test   %rax,%rax
jns    1226 <main+0xa6>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"usage: openfile sockfd pathname flags\0A\00"
@.str.1 = [10 x i8] c"open: %m\0A\00"
@.str.2 = [16 x i8] c"pid %d fd = %d\0A\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [15 x i8] c"writefd error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 4
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 2
%16 = load i8*, i8** %15
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 3
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
%21 = call i32 @open(i8* %16, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %13
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %13
%29 = call i32 (...) @getpid()
%30 = load i32, i32* %6
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
%36 = load i32, i32* %6
%37 = call i64 @writefd(i32 %35, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %28
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %28
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @atoi(i8* %48)
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"usage: openfile sockfd pathname flags\0A\00"
@.str.1 = [10 x i8] c"open: %m\0A\00"
@.str.2 = [16 x i8] c"pid %d fd = %d\0A\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [15 x i8] c"writefd error\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 3
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = tail call i32 @open(i8* %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %14)
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = tail call i32 @atoi(i8* %24)
%26 = tail call i64 @writefd(i32 %25, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %14)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %20
%33 = tail call i32 @close(i32 %14)
%34 = load i8*, i8** %23
%35 = tail call i32 @atoi(i8* %34)
%36 = tail call i32 @close(i32 %35)
%37 = tail call i32 @exit(i32 0)
unreachable
}