#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 scalar_t__ off_t;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ftruncate(int, scalar_t__);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int sscanf(char *, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main(int argc, char *argv[]) {
  int fd;
  long long scan_offset = 0;
  off_t offset = 0;
  int errsv;
  int rv;
  if (argc <= 1) {
    fprintf(stderr, "usage: rewindfd fd [offset]\n");
    exit(1);
  }
  if (argc > 1) {
    if (sscanf(argv[1], "%d", &fd) == EOF) {
      errsv = errno;
      fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
      exit(1);
    }
  }
  if (argc > 2) {
    rv = sscanf(argv[2], "%lld", &scan_offset);
    if (rv == EOF) {
      errsv = errno;
      fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
      exit(1);
    }
    offset = (off_t)scan_offset;
  }
  if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
    errsv = errno;
    fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
    exit(2);
  }
  if (ftruncate(fd, offset) == (off_t)-1) {
    errsv = errno;
    fprintf(stderr, "%s: %s\n", argv[0], strerror(errsv));
    exit(2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11b1 <main+0x51>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1236 <main+0xd6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe53(%rip),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%eax
jne    1231 <main+0xd1>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1236 <main+0xd6>
cmpl   $0x2,-0x8(%rbp)
jle    12c4 <main+0x164>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdd9(%rip),%rsi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%eax
jne    12bc <main+0x15c>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    0x2cf6(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    1330 <main+0x1d0>
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2ce2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd01(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1030 <ftruncate@plt>
cmp    $0xffffffffffffffff,%rax
jne    1393 <main+0x233>
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xc9e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
cmp    $0x1,%edi
jle    127e <main+0x11e>
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
lea    0xe92(%rip),%rsi
lea    0x4(%rsp),%rdx
xor    %eax,%eax
call   1060 <sscanf@plt>
mov    0x2e4a(%rip),%r15
cmp    (%r15),%eax
je     1244 <main+0xe4>
cmp    $0x3,%ebx
jb     11cf <main+0x6f>
mov    0x10(%r14),%rdi
lea    0xe71(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1060 <sscanf@plt>
cmp    (%r15),%eax
je     1244 <main+0xe4>
mov    0x8(%rsp),%rbx
jmp    11d1 <main+0x71>
xor    %ebx,%ebx
mov    0x4(%rsp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     120a <main+0xaa>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1030 <ftruncate@plt>
cmp    $0xffffffffffffffff,%rax
je     120a <main+0xaa>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
call   1080 <strerror@plt>
lea    0xdf5(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
call   1080 <strerror@plt>
lea    0xdbb(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"usage: rewindfd fd [offset]\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i32 0
@errno = global i32 0
@.str.2 = [8 x i8] c"%s: %s\0A\00"
@.str.3 = [5 x i8] c"%lld\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %7
store i64 0, i64* %8
%11 = load i32, i32* %4
%12 = icmp sle i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 1
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i8*, ...) @sscanf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %20
%28 = load i32, i32* @errno
store i32 %28, i32* %9
%29 = load i32, i32* @stderr
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = load i32, i32* %9
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %32, i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %20
br label %38
38: ; preds = %37, %17
%39 = load i32, i32* %4
%40 = icmp sgt i32 %39, 2
br i1 %40, label %41, label %61
41: ; preds = %38
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = call i32 (i8*, i8*, ...) @sscanf(i8* %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64* %7)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = load i32, i32* @EOF
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %59
49: ; preds = %41
%50 = load i32, i32* @errno
store i32 %50, i32* %9
%51 = load i32, i32* @stderr
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 0
%54 = load i8*, i8** %53
%55 = load i32, i32* %9
%56 = call i8* @strerror(i32 %55)
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %54, i8* %56)
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %41
%60 = load i64, i64* %7
store i64 %60, i64* %8
br label %61
61: ; preds = %59, %38
%62 = load i32, i32* %6
%63 = load i64, i64* %8
%64 = load i32, i32* @SEEK_SET
%65 = call i64 @lseek(i32 %62, i64 %63, i32 %64)
%66 = icmp eq i64 %65, -1
br i1 %66, label %67, label %77
67: ; preds = %61
%68 = load i32, i32* @errno
store i32 %68, i32* %9
%69 = load i32, i32* @stderr
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 0
%72 = load i8*, i8** %71
%73 = load i32, i32* %9
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %72, i8* %74)
%76 = call i32 @exit(i32 2)
unreachable
77: ; preds = %61
%78 = load i32, i32* %6
%79 = load i64, i64* %8
%80 = call i64 @ftruncate(i32 %78, i64 %79)
%81 = icmp eq i64 %80, -1
br i1 %81, label %82, label %92
82: ; preds = %77
%83 = load i32, i32* @errno
store i32 %83, i32* %9
%84 = load i32, i32* @stderr
%85 = load i8**, i8*** %5
%86 = getelementptr inbounds i8*, i8** %85, i64 0
%87 = load i8*, i8** %86
%88 = load i32, i32* %9
%89 = call i8* @strerror(i32 %88)
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %87, i8* %89)
%91 = call i32 @exit(i32 2)
unreachable
92: ; preds = %77
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"usage: rewindfd fd [offset]\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i32 0
@errno = global i32 0
@.str.2 = [8 x i8] c"%s: %s\0A\00"
@.str.3 = [5 x i8] c"%lld\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i64
%5 = bitcast i32* %3 to i8*
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 (i8*, i8*, ...) @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %25
18: ; preds = %12
%19 = load i32, i32* @errno
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %1
%22 = call i8* @strerror(i32 %19)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = icmp ugt i32 %0, 2
br i1 %26, label %27, label %42
27: ; preds = %25
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = call i32 (i8*, i8*, ...) @sscanf(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64* %4)
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = load i32, i32* @errno
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %1
%37 = call i8* @strerror(i32 %34)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %27
%41 = load i64, i64* %4
br label %42
42: ; preds = %40, %25
%43 = phi i64 [ %41, %40 ], [ 0, %25 ]
%44 = load i32, i32* %3
%45 = load i32, i32* @SEEK_SET
%46 = call i64 @lseek(i32 %44, i64 %43, i32 %45)
%47 = icmp eq i64 %46, -1
br i1 %47, label %48, label %55
48: ; preds = %42
%49 = load i32, i32* @errno
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %1
%52 = call i8* @strerror(i32 %49)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %51, i8* %52)
%54 = call i32 @exit(i32 2)
unreachable
55: ; preds = %42
%56 = load i32, i32* %3
%57 = call i64 @ftruncate(i32 %56, i64 %43)
%58 = icmp eq i64 %57, -1
br i1 %58, label %59, label %66
59: ; preds = %55
%60 = load i32, i32* @errno
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %1
%63 = call i8* @strerror(i32 %60)
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %62, i8* %63)
%65 = call i32 @exit(i32 2)
unreachable
66: ; preds = %55
ret i32 0
}