Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How does gcc (any compiler) actually convert a function call, say atoi(), into assembly/machine code? Date: Fri, 23 Dec 2016 13:24:47 -0800 Organization: None to speak of Lines: 31 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="df4a13e992574b38b1b7cd38c2b29037"; logging-data="15409"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192ohc5JrPhVmkiqO2aF3AZ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:7O4IVIj21mL6Qsa28TdlJ704d9g= sha1:RrTWPBqA+j8bzkJ5WQYI148lSmo= Xref: csiph.com comp.lang.c:100094 Malcolm McLean writes: [...] > It;s virtually impossible to reverse engineer a huge hunk of > software like gcc like that. If you root around in the documentation > for developers it will tell you where the code for compiling atoi > is stored. It's probably inlined as a special function because > it is standard and so simple. A quick experiment shows that gcc generates a call to strtol() when optimization is enabled. I haven't taken the time to figure out how it does that, whether it knows that atoi() is a standard function, or whether it takes advantage of some annotation on the declaration. It should also be mentioned that atoi() is a poorly designed function. It performs no error checking. atoi("FOO") is likely to return 0, but the behavior is undefined. All the standard says is that Except for the behavior on error, [atoi is] equivalent to (int)strtol(nptr, (char **)NULL, 10) Certainly if strtol returns a result that's outside the range of int, the cast has undefined behavior. If there's an error, the standard doesn't say anything about how atoi behaves, which means the behavior is undefined. (I'd be happier if it said so explicitly.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"